多线程join的问题
来源:互联网 发布:柴鸡蛋逆袭网络剧下载 编辑:程序博客网 时间:2024/05/20 18:44
package com.company;/** * Created by Mr.Meng on 2016/7/1. */public class Main2 { public static void main(String[] args) { Thread2 mTh1=new Thread2("A"); Thread2 mTh2= new Thread2("B"); System.out.println("id "+mTh1.getId()); System.out.println("toString "+mTh1.toString()); System.out.println("classLoader "+mTh1.getContextClassLoader()); System.out.println("priority "+mTh1.getPriority()); System.out.println("stackTrace "+mTh1.getStackTrace()); System.out.println("state "+mTh1.getState()); System.out.println("group "+mTh1.getThreadGroup()); System.out.println("alive "+mTh1.isAlive()); System.out.println("daemon "+mTh1.isDaemon()); System.out.println("isInterrupted "+mTh1.isInterrupted()); System.out.println("------------------------------"); System.out.println("id "+mTh2.getId()); System.out.println("toString "+mTh2.toString()); System.out.println("classLoader "+mTh2.getContextClassLoader()); System.out.println("priority "+mTh2.getPriority()); System.out.println("stackTrace "+mTh2.getStackTrace()); System.out.println("state "+mTh2.getState()); System.out.println("group "+mTh2.getThreadGroup()); System.out.println("alive "+mTh2.isAlive()); System.out.println("daemon "+mTh2.isDaemon()); System.out.println("isInterrupted "+mTh2.isInterrupted()); mTh1.start(); try { mTh1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("state "+mTh1.getState()); System.out.println("state "+mTh2.getState()); mTh2.start();//这样做的话thread2等待thread1执行完以后再执行但是main的soutp也是一个线程 //cpu很可能会先让主线程跑,不对是一定让主线程跑完也就是soutp跑完后再跑thread2 System.out.println("state "+mTh1.getState()); System.out.println("state "+mTh2.getState()); }}class Thread2 extends Thread{ private String name; public Thread2(String name) { this.name=name; } public void run() { for (int i = 0; i < 100; i++) { System.out.println(name + "运行 : " + i); try { sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } }}
如下是运行结果:
id 9
toString Thread[Thread-0,5,main]
classLoader sun.misc.Launcher$AppClassLoader@19821f
priority 5
stackTrace [Ljava.lang.StackTraceElement;@10b30a7
state NEW
group java.lang.ThreadGroup[name=main,maxpri=10]
alive false
daemon false
isInterrupted false
id 10
toString Thread[Thread-1,5,main]
classLoader sun.misc.Launcher$AppClassLoader@19821f
priority 5
stackTrace [Ljava.lang.StackTraceElement;@10b30a7
state NEW
group java.lang.ThreadGroup[name=main,maxpri=10]
alive false
daemon false
isInterrupted false
A运行 : 0
……
A运行 : 99
state TERMINATED
state NEW
state TERMINATED
state RUNNABLE
B运行 : 0
………
B运行 : 99
Process finished with exit code 0
下面是当thread1和thread2同时join的时候
package com.company;/** * Created by Mr.Meng on 2016/7/2. */public class Main3 { public static void main(String[] args) { Thread thread=Thread.currentThread(); System.out.println(thread.getName()); System.out.println("args = [" + thread.getPriority() + "]"); Thread2 mTh1=new Thread2("A"); Thread2 mTh2= new Thread2("B"); mTh1.start(); mTh2.start(); try { mTh1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("state " + mTh1.getState()); System.out.println("state "+mTh2.getState()); try { mTh2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("state "+mTh1.getState()); System.out.println("state "+mTh2.getState()); }}
“C:\Program Files (x86)\Java\jdk1.6.0_16\bin\java” -Didea.launcher.port=7547 “-Didea.launcher.bin.path=E:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.2\bin” -Dfile.encoding=UTF-8 -classpath “C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.6.0_16\jre\lib\ext\sunpkcs11.jar;E:\Program Files (x86)\JetBrains\out\production\JetBrains;E:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.2\lib\idea_rt.jar” com.intellij.rt.execution.application.AppMain com.company.Main3
main
args = [5]
A运行 : 0
B运行 : 0
A运行 : 1
B运行 : 1
A运行 : 2
B运行 : 2
A运行 : 3
B运行 : 3
A运行 : 4
B运行 : 4
A运行 : 5
B运行 : 5
A运行 : 6
B运行 : 6
A运行 : 7
B运行 : 7
A运行 : 8
B运行 : 8
A运行 : 9
B运行 : 9
A运行 : 10
B运行 : 10
A运行 : 11
B运行 : 11
A运行 : 12
B运行 : 12
A运行 : 13
B运行 : 13
A运行 : 14
B运行 : 14
A运行 : 15
B运行 : 15
A运行 : 16
B运行 : 16
A运行 : 17
B运行 : 17
A运行 : 18
B运行 : 18
A运行 : 19
B运行 : 19
A运行 : 20
B运行 : 20
A运行 : 21
B运行 : 21
A运行 : 22
A运行 : 23
B运行 : 22
A运行 : 24
B运行 : 23
A运行 : 25
B运行 : 24
A运行 : 26
B运行 : 25
A运行 : 27
B运行 : 26
A运行 : 28
B运行 : 27
A运行 : 29
B运行 : 28
A运行 : 30
B运行 : 29
A运行 : 31
B运行 : 30
A运行 : 32
B运行 : 31
A运行 : 33
B运行 : 32
A运行 : 34
B运行 : 33
A运行 : 35
B运行 : 34
A运行 : 36
B运行 : 35
A运行 : 37
B运行 : 36
A运行 : 38
B运行 : 37
A运行 : 39
B运行 : 38
A运行 : 40
B运行 : 39
A运行 : 41
B运行 : 40
A运行 : 42
B运行 : 41
A运行 : 43
B运行 : 42
A运行 : 44
B运行 : 43
A运行 : 45
B运行 : 44
A运行 : 46
B运行 : 45
A运行 : 47
B运行 : 46
A运行 : 48
B运行 : 47
A运行 : 49
B运行 : 48
B运行 : 49
state TERMINATED
state TERMINATED
state TERMINATED
state TERMINATED
Process finished with exit code 0
而thread1和thread2都是子线程它们并不会等join结束后在执行而是争抢着执行
- 多线程join的问题
- 多线程-join的应用
- 多线程的join()方法
- 多线程的join方法
- python 多线程 join 的 细节问题 注意使用事项
- java多线程里面的join
- 多线程中join的理解
- java多线程 join的使用
- Java多线程的join方法
- 多线程中Join的运用(困惑的经典问题终于解决)
- 多线程 -- join
- 多线程:Thread类的Join()方法
- 初学Java多线程:join方法的使用
- 初学Java多线程:join方法的使用
- Java多线程中join方法的理解
- 初学Java多线程:join方法的使用
- Java多线程中join方法的测试
- 多线程:Thread类的Join()方法
- 查看linux性能信息cpu-内存-硬盘-网卡
- CSS之派生选择器(上下文选择器)
- Android缓存技术
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- javascript渲染单页效率简述
- 多线程join的问题
- 贪心算法-最优装载
- android23种设计模式
- Unity3D仿仙剑对话系统开发
- 七种经典排序算法最全攻略
- Androidstudio下代码混淆
- java集合框架
- 进程通信--命名管道(FIFO)
- Ubuntu安装android-ndk编译出错的处理