Skip to content

Commit 5edff68

Browse files
author
fengyuan.wan
committed
sds
1 parent 509ea06 commit 5edff68

File tree

11 files changed

+205
-20
lines changed

11 files changed

+205
-20
lines changed

03concurrency/0301/src/main/java/java0/conc0301/ThreadCount.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
public class ThreadCount {
44
public static void main(String[] args) throws InterruptedException {
55
//System.out.println("system:"+Thread.currentThread().getThreadGroup().getParent());
6-
Thread.currentThread().getThreadGroup().getParent().list();
6+
// Thread.currentThread().getThreadGroup().getParent().list();
77

88
// System.out.println("main:"+Thread.currentThread().getThreadGroup());
9-
// Thread.currentThread().getThreadGroup().list();
9+
Thread.currentThread().getThreadGroup().list();
1010
}
1111
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package java0.conc0301.base;
2+
3+
/**
4+
* @Desc
5+
* @Author wfy
6+
* @Date 2021/2/1 14:19
7+
*/
8+
public class FinalTest {
9+
final int a;
10+
11+
public FinalTest() {
12+
a = 2;
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package java0.conc0301.base;
2+
3+
/**
4+
* @Desc
5+
* @Author wfy
6+
* @Date 2021/2/1 14:14
7+
*/
8+
public class IntStreamTest {
9+
public static void main(String[] args) {
10+
int loopNum = 100_0000;
11+
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java0.conc0301.base;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @Desc
7+
* @Author wfy
8+
* @Date 2021/1/26 13:32
9+
*/
10+
public class InterruptedTest {
11+
12+
public static void main(String[] args) {
13+
Thread t1 = new Thread(() -> {
14+
System.out.println("run:" + Thread.currentThread().getName() + Thread.currentThread().getId());
15+
try {
16+
TimeUnit.SECONDS.sleep(1);
17+
} catch (InterruptedException e) {
18+
throw new RuntimeException();
19+
}
20+
Thread.interrupted();
21+
System.out.println("alive!");
22+
});
23+
t1.start();
24+
t1.interrupt();
25+
System.out.println("111");
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java0.conc0301.base;
2+
3+
public class PossibleReordering {
4+
static int x = 0, y = 0;
5+
static int a = 0, b = 0;
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
Thread one = new Thread(new Runnable() {
9+
public void run() {
10+
a = 1;
11+
x = b;
12+
}
13+
});
14+
15+
Thread other = new Thread(new Runnable() {
16+
public void run() {
17+
b = 1;
18+
y = a;
19+
}
20+
});
21+
one.start();
22+
other.start();
23+
one.join();
24+
other.join();
25+
System.out.println(" (" + x + "," + y + ")");
26+
}
27+
}

03concurrency/0301/src/main/java/java0/conc0301/op/Join.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public static void main(String[] args) {
1010
thread1.setOo(oo);
1111
thread1.start();
1212

13-
synchronized (oo) { // 这里用oo或thread1/this
13+
synchronized (thread1) { // 这里用oo或thread1/this
1414
for (int i = 0; i < 100; i++) {
1515
if (i == 20) {
1616
try {
17-
oo.wait(0);
18-
//thread1.join();
17+
// oo.wait(0);
18+
thread1.join();
1919
} catch (InterruptedException e) {
2020
e.printStackTrace();
2121
}
@@ -42,7 +42,7 @@ public MyThread(String name) {
4242

4343
@Override
4444
public void run() {
45-
synchronized (oo) { // 这里用oo或this,效果不同
45+
synchronized (this) { // 这里用oo或this,效果不同
4646
for (int i = 0; i < 100; i++) {
4747
System.out.println(name + i);
4848
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package java0.conc0301.op;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
public class WaitAndNotify01 {
7+
8+
public static void main(String[] args) {
9+
Queue q = new Queue();
10+
Thread t1 = new Thread(q::consume);
11+
Thread t2 = new Thread(q::produce);
12+
t1.start();
13+
t2.start();
14+
}
15+
16+
17+
private static class Queue {
18+
private AtomicInteger count = new AtomicInteger(0);
19+
20+
private static final int SIZE = 10;
21+
22+
public void consume() {
23+
while (true) {
24+
synchronized (this) {
25+
System.out.println("consume " + count.get());
26+
if (count.get() <= 0) {
27+
System.out.println("队列为空。。");
28+
try {
29+
wait();
30+
} catch (InterruptedException e) {
31+
}
32+
} else {
33+
count.decrementAndGet();
34+
}
35+
notifyAll();
36+
}
37+
}
38+
}
39+
40+
public void produce() {
41+
while (true) {
42+
synchronized (this) {
43+
System.out.println("produce " + count.get());
44+
if (count.get() >= SIZE) {
45+
System.out.println("队列满了");
46+
try {
47+
wait();
48+
} catch (InterruptedException e) {
49+
}
50+
} else {
51+
count.incrementAndGet();
52+
}
53+
notifyAll();
54+
}
55+
}
56+
}
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package java0.conc0301.op;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @Desc
7+
* @Author wfy
8+
* @Date 2021/1/25 15:12
9+
*/
10+
public class WaitTest {
11+
public static void main(String[] args) throws InterruptedException {
12+
Thread t = new Thread(() -> {
13+
while (true) {
14+
synchronized (WaitTest.class) {
15+
System.out.println(1);
16+
try {
17+
TimeUnit.SECONDS.sleep(1);
18+
WaitTest.class.wait();
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
23+
24+
System.out.println(3);
25+
}
26+
try {
27+
TimeUnit.SECONDS.sleep(1);
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
});
33+
t.start();
34+
TimeUnit.SECONDS.sleep(1);
35+
while (true) {
36+
synchronized (WaitTest.class) {
37+
System.out.println(2);
38+
WaitTest.class.notify();
39+
}
40+
TimeUnit.SECONDS.sleep(1);
41+
}
42+
43+
}
44+
}

03concurrency/0301/src/main/java/java0/conc0301/pool/ThreadPoolTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public class ThreadPoolTest {
1414
public static void main(String[] args) {
1515
TimeUnit unit;
1616
BlockingQueue workQueue;
17-
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, , unit, workQueue);
17+
// ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, , unit, workQueue);
1818
}
1919
}

03concurrency/0301/src/main/java/java0/conc0301/sync/Counter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static void main(String[] args) throws InterruptedException {
3838
});
3939
t1.start();
4040
t2.start();
41+
// t1.join();
42+
// t2.join();
4143
Thread.sleep(1000);
4244
// while (Thread.activeCount()>2){//当前线程的线程组中的数量>2
4345
// Thread.yield();

0 commit comments

Comments
 (0)