1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.simple.multiThreadedExecution;
26
27 import java.io.PrintStream;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40 public class MultithereadedExecutionTest {
41
42 private static final int THREAD_COUNT = 2;
43 private final Thread[] threads = new Thread[THREAD_COUNT];
44
45 private static final long TEST_DURATION_IN_MILLIS = 100;
46
47 private final PrintStream oldOut = System.out;
48 StateCheckingPrintStream scps = new StateCheckingPrintStream(oldOut);
49
50 volatile boolean signal = false;
51
52 @Before
53 public void setup() {
54 System.setErr(scps);
55
56
57 }
58
59 @After
60 public void tearDown() throws Exception {
61
62
63 System.setErr(oldOut);
64 }
65
66 @Test
67 public void test() throws Throwable {
68 WithException withException = new WithException();
69 Other other = new Other();
70 threads[0] = new Thread(withException);
71 threads[1] = new Thread(other);
72 threads[0].start();
73 threads[1].start();
74 Thread.sleep(TEST_DURATION_IN_MILLIS);
75 signal = true;
76 threads[0].join();
77 threads[1].join();
78
79 if (withException.throwable != null) {
80 throw withException.throwable;
81 }
82
83 if (other.throwable != null) {
84 throw other.throwable;
85 }
86
87 }
88
89 class WithException implements Runnable {
90
91 volatile Throwable throwable;
92 Logger logger = LoggerFactory.getLogger(WithException.class);
93
94 @Override
95 public void run() {
96 int i = 0;
97
98 while (!signal) {
99 try {
100 logger.info("Hello {}", i, new Throwable("i=" + i));
101 i++;
102 } catch (Throwable t) {
103 throwable = t;
104 MultithereadedExecutionTest.this.signal = true;
105 return;
106 }
107 }
108
109 }
110 }
111
112 class Other implements Runnable {
113 volatile Throwable throwable;
114 Logger logger = LoggerFactory.getLogger(Other.class);
115
116 @Override
117 public void run() {
118 int i = 0;
119 while (!signal) {
120 try {
121 logger.info("Other {}", i++);
122 } catch (Throwable t) {
123 throwable = t;
124 MultithereadedExecutionTest.this.signal = true;
125 return;
126 }
127 }
128 }
129 }
130
131 }