|
1 | 1 | package testmanually; |
| 2 | + |
2 | 3 | import org.junit.Test; |
3 | 4 | import org.springframework.core.task.SimpleAsyncTaskExecutor; |
4 | 5 | import org.springframework.core.task.TaskExecutor; |
|
8 | 9 |
|
9 | 10 | public class ThreadTester { |
10 | 11 |
|
11 | | - static boolean showError = false; |
12 | | - |
13 | | - @Test |
14 | | - public void threadSafeTest() throws Exception { |
15 | | - String json1 = "{ \"a\":\"aaa\" }"; |
16 | | - String json2 = "{ \"a\":\"bbb\" }"; |
17 | | - Expressions exp = Expressions.parse("a"); |
18 | | - TaskExecutor theExecutor = new SimpleAsyncTaskExecutor(); |
19 | | - if (showError) { |
20 | | - theExecutor.execute(new ThreadTest(exp, new ObjectMapper().readTree(json1))); |
21 | | - theExecutor.execute(new ThreadTest(exp, new ObjectMapper().readTree(json2))); |
22 | | - } else { |
23 | | - theExecutor.execute(new ThreadTest(Expressions.parse("a"), new ObjectMapper().readTree(json1))); |
24 | | - theExecutor.execute(new ThreadTest(Expressions.parse("a"), new ObjectMapper().readTree(json2))); |
25 | | - |
26 | | - } |
27 | | - |
28 | | - while (true) { |
29 | | - Thread.sleep(1000); |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - public class ThreadTest implements Runnable { |
34 | | - |
35 | | - JsonNode data; |
36 | | - Expressions exp; |
37 | | - |
38 | | - public ThreadTest(Expressions exp, JsonNode data) { |
39 | | - this.data = data; |
40 | | - this.exp = exp; |
41 | | - } |
42 | | - |
43 | | - @Override |
44 | | - public void run() { |
45 | | - while (true) { |
46 | | - try { |
47 | | - JsonNode result = exp.evaluate(data); |
48 | | - if (result != null) { |
49 | | - if (!this.data.get("a").asText().equals(result.asText())) { |
50 | | - System.out.println(String.format("\n%s %s", this.data.get("a").asText(), result.asText())); |
51 | | - } |
52 | | - } else { |
53 | | - System.out.println("Got null result from " + exp.hashCode() + " data: " + data); |
54 | | - } |
55 | | - } catch (Exception ex) { |
56 | | - // System.out.println(ex); |
57 | | - ex.printStackTrace(); |
58 | | - } |
59 | | - } |
60 | | - } |
61 | | - } |
| 12 | + static boolean showError = false; |
| 13 | + |
| 14 | + @Test |
| 15 | + public void threadSafeTest() throws Exception { |
| 16 | + String json1 = "{ \"a\":\"aaa\" }"; |
| 17 | + String json2 = "{ \"a\":\"bbb\" }"; |
| 18 | + Expressions exp = Expressions.parse("a"); |
| 19 | + TaskExecutor theExecutor = new SimpleAsyncTaskExecutor(); |
| 20 | + try { |
| 21 | + if (showError) { |
| 22 | + theExecutor.execute(new ThreadTest(exp, new ObjectMapper().readTree(json1))); |
| 23 | + theExecutor.execute(new ThreadTest(exp, new ObjectMapper().readTree(json2))); |
| 24 | + } else { |
| 25 | + theExecutor.execute(new ThreadTest(Expressions.parse("a"), new ObjectMapper().readTree(json1))); |
| 26 | + theExecutor.execute(new ThreadTest(Expressions.parse("a"), new ObjectMapper().readTree(json2))); |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + while (true) { |
| 31 | + Thread.sleep(1000); |
| 32 | + } |
| 33 | + } finally { |
| 34 | + ((SimpleAsyncTaskExecutor) theExecutor).close(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public class ThreadTest implements Runnable { |
| 39 | + |
| 40 | + JsonNode data; |
| 41 | + Expressions exp; |
| 42 | + |
| 43 | + public ThreadTest(Expressions exp, JsonNode data) { |
| 44 | + this.data = data; |
| 45 | + this.exp = exp; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void run() { |
| 50 | + while (true) { |
| 51 | + try { |
| 52 | + JsonNode result = exp.evaluate(data); |
| 53 | + if (result != null) { |
| 54 | + if (!this.data.get("a").asText().equals(result.asText())) { |
| 55 | + System.out.println(String.format("\n%s %s", this.data.get("a").asText(), result.asText())); |
| 56 | + } |
| 57 | + } else { |
| 58 | + System.out.println("Got null result from " + exp.hashCode() + " data: " + data); |
| 59 | + } |
| 60 | + } catch (Exception ex) { |
| 61 | + // System.out.println(ex); |
| 62 | + ex.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
62 | 67 | } |
0 commit comments