Skip to content

Commit a1a1d8d

Browse files
thread tutorial (examplehub#180)
* thread tutorial * remove log.txt
1 parent fe55824 commit a1a1d8d

File tree

10 files changed

+196
-37
lines changed

10 files changed

+196
-37
lines changed

src/main/java/com/examplehub/basics/RunnableExample.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class InterruptedThread {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class VolatileExample {
4+
}

src/main/java/com/examplehub/basics/thread/YieldThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class YieldThread implements Runnable {
44

55
@Override
66
public void run() {
7-
System.out.println(Thread.currentThread().getName() + " is running");
7+
// System.out.println(Thread.currentThread().getName() + " is running");
88
Thread.yield();
99
System.out.println(Thread.currentThread().getName() + " is stopped");
1010
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.examplehub.basics.thread;
2+
3+
import org.junit.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.Test;
5+
6+
class InterruptedThreadTest {
7+
@Test
8+
@Disabled
9+
void test() throws InterruptedException {
10+
//TODO
11+
class MyThread extends Thread {
12+
@Override
13+
public void run() {
14+
while (!isInterrupted()) {
15+
System.out.println(Thread.currentThread().getName() + " running");
16+
}
17+
System.out.println(Thread.currentThread().getName() + " stopped");
18+
}
19+
}
20+
21+
Thread thread = new MyThread();
22+
thread.start();
23+
Thread.sleep(1);
24+
thread.interrupt();
25+
thread.join();
26+
System.out.println(Thread.currentThread().getName() + " stopped");
27+
}
28+
}

src/test/java/com/examplehub/basics/thread/JoinThreadTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
59
class JoinThreadTest {
610
@Test
711
void test() throws InterruptedException {
@@ -14,4 +18,21 @@ void test() throws InterruptedException {
1418
System.out.println(Thread.currentThread().getName() + i);
1519
}
1620
}
21+
22+
@Test
23+
void testJoinWithTime() throws InterruptedException {
24+
AtomicInteger number = new AtomicInteger();
25+
Thread thread = new Thread(()->{
26+
try {
27+
Thread.sleep(100);
28+
number.set(1);
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
});
33+
thread.start();
34+
thread.join(50);
35+
assertEquals(0, number.get());
36+
assertEquals(Thread.State.TIMED_WAITING, thread.getState());
37+
}
1738
}

src/test/java/com/examplehub/basics/thread/SellTicketThreadTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.examplehub.basics.thread;
22

3-
import static org.junit.jupiter.api.Assertions.*;
4-
53
import org.junit.jupiter.api.Test;
64

75
class SellTicketThreadTest {

src/test/java/com/examplehub/basics/thread/SynchronizedThreadTest.java

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.examplehub.basics.thread;
22

3+
import org.junit.jupiter.api.Test;
4+
35
import static org.junit.jupiter.api.Assertions.*;
46

5-
import org.junit.jupiter.api.Test;
7+
class Counter {
8+
public static final Object lock = new Object();
9+
public static int count = 0;
10+
}
611

712
class SynchronizedThreadTest {
13+
814
@Test
915
void test() {
1016
SynchronizedThread synchronizedThread = new SynchronizedThread();
@@ -14,4 +20,105 @@ void test() {
1420
first.start();
1521
second.start();
1622
}
23+
24+
@Test
25+
void testCounter() throws InterruptedException {
26+
class AddThread extends Thread {
27+
@Override
28+
public void run() {
29+
for (int i = 0; i < 100; ++i) {
30+
synchronized (Counter.lock) {
31+
Counter.count++;
32+
}
33+
}
34+
}
35+
}
36+
class DesThread extends Thread {
37+
@Override
38+
public void run() {
39+
for (int i = 0; i < 100; i++) {
40+
synchronized (Counter.lock) {
41+
Counter.count--;
42+
}
43+
}
44+
}
45+
}
46+
AddThread addThread = new AddThread();
47+
DesThread desThread = new DesThread();
48+
addThread.start();
49+
desThread.start();
50+
addThread.join();
51+
desThread.join();
52+
assertEquals(0, Counter.count);
53+
}
54+
55+
@Test
56+
void testSynchronizedThis() throws InterruptedException {
57+
class Counter {
58+
private int count = 0;
59+
public void dec() {
60+
synchronized (this) {
61+
count = count - 1;
62+
}
63+
}
64+
public void add() {
65+
synchronized (this) {
66+
count = count + 1;
67+
}
68+
}
69+
70+
public int getCount() {
71+
return count;
72+
}
73+
}
74+
Counter counter = new Counter();
75+
var firstThread = new Thread(() -> {
76+
for (int i = 0; i < 100; i++) {
77+
counter.add();
78+
}
79+
});
80+
var secondThread = new Thread(() -> {
81+
for (int i = 0; i < 100; i++) {
82+
counter.dec();
83+
}
84+
});
85+
firstThread.start();
86+
secondThread.start();
87+
firstThread.join();
88+
secondThread.join();
89+
assertEquals(0, counter.getCount());
90+
}
91+
92+
@Test
93+
void testSynchronizedFunction() throws InterruptedException {
94+
class Counter {
95+
private int count = 0;
96+
public synchronized void dec() {
97+
count = count - 1;
98+
}
99+
public synchronized void add() {
100+
count = count + 1;
101+
}
102+
103+
public int getCount() {
104+
return count;
105+
}
106+
}
107+
Counter counter = new Counter();
108+
var firstThread = new Thread(() -> {
109+
for (int i = 0; i < 100; i++) {
110+
counter.add();
111+
}
112+
});
113+
var secondThread = new Thread(() -> {
114+
for (int i = 0; i < 100; i++) {
115+
counter.dec();
116+
}
117+
});
118+
firstThread.start();
119+
secondThread.start();
120+
firstThread.join();
121+
secondThread.join();
122+
assertEquals(0, counter.getCount());
123+
}
17124
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.examplehub.basics.thread;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class VolatileExampleTest {
8+
int number = 0;
9+
@Test
10+
void test() throws InterruptedException {
11+
class MyThread extends Thread {
12+
@Override
13+
public void run() {
14+
for (int i = 1; i <= 10; ++i) {
15+
number++;
16+
}
17+
}
18+
}
19+
MyThread firstThread = new MyThread();
20+
MyThread secondThread = new MyThread();
21+
MyThread thirdThread = new MyThread();
22+
firstThread.start();
23+
secondThread.start();
24+
thirdThread.start();
25+
firstThread.join();
26+
secondThread.join();
27+
thirdThread.join();
28+
assertEquals(30, number);
29+
}
30+
}

src/test/java/com/examplehub/basics/thread/YieldThreadTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.examplehub.basics.thread;
22

3-
import static org.junit.jupiter.api.Assertions.*;
4-
53
import org.junit.jupiter.api.Test;
64

75
class YieldThreadTest {

0 commit comments

Comments
 (0)