多线程编程

This commit is contained in:
罗祥
2019-11-18 09:31:03 +08:00
parent 2ffc0a3a94
commit 910790be8c
39 changed files with 1605 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}