1.java多线程基本知识

1.1.进程介绍

  不管是我们开发的应用程序,还是我们运行的其他的应用程序,都需要先把程序安装在本地的硬盘上。然后找到这个程序的启动文件,

启动程序的时候,其实是电脑把当前的这个程序加载到内存中,在内存中需要给当前的程序分配一段独立的运行空间。这片空间就专门负

责当前这个程序的运行。

  不同的应用程序运行的过程中都需要在内存中分配自己独立的运行空间,彼此之间不会相互的影响。我们把每个独立应用程序在内存

的独立空间称为当前应用程序运行的一个进程。

  进程:它是内存中的一段独立的空间,可以负责当前应用程序的运行。当前这个进程负责调度当前程序中的所有运行细节

1.2.线程介绍

  启动的QQ聊天软件,需要和多个人进行聊天。这时多个人之间是不能相互影响,但是它们都位于当前QQ这个软件运行时所分配的内

存的独立空间中。

  在一个进程中,每个独立的功能都需要独立的去运行,这时又需要把当前这个进程划分成多个运行区域,每个独立的小区域(小单元)

称为一个线程。线程:它是位于进程中,负责当前进程中的某个具备独立运行资格的空间。进程是负责整个程序的运行,而线程是程序中具

体的某个独立功能的运行。一个进程中至少应该有一个线程

1.3.多线程介绍 

  现在的操作系统基本都是多用户,多任务的操作系统。每个任务就是一个进程。而在这个进程中就会有线程。

  真正可以完成程序运行和功能的实现靠的是进程中的线程。多线程:在一个进程中,我们同时开启多个线程,让多个线程同时去完成某

些任务(功能)。(比如后台服务系统,就可以用多个线程同时响应多个客户的请求)

  多线程的目的:提高程序的运行效率。

1.4.多线程运行的原理:CPU在线程中做时间片的切换

  其实真正电脑中的程序的运行不是同时在运行的。CPU负责程序的运行,而CPU在运行程序的过程中某个时刻点上,它其实只能运行一

个程序。而不是多个程序。而CPU它可以在多个程序之间进行高速的切换。而切换频率和速度太快,导致人的肉眼看不到。每个程序就是进程,

而每个进程中会有多个线程,而CPU是在这些线程之间进行切换。了解了CPU对一个任务的执行过程,我们就必须知道,多线程可以提高程序

的运行效率,但不能无限制的开线程。

1.5.实现线程的两种方式:继承Thread类和实现Runnable接口

1.5.1.继承Thread类  

package cn.thread;
import java.util.Random;
/**
* Created by Administrator on 2018/6/7.
* 线程实现方式1:继承Thread的方式
* 继承类必须重写 run() 方法,该方法是新线程的入口点。它也必须调用 start() 方法才能执行。
*/
public class MyThreadWithExtends extends Thread {
String flag;
public MyThreadWithExtends(String flag){
this.flag = flag;
}
@Override
public void run() {
String tname = Thread.currentThread().getName();
System.out.println(tname+"线程的run方法被调用……");
Random random = new Random();
for(int i=0;i<20;i++){
try {
Thread.sleep(random.nextInt(10)*100);
System.out.println(tname+ "...."+ flag);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread thread1 = new MyThreadWithExtends("a");
Thread thread2 = new MyThreadWithExtends("b");
thread1.start();
thread2.start();
//如果是调用thread的run方法,则只是一个普通的方法调用,不会开启新的线程
// thread1.run();
// thread2.run();
}
}

1.5.2.实现Runnable接口

package cn.thread;

/**
* Created by Administrator on 2018/6/7.
* 线程实现方式2:继承Thread的方式
*/
public class MyThreadWithImpliment implements Runnable{
int x;
public MyThreadWithImpliment(int x) {
this.x = x;
} public void run() {
String name = Thread.currentThread().getName();
System.out.println("线程" + name + "的run方法被调用……");
for (int i = 0; i < 10; i++) {
System.out.println(x);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Thread thread1 = new Thread(new MyThreadWithImpliment(1), "thread-1");
Thread thread2 = new Thread(new MyThreadWithImpliment(2), "thread-2");
thread1.start();
thread2.start();
// 注意调用run和调用start的区别,直接调用run,则都运行在main线程中
// thread1.run();
// thread2.run();
}
}

  

最新文章

  1. Linux Kernel代码艺术——系统调用宏定义
  2. 二、innerHTML应用测试
  3. javascript中的数组操作
  4. OC学习笔记——类别(Category)
  5. c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)
  6. JDK下sun.net.www.protocol.http.HttpURLConnection类-----Http客户端实现类的实现分析
  7. (使用步骤)ThinkPHP3.1.2中如何配置Ckeditor_4.1.1和Ckfindtor(转)
  8. 去除后台ckeditor的style=&quot;....&quot;的样式
  9. 移动app测试浅析
  10. ListView OnScrollListener详解(滑屏分页显示数据)
  11. AndroidManifest.xml文件
  12. JAVA基础---编码解码
  13. 20145237 实验一 逆向与Bof基础
  14. [LeetCode] 22. 括号生成
  15. 计算Java对象内存大小
  16. JDK 1.8源码阅读 HashSet
  17. html的初了解(更新中&#183;&#183;&#183;)
  18. possible error
  19. thinkphp实现采集功能的三种方法!
  20. 雷林鹏分享:Ruby 类和对象

热门文章

  1. vue/cli 3.0配置NODE_ENV
  2. 非常好用的vue数字滚动插件vue-countTo
  3. activeMQ(2)
  4. Angular里使用(image-compressor.js)图片压缩
  5. A New 3-bit Programming Algorithm using SLC-to-TLC Migration for 8MBs High Performance TLC NAND Flash Memory
  6. zping ping包工具20180605.exe测试版
  7. [转帖]瀚高数据库创建uuid的方法
  8. Response.write()弹出窗口的问题!
  9. MySQL优化——索引
  10. 【深入浅出-JVM】(2):原码、反码、补码