|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.*;
|
4 | 4 |
|
| 5 | +import org.junit.jupiter.api.Disabled; |
5 | 6 | import org.junit.jupiter.api.Test;
|
6 | 7 |
|
7 | 8 | class ExampleThreadTest {
|
8 | 9 | @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 | + } |
15 | 24 | }
|
| 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()); |
16 | 99 | }
|
17 | 100 | }
|
0 commit comments