Thread 是java操作多线程最核心类。
使用Thread不需要import导入别的包,因为它是在java.lang下面
package thread;class MyThread extends Thread{//在此类中重写run方法@Overridepublic void run() {System.out.println("hello world");}
}public class ThreadDemo1 {public static void main(String[] args) {Thread thread = new MyThread();thread.start();//线程中的特殊方法 - 创建一个线程}
}
在thread包中创建一个对象,Mythread子类继承Thread父类。
重写Thread类中的run方法输出hello world。
start是线程中的特殊方法,在这里的作用是创建一个新的线程,而不是调用run。
新的线程负责调用run方法来输出hello world。
新的进程调用操作系统的API,通过操作系统内核创建新线程的PCB。
并且把要执行的指令交给这个PCB。
当这个PCB被调度到CPU上的时候,也就执行到了线程run方法中的代码了。
如果run方法执行完毕,这个新的线程会销毁。
改动一下上面的代码:
package thread;class MyThread extends Thread{//在此类中重写run方法@Overridepublic void run() {while (true) {System.out.println("hello world");}}
}public class ThreadDemo1 {public static void main(String[] args) {Thread thread = new MyThread();thread.start();//线程中的特殊方法 - 创建一个线程while (true) {System.out.println("hello java");}}
}
如果此时的代码时非线程的,程序会死循环的输出 hello world,并且没有机会执行 hello java
但是实际情况是,这两条输出语句都会执行,并且都是死循环。
为了方便观察,调用sleep设置睡眠时间为1秒。
package thread;class MyThread extends Thread{//在此类中重写run方法@Overridepublic void run() {while (true) {System.out.println("hello world");//睡一会儿try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}public class ThreadDemo1 {public static void main(String[] args) {Thread thread = new MyThread();thread.start();//线程中的特殊方法 - 创建一个线程while (true) {System.out.println("hello java");//睡一会儿try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}

先执行 hello world 还是先执行 hello java 是不确定的。
操作系统调度线程的时候,是“抢占式执行的”,具体哪个先哪个后,是不确定的。
这要取决与操作系统的调度器具体的实现策略。
虽然有优先级,但是在应用程序层面上无法修改。
从应用程序(代码)的角度,看到的效果,就好像是线程的调度效果好像是随机的一样。
内核里本身并不是随机的,但是干预的元素太多了,并且应用程序这一层也无法感知到细节,就只能认为是随机的了。
抢占式执行,随机访问就是线程安全问题的罪魁祸首。
如果其中一条语句无法输入,不会影响另一条语句。
把输出 hello world 的语句给屏蔽了,但是不会影响输出 hello java
class MyThread extends Thread{//在此类中重写run方法@Overridepublic void run() {while (true) {//System.out.println("hello world");//睡一会儿try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}

就像是,张三和李四去学校,张三不来了并不会决定李四来不来。
start 和 run 之间的区别:
start是真正上创建了一个进程(从系统这里创建的),线程是独立的执行流。
run只是描述了线程要干的活是什么,如果直接在main中调用 run 。
此时没有创建新线程,全是main线程一个人干活。

第一种方法就是前面介绍的方法。
package thread;//Runnable 的作用是描述一个要“执行的任务”,run方法就是任务的执行细节
class MyRunnable implements Runnable {@Overridepublic void run() {System.out.println("hello 张三");}
}public class ThreadDemo2 {public static void main(String[] args) {//这只是描述了任务Runnable runnable = new MyRunnable();//把任务交给线程来执行Thread thread = new Thread(runnable);thread.start();}
}
如果未来要更改代码,不用多线程,
而是使用多进程,或者线程池、协程…此时的代码改动比较小。
package thread;public class ThreadDemo3 {public static void main(String[] args) {Thread thread = new Thread() {@Overridepublic void run() {System.out.println("haha");}};thread.start();}
}
上面方法先是创建了一个Thread的子类(子类没有名字),所以才叫做匿名。
然后是创建了子类的实例,并且让thread引用指向该实例。
package thread;public class ThreadDemo4 {public static void main(String[] args) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {System.out.println("hehehehe");}});thread.start();}
}
这个写法本质和2相同。
只不过是把实现 Runnable 任务交给了匿名内部类的语法。
此处是创建了一个类,实现 Runnable ,同时创建了类的实例,并且传给 Thread 的构造方法。
package thread;public class ThreadDemo5 {public static void main(String[] args) {Thread thread = new Thread(() -> {System.out.println("hellohello");});thread.start();}
}
把任务用 Lambda 表达式来描述
直接把 Lambda 传给 Thread 构造方法。
上一篇:脉的成语有哪些
下一篇:车水马龙的近义词是什么