多线程编程
This commit is contained in:
@ -0,0 +1,19 @@
|
||||
package com.heibaiying.yieldAndJoin;
|
||||
|
||||
/**
|
||||
* 正常情况下,输出 0
|
||||
*/
|
||||
public class J1_Normal {
|
||||
|
||||
private static int j = 0;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread = new Thread(() -> {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
j++;
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
System.out.println(j);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.heibaiying.yieldAndJoin;
|
||||
|
||||
/**
|
||||
* 使用Join让线程的并行执行换成串行执行,输出:100000
|
||||
*/
|
||||
public class J2_Join {
|
||||
|
||||
private static int j = 0;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread thread = new Thread(() -> {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
j++;
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
thread.join();
|
||||
System.out.println(j);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user