java多线程中start和run的区别
Posted yjl33
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程中start和run的区别相关的知识,希望对你有一定的参考价值。
1) start:
用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。
2) run:
run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。这两个方法应该都比较熟悉,把需要并行处理的代码放在run()方法中,start()方法启动线程将自动调用 run()方法,这是由jvm的内存机制规定的。并且run()方法必须是public访问权限,返回值类型为void.。
示例代码:查看执行日志就能很清楚的分清start和run的区别package com.yjl.thread; public class ThreadTest public static void main(String[] args) Runner1 runner1 = new Runner1(); Runner2 runner2 = new Runner2(); // Thread(Runnable target) 分配新的 Thread 对象。 Thread thread1 = new Thread(runner1); Thread thread2 = new Thread(runner2); thread1.start(); //执行start,thread1与thread2交叉执行 thread2.start(); //thread1.run(); //执行run,thread1与thread2顺序执行 //thread2.run(); class Runner1 implements Runnable // 实现了Runnable接口,jdk就知道这个类是一个线程 public void run() for (int i = 0; i < 100; i++) Thread.currentThread().getName(); System.out.println(Thread.currentThread().getName()+"----进入Runner1运行状态—--" + i); class Runner2 implements Runnable // 实现了Runnable接口,jdk就知道这个类是一个线程 public void run() for (int i = 0; i < 100; i++) System.out.println(Thread.currentThread().getName()+"====进入Runner2运行状态====" + i);
运行 thread1.start(); //执行start,thread1与thread2交叉执行
thread2.start();
后的日志:其中Tread-0,Thread-1为当前执行线程的名称,可以看到2个线程交叉运行。Thread-0----进入Runner1运行状态—--0 Thread-0----进入Runner1运行状态—--1 Thread-0----进入Runner1运行状态—--2 Thread-0----进入Runner1运行状态—--3 Thread-0----进入Runner1运行状态—--4 Thread-0----进入Runner1运行状态—--5 Thread-0----进入Runner1运行状态—--6 Thread-0----进入Runner1运行状态—--7 Thread-0----进入Runner1运行状态—--8 Thread-0----进入Runner1运行状态—--9 Thread-0----进入Runner1运行状态—--10 Thread-0----进入Runner1运行状态—--11 Thread-0----进入Runner1运行状态—--12 Thread-0----进入Runner1运行状态—--13 Thread-0----进入Runner1运行状态—--14 Thread-0----进入Runner1运行状态—--15 Thread-0----进入Runner1运行状态—--16 Thread-0----进入Runner1运行状态—--17 Thread-0----进入Runner1运行状态—--18 Thread-0----进入Runner1运行状态—--19 Thread-0----进入Runner1运行状态—--20 Thread-0----进入Runner1运行状态—--21 Thread-0----进入Runner1运行状态—--22 Thread-0----进入Runner1运行状态—--23 Thread-0----进入Runner1运行状态—--24 Thread-0----进入Runner1运行状态—--25 Thread-0----进入Runner1运行状态—--26 Thread-0----进入Runner1运行状态—--27 Thread-0----进入Runner1运行状态—--28 Thread-0----进入Runner1运行状态—--29 Thread-0----进入Runner1运行状态—--30 Thread-0----进入Runner1运行状态—--31 Thread-0----进入Runner1运行状态—--32 Thread-0----进入Runner1运行状态—--33 Thread-0----进入Runner1运行状态—--34 Thread-0----进入Runner1运行状态—--35 Thread-0----进入Runner1运行状态—--36 Thread-0----进入Runner1运行状态—--37 Thread-0----进入Runner1运行状态—--38 Thread-0----进入Runner1运行状态—--39 Thread-0----进入Runner1运行状态—--40 Thread-0----进入Runner1运行状态—--41 Thread-0----进入Runner1运行状态—--42 Thread-0----进入Runner1运行状态—--43 Thread-0----进入Runner1运行状态—--44 Thread-0----进入Runner1运行状态—--45 Thread-0----进入Runner1运行状态—--46 Thread-0----进入Runner1运行状态—--47 Thread-0----进入Runner1运行状态—--48 Thread-0----进入Runner1运行状态—--49 Thread-1====进入Runner2运行状态====0 Thread-0----进入Runner1运行状态—--50 Thread-0----进入Runner1运行状态—--51 Thread-1====进入Runner2运行状态====1 Thread-0----进入Runner1运行状态—--52 Thread-1====进入Runner2运行状态====2 Thread-0----进入Runner1运行状态—--53 Thread-1====进入Runner2运行状态====3 Thread-0----进入Runner1运行状态—--54 Thread-1====进入Runner2运行状态====4 Thread-0----进入Runner1运行状态—--55 Thread-1====进入Runner2运行状态====5 Thread-0----进入Runner1运行状态—--56 Thread-1====进入Runner2运行状态====6 Thread-0----进入Runner1运行状态—--57 Thread-1====进入Runner2运行状态====7 Thread-0----进入Runner1运行状态—--58 Thread-1====进入Runner2运行状态====8 Thread-0----进入Runner1运行状态—--59 Thread-1====进入Runner2运行状态====9 Thread-0----进入Runner1运行状态—--60 Thread-1====进入Runner2运行状态====10 Thread-0----进入Runner1运行状态—--61 Thread-1====进入Runner2运行状态====11 Thread-0----进入Runner1运行状态—--62 Thread-1====进入Runner2运行状态====12 Thread-0----进入Runner1运行状态—--63 Thread-0----进入Runner1运行状态—--64 Thread-0----进入Runner1运行状态—--65 Thread-0----进入Runner1运行状态—--66 Thread-1====进入Runner2运行状态====13 Thread-0----进入Runner1运行状态—--67 Thread-1====进入Runner2运行状态====14 Thread-0----进入Runner1运行状态—--68 Thread-1====进入Runner2运行状态====15 Thread-0----进入Runner1运行状态—--69 Thread-1====进入Runner2运行状态====16 Thread-0----进入Runner1运行状态—--70 Thread-1====进入Runner2运行状态====17 Thread-0----进入Runner1运行状态—--71 Thread-1====进入Runner2运行状态====18 Thread-0----进入Runner1运行状态—--72 Thread-1====进入Runner2运行状态====19 Thread-0----进入Runner1运行状态—--73 Thread-0----进入Runner1运行状态—--74 Thread-1====进入Runner2运行状态====20 Thread-0----进入Runner1运行状态—--75 Thread-1====进入Runner2运行状态====21 Thread-0----进入Runner1运行状态—--76 Thread-1====进入Runner2运行状态====22 Thread-0----进入Runner1运行状态—--77 Thread-1====进入Runner2运行状态====23 Thread-0----进入Runner1运行状态—--78 Thread-1====进入Runner2运行状态====24 Thread-0----进入Runner1运行状态—--79 Thread-1====进入Runner2运行状态====25 Thread-0----进入Runner1运行状态—--80 Thread-1====进入Runner2运行状态====26 Thread-0----进入Runner1运行状态—--81 Thread-1====进入Runner2运行状态====27 Thread-0----进入Runner1运行状态—--82 Thread-0----进入Runner1运行状态—--83 Thread-0----进入Runner1运行状态—--84 Thread-0----进入Runner1运行状态—--85 Thread-0----进入Runner1运行状态—--86 Thread-0----进入Runner1运行状态—--87 Thread-0----进入Runner1运行状态—--88 Thread-0----进入Runner1运行状态—--89 Thread-0----进入Runner1运行状态—--90 Thread-0----进入Runner1运行状态—--91 Thread-1====进入Runner2运行状态====28 Thread-0----进入Runner1运行状态—--92 Thread-0----进入Runner1运行状态—--93 Thread-1====进入Runner2运行状态====29 Thread-0----进入Runner1运行状态—--94 Thread-1====进入Runner2运行状态====30 Thread-0----进入Runner1运行状态—--95 Thread-1====进入Runner2运行状态====31 Thread-1====进入Runner2运行状态====32 Thread-0----进入Runner1运行状态—--96 Thread-1====进入Runner2运行状态====33 Thread-1====进入Runner2运行状态====34 Thread-1====进入Runner2运行状态====35 Thread-1====进入Runner2运行状态====36 Thread-1====进入Runner2运行状态====37 Thread-1====进入Runner2运行状态====38 Thread-1====进入Runner2运行状态====39 Thread-1====进入Runner2运行状态====40 Thread-1====进入Runner2运行状态====41 Thread-1====进入Runner2运行状态====42 Thread-1====进入Runner2运行状态====43 Thread-1====进入Runner2运行状态====44 Thread-1====进入Runner2运行状态====45 Thread-1====进入Runner2运行状态====46 Thread-1====进入Runner2运行状态====47 Thread-1====进入Runner2运行状态====48 Thread-1====进入Runner2运行状态====49 Thread-1====进入Runner2运行状态====50 Thread-1====进入Runner2运行状态====51 Thread-1====进入Runner2运行状态====52 Thread-1====进入Runner2运行状态====53 Thread-1====进入Runner2运行状态====54 Thread-1====进入Runner2运行状态====55 Thread-1====进入Runner2运行状态====56 Thread-1====进入Runner2运行状态====57 Thread-1====进入Runner2运行状态====58 Thread-1====进入Runner2运行状态====59 Thread-1====进入Runner2运行状态====60 Thread-1====进入Runner2运行状态====61 Thread-1====进入Runner2运行状态====62 Thread-1====进入Runner2运行状态====63 Thread-1====进入Runner2运行状态====64 Thread-1====进入Runner2运行状态====65 Thread-1====进入Runner2运行状态====66 Thread-1====进入Runner2运行状态====67 Thread-1====进入Runner2运行状态====68 Thread-1====进入Runner2运行状态====69 Thread-1====进入Runner2运行状态====70 Thread-1====进入Runner2运行状态====71 Thread-1====进入Runner2运行状态====72 Thread-1====进入Runner2运行状态====73 Thread-1====进入Runner2运行状态====74 Thread-1====进入Runner2运行状态====75 Thread-1====进入Runner2运行状态====76 Thread-1====进入Runner2运行状态====77 Thread-1====进入Runner2运行状态====78 Thread-1====进入Runner2运行状态====79 Thread-1====进入Runner2运行状态====80 Thread-1====进入Runner2运行状态====81 Thread-1====进入Runner2运行状态====82 Thread-1====进入Runner2运行状态====83 Thread-1====进入Runner2运行状态====84 Thread-0----进入Runner1运行状态—--97 Thread-0----进入Runner1运行状态—--98 Thread-0----进入Runner1运行状态—--99 Thread-1====进入Runner2运行状态====85 Thread-1====进入Runner2运行状态====86 Thread-1====进入Runner2运行状态====87 Thread-1====进入Runner2运行状态====88 Thread-1====进入Runner2运行状态====89 Thread-1====进入Runner2运行状态====90 Thread-1====进入Runner2运行状态====91 Thread-1====进入Runner2运行状态====92 Thread-1====进入Runner2运行状态====93 Thread-1====进入Runner2运行状态====94 Thread-1====进入Runner2运行状态====95 Thread-1====进入Runner2运行状态====96 Thread-1====进入Runner2运行状态====97 Thread-1====进入Runner2运行状态====98 Thread-1====进入Runner2运行状态====99
然后我们在运行thread1.run(); //执行run,thread1与thread2顺序执行
thread2.run();
得到日志:main----进入Runner1运行状态—--0 main----进入Runner1运行状态—--1 main----进入Runner1运行状态—--2 main----进入Runner1运行状态—--3 main----进入Runner1运行状态—--4 main----进入Runner1运行状态—--5 main----进入Runner1运行状态—--6 main----进入Runner1运行状态—--7 main----进入Runner1运行状态—--8 main----进入Runner1运行状态—--9 main----进入Runner1运行状态—--10 main----进入Runner1运行状态—--11 main----进入Runner1运行状态—--12 main----进入Runner1运行状态—--13 main----进入Runner1运行状态—--14 main----进入Runner1运行状态—--15 main----进入Runner1运行状态—--16 main----进入Runner1运行状态—--17 main----进入Runner1运行状态—--18 main----进入Runner1运行状态—--19 main----进入Runner1运行状态—--20 main----进入Runner1运行状态—--21 main----进入Runner1运行状态—--22 main----进入Runner1运行状态—--23 main----进入Runner1运行状态—--24 main----进入Runner1运行状态—--25 main----进入Runner1运行状态—--26 main----进入Runner1运行状态—--27 main----进入Runner1运行状态—--28 main----进入Runner1运行状态—--29 main----进入Runner1运行状态—--30 main----进入Runner1运行状态—--31 main----进入Runner1运行状态—--32 main----进入Runner1运行状态—--33 main----进入Runner1运行状态—--34 main----进入Runner1运行状态—--35 main----进入Runner1运行状态—--36 main----进入Runner1运行状态—--37 main----进入Runner1运行状态—--38 main----进入Runner1运行状态—--39 main----进入Runner1运行状态—--40 main----进入Runner1运行状态—--41 main----进入Runner1运行状态—--42 main----进入Runner1运行状态—--43 main----进入Runner1运行状态—--44 main----进入Runner1运行状态—--45 main----进入Runner1运行状态—--46 main----进入Runner1运行状态—--47 main----进入Runner1运行状态—--48 main----进入Runner1运行状态—--49 main----进入Runner1运行状态—--50 main----进入Runner1运行状态—--51 main----进入Runner1运行状态—--52 main----进入Runner1运行状态—--53 main----进入Runner1运行状态—--54 main----进入Runner1运行状态—--55 main----进入Runner1运行状态—--56 main----进入Runner1运行状态—--57 main----进入Runner1运行状态—--58 main----进入Runner1运行状态—--59 main----进入Runner1运行状态—--60 main----进入Runner1运行状态—--61 main----进入Runner1运行状态—--62 main----进入Runner1运行状态—--63 main----进入Runner1运行状态—--64 main----进入Runner1运行状态—--65 main----进入Runner1运行状态—--66 main----进入Runner1运行状态—--67 main----进入Runner1运行状态—--68 main----进入Runner1运行状态—--69 main----进入Runner1运行状态—--70 main----进入Runner1运行状态—--71 main----进入Runner1运行状态—--72 main----进入Runner1运行状态—--73 main----进入Runner1运行状态—--74 main----进入Runner1运行状态—--75 main----进入Runner1运行状态—--76 main----进入Runner1运行状态—--77 main----进入Runner1运行状态—--78 main----进入Runner1运行状态—--79 main----进入Runner1运行状态—--80 main----进入Runner1运行状态—--81 main----进入Runner1运行状态—--82 main----进入Runner1运行状态—--83 main----进入Runner1运行状态—--84 main----进入Runner1运行状态—--85 main----进入Runner1运行状态—--86 main----进入Runner1运行状态—--87 main----进入Runner1运行状态—--88 main----进入Runner1运行状态—--89 main----进入Runner1运行状态—--90 main----进入Runner1运行状态—--91 main----进入Runner1运行状态—--92 main----进入Runner1运行状态—--93 main----进入Runner1运行状态—--94 main----进入Runner1运行状态—--95 main----进入Runner1运行状态—--96 main----进入Runner1运行状态—--97 main----进入Runner1运行状态—--98 main----进入Runner1运行状态—--99 main====进入Runner2运行状态====0 main====进入Runner2运行状态====1 main====进入Runner2运行状态====2 main====进入Runner2运行状态====3 main====进入Runner2运行状态====4 main====进入Runner2运行状态====5 main====进入Runner2运行状态====6 main====进入Runner2运行状态====7 main====进入Runner2运行状态====8 main====进入Runner2运行状态====9 main====进入Runner2运行状态====10 main====进入Runner2运行状态====11 main====进入Runner2运行状态====12 main====进入Runner2运行状态====13 main====进入Runner2运行状态====14 main====进入Runner2运行状态====15 main====进入Runner2运行状态====16 main====进入Runner2运行状态====17 main====进入Runner2运行状态====18 main====进入Runner2运行状态====19 main====进入Runner2运行状态====20 main====进入Runner2运行状态====21 main====进入Runner2运行状态====22 main====进入Runner2运行状态====23 main====进入Runner2运行状态====24 main====进入Runner2运行状态====25 main====进入Runner2运行状态====26 main====进入Runner2运行状态====27 main====进入Runner2运行状态====28 main====进入Runner2运行状态====29 main====进入Runner2运行状态====30 main====进入Runner2运行状态====31 main====进入Runner2运行状态====32 main====进入Runner2运行状态====33 main====进入Runner2运行状态====34 main====进入Runner2运行状态====35 main====进入Runner2运行状态====36 main====进入Runner2运行状态====37 main====进入Runner2运行状态====38 main====进入Runner2运行状态====39 main====进入Runner2运行状态====40 main====进入Runner2运行状态====41 main====进入Runner2运行状态====42 main====进入Runner2运行状态====43 main====进入Runner2运行状态====44 main====进入Runner2运行状态====45 main====进入Runner2运行状态====46 main====进入Runner2运行状态====47 main====进入Runner2运行状态====48 main====进入Runner2运行状态====49 main====进入Runner2运行状态====50 main====进入Runner2运行状态====51 main====进入Runner2运行状态====52 main====进入Runner2运行状态====53 main====进入Runner2运行状态====54 main====进入Runner2运行状态====55 main====进入Runner2运行状态====56 main====进入Runner2运行状态====57 main====进入Runner2运行状态====58 main====进入Runner2运行状态====59 main====进入Runner2运行状态====60 main====进入Runner2运行状态====61 main====进入Runner2运行状态====62 main====进入Runner2运行状态====63 main====进入Runner2运行状态====64 main====进入Runner2运行状态====65 main====进入Runner2运行状态====66 main====进入Runner2运行状态====67 main====进入Runner2运行状态====68 main====进入Runner2运行状态====69 main====进入Runner2运行状态====70 main====进入Runner2运行状态====71 main====进入Runner2运行状态====72 main====进入Runner2运行状态====73 main====进入Runner2运行状态====74 main====进入Runner2运行状态====75 main====进入Runner2运行状态====76 main====进入Runner2运行状态====77 main====进入Runner2运行状态====78 main====进入Runner2运行状态====79 main====进入Runner2运行状态====80 main====进入Runner2运行状态====81 main====进入Runner2运行状态====82 main====进入Runner2运行状态====83 main====进入Runner2运行状态====84 main====进入Runner2运行状态====85 main====进入Runner2运行状态====86 main====进入Runner2运行状态====87 main====进入Runner2运行状态====88 main====进入Runner2运行状态====89 main====进入Runner2运行状态====90 main====进入Runner2运行状态====91 main====进入Runner2运行状态====92 main====进入Runner2运行状态====93 main====进入Runner2运行状态====94 main====进入Runner2运行状态====95 main====进入Runner2运行状态====96 main====进入Runner2运行状态====97 main====进入Runner2运行状态====98 main====进入Runner2运行状态====99
其中main为当先运行线程名称,可以看出就是一个线程在运行
通过对比日志就能清楚分清楚start和run的区别
以上是关于java多线程中start和run的区别的主要内容,如果未能解决你的问题,请参考以下文章