Skip to content

Commit 64bed62

Browse files
authoredNov 10, 2021
update thread (#204)
* update thread * fixed build on gradle
1 parent 294c93e commit 64bed62

22 files changed

+360
-145
lines changed
 

‎src/main/java/com/examplehub/basics/thread/CallableExample.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,5 @@
22

33
import java.util.concurrent.Callable;
44

5-
public class CallableExample implements Callable<Integer> {
6-
7-
@Override
8-
public Integer call() throws Exception {
9-
int sum = 0;
10-
for (int i = 1; i <= 100; ++i) {
11-
sum += i;
12-
}
13-
return sum;
14-
}
5+
public class CallableExample {
156
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
package com.examplehub.basics.thread;
22

3-
public class DaemonThread implements Runnable {
4-
5-
@Override
6-
public void run() {
7-
while (Thread.currentThread().getState() != Thread.State.TERMINATED) {
8-
System.out.println("daemon thread is terminated");
9-
}
10-
}
3+
public class DaemonThread {
114
}

‎src/main/java/com/examplehub/basics/thread/DeadLockExample.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ public static void main(String[] args) {
99
Runnable runnableA =
1010
() -> {
1111
synchronized (deadLockExample.resourceA) {
12-
System.out.println(Thread.currentThread().getName() + "get resourceA");
12+
System.out.println(Thread.currentThread().getName() + " get resourceA");
1313
try {
1414
Thread.sleep(1000);
1515
} catch (InterruptedException e) {
1616
e.printStackTrace();
1717
}
1818
System.out.println(Thread.currentThread().getName() + " is trying to get resourceB");
1919
synchronized (deadLockExample.resourceB) {
20-
System.out.println(Thread.currentThread().getName() + "get resourceB");
20+
System.out.println(Thread.currentThread().getName() + " get resourceB");
2121
}
2222
}
2323
};
2424

2525
Runnable runnableB =
2626
() -> {
2727
synchronized (deadLockExample.resourceB) {
28-
System.out.println(Thread.currentThread().getName() + "get resourceB");
28+
System.out.println(Thread.currentThread().getName() + " get resourceB");
2929
try {
3030
Thread.sleep(1000);
3131
} catch (InterruptedException e) {
3232
e.printStackTrace();
3333
}
3434
System.out.println(Thread.currentThread().getName() + " is trying to get resourceA");
3535
synchronized (deadLockExample.resourceA) {
36-
System.out.println(Thread.currentThread().getName() + "get resourceA");
36+
System.out.println(Thread.currentThread().getName() + " get resourceA");
3737
}
3838
}
3939
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
package com.examplehub.basics.thread;
22

3-
public class ExampleRunnable implements Runnable {
4-
5-
@Override
6-
public void run() {
7-
for (int i = 0; i < 10; ++i) {
8-
System.out.println(Thread.currentThread().getName() + "->" + i);
9-
}
10-
}
3+
public class ExampleRunnable{
114
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
package com.examplehub.basics.thread;
22

3-
public class ExampleThread extends Thread {
4-
@Override
5-
public void run() {
6-
for (int i = 0; i < 10; ++i) {
7-
System.out.println(getName() + "->" + i);
8-
}
9-
}
3+
public class ExampleThread {
104
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
package com.examplehub.basics.thread;
22

3-
public class JoinThread implements Runnable {
4-
@Override
5-
public void run() {
6-
for (int i = 0; i < 10; i++) {
7-
System.out.println(Thread.currentThread().getName() + i);
8-
}
9-
}
3+
public class JoinThread {
104
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.examplehub.basics.thread;
22

3-
public class PriorityOfThread implements Runnable {
4-
@Override
5-
public void run() {
6-
System.out.println(
7-
Thread.currentThread().getName() + "-> priority = " + Thread.currentThread().getPriority());
8-
}
3+
public class PriorityOfThread {
4+
95
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class SleepExample {
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
package com.examplehub.basics.thread;
22

3-
public class StopThread implements Runnable {
4-
5-
private boolean flag = true;
6-
7-
@Override
8-
public synchronized void run() {
9-
while (true) {
10-
for (int i = 1; ; ++i) {
11-
System.out.println(i);
12-
try {
13-
Thread.sleep(500);
14-
} catch (InterruptedException e) {
15-
e.printStackTrace();
16-
}
17-
}
18-
}
19-
}
20-
21-
public void stop() {
22-
flag = false;
23-
System.out.println("sub thread stopped");
24-
}
3+
public class StopThread {
254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.thread;
2+
3+
public class TakeMoneyFromAccountExample {
4+
}

‎src/test/java/com/examplehub/basics/thread/CallableExampleTest.java

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
11
package com.examplehub.basics.thread;
22

3-
import java.util.concurrent.ExecutionException;
4-
import java.util.concurrent.ExecutorService;
5-
import java.util.concurrent.Executors;
6-
import java.util.concurrent.Future;
3+
import java.util.concurrent.*;
4+
75
import org.junit.jupiter.api.*;
86
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
98

109
class CallableExampleTest {
10+
1111
@Test
12-
void test() throws ExecutionException, InterruptedException {
12+
void testCallable() throws ExecutionException, InterruptedException {
13+
class ExampleCallable implements Callable<Integer> {
14+
15+
@Override
16+
public Integer call() throws Exception {
17+
int sum = 0;
18+
for (int i = 1; i <= 100; ++i) {
19+
sum += i;
20+
}
21+
return sum;
22+
}
23+
}
24+
ExampleCallable exampleCallable = new ExampleCallable();
25+
FutureTask<Integer> futureTask = new FutureTask<>(exampleCallable);
26+
new Thread(futureTask).start();
27+
assertEquals(5050, futureTask.get());
28+
}
29+
30+
@Test
31+
void testThreadPool() throws ExecutionException, InterruptedException {
32+
class ExampleCallable implements Callable<Integer> {
33+
34+
@Override
35+
public Integer call() throws Exception {
36+
int sum = 0;
37+
for (int i = 1; i <= 100; ++i) {
38+
sum += i;
39+
}
40+
return sum;
41+
}
42+
}
1343
ExecutorService executorService = Executors.newFixedThreadPool(1);
14-
Future<Integer> result = executorService.submit(new CallableExample());
44+
Future<Integer> result = executorService.submit(new ExampleCallable());
1545
int callableSum = result.get();
1646
Assertions.assertEquals(5050, callableSum);
1747
executorService.shutdown();
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
package com.examplehub.basics.thread;
22

3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
38
class DaemonThreadTest {
4-
// @Test
9+
@Test
510
void test() {
6-
Thread thread = new Thread(new DaemonThread());
11+
class ExampleThread extends Thread {
12+
@Override
13+
public void run() {
14+
assertTrue(Thread.currentThread().isDaemon());
15+
while (true) {
16+
}
17+
}
18+
}
19+
ExampleThread thread = new ExampleThread();
720
thread.setDaemon(true);
821
thread.start();
9-
for (int i = 0; i < 10; i++) {
10-
System.out.println("main thread");
22+
23+
int sum = 0;
24+
for (int i = 1; i <= 100; ++i) {
25+
sum = sum + i;
26+
assertTrue(thread.isAlive());
27+
assertTrue(thread.isDaemon());
1128
}
29+
assertEquals(5050, sum);
1230
}
1331
}

‎src/test/java/com/examplehub/basics/thread/ExampleRunnableTest.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@
66

77
class ExampleRunnableTest {
88
@Test
9-
void testRunnable() {
10-
Thread thread = new Thread(new ExampleRunnable());
11-
thread.setName("runnable-thread");
12-
thread.start();
13-
for (int i = 0; i < 10; ++i) {
14-
System.out.println(Thread.currentThread().getName() + "->" + i);
9+
void testRunnable() throws InterruptedException {
10+
class ExampleRunnable implements Runnable {
11+
private int sum = 0;
12+
@Override
13+
public void run() {
14+
for (int i = 1; i <= 100; ++i) {
15+
sum = sum + i;
16+
}
17+
}
18+
public int getSum() {
19+
return sum;
20+
}
1521
}
22+
ExampleRunnable exampleRunnable = new ExampleRunnable();
23+
Thread thread = new Thread(exampleRunnable);
24+
thread.start();
25+
thread.join();
26+
assertEquals(5050, exampleRunnable.getSum());
27+
}
28+
29+
@Test
30+
void testAnonymous() {
31+
new Thread(() -> System.out.println("testAnonymous")).start();
1632
}
1733
}

‎src/test/java/com/examplehub/basics/thread/ExampleThreadTest.java

+89-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,99 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import org.junit.jupiter.api.Disabled;
56
import org.junit.jupiter.api.Test;
67

78
class ExampleThreadTest {
89
@Test
9-
void test() {
10-
ExampleThread exampleThread = new ExampleThread();
11-
exampleThread.setName("custom-thread");
12-
exampleThread.start();
13-
for (int i = 0; i < 10; ++i) {
14-
System.out.println(Thread.currentThread().getName() + "->" + i);
10+
void test() throws Exception{
11+
class ExampleThread extends Thread {
12+
private int sum = 0;
13+
14+
public int getSum() {
15+
return sum;
16+
}
17+
18+
@Override
19+
public void run() {
20+
for (int i = 1; i <= 100; ++i) {
21+
sum += i;
22+
}
23+
}
1524
}
25+
ExampleThread thread = new ExampleThread();
26+
thread.start();
27+
thread.join(); // main thread wait unit sub thread completed
28+
assertEquals(5050, thread.getSum());
29+
}
30+
31+
@Test
32+
void testAnonymous() {
33+
new Thread(() -> System.out.println("testAnonymous")).start();
34+
}
35+
36+
@Test
37+
@Disabled //TODO build fail on gradle platform
38+
void testGetThreadId() {
39+
new Thread(()->assertTrue(Thread.currentThread().getId() > 1)).start();
40+
assertEquals(1, Thread.currentThread().getId());
41+
}
42+
43+
@Test
44+
@Disabled //TODO build fail on gradle platform
45+
void testGetThreadName() {
46+
class ExampleThread extends Thread {
47+
public ExampleThread(String name) {
48+
super(name);
49+
}
50+
51+
@Override
52+
public void run() {
53+
assertEquals("sub-thread", getName());
54+
}
55+
}
56+
new ExampleThread("sub-thread").start();
57+
new Thread(() -> assertEquals("second-sub-thread", Thread.currentThread().getName()), "second-sub-thread").start();
58+
assertEquals("main", Thread.currentThread().getName());
59+
}
60+
61+
@Test
62+
void testSumOddEven() throws InterruptedException {
63+
class OddSumThread extends Thread {
64+
private int sum = 0;
65+
66+
public int getSum() {
67+
return sum;
68+
}
69+
70+
@Override
71+
public void run() {
72+
for (int i = 1; i <= 99; i += 2) {
73+
sum += i;
74+
}
75+
}
76+
}
77+
class EvenSumThread extends Thread {
78+
private int sum = 0;
79+
80+
public int getSum() {
81+
return sum;
82+
}
83+
84+
@Override
85+
public void run() {
86+
for (int i = 2; i <= 100; i += 2) {
87+
sum += i;
88+
}
89+
}
90+
}
91+
92+
OddSumThread oddSumThread = new OddSumThread();
93+
EvenSumThread evenSumThread = new EvenSumThread();
94+
oddSumThread.start();
95+
evenSumThread.start();
96+
oddSumThread.join();
97+
evenSumThread.join();
98+
assertEquals(5050, oddSumThread.getSum() + evenSumThread.getSum());
1699
}
17100
}

0 commit comments

Comments
 (0)