01: package org.mvel.tests.special;
02:
03: import junit.framework.TestCase;
04:
05: import java.util.Map;
06: import java.util.HashMap;
07: import java.util.LinkedList;
08: import java.util.List;
09:
10: import org.mvel.TemplateInterpreter;
11:
12: public class ThreadSafetyTests extends TestCase {
13: private static final int TOTAL_THREADS = 5;
14: private static final int RUNCOUNT = 1000;
15:
16: private Thread[] threads;
17:
18: public void testMultiIteratorMultiThread() {
19: threads = new Thread[TOTAL_THREADS];
20: for (int i = 0; i < TOTAL_THREADS; i++) {
21: threads[i] = new Thread(new TestRunner(this , i));
22: }
23: for (int i = 0; i < TOTAL_THREADS; i++) {
24: threads[i].setPriority(Thread.MIN_PRIORITY);
25: threads[i].start();
26: }
27:
28: while (areThreadsActive()) {
29: try {
30: Thread.sleep(100);
31: } catch (InterruptedException e) {
32: return;
33: }
34: }
35:
36: System.out.println("Done.");
37:
38: }
39:
40: private boolean areThreadsActive() {
41: for (int i = 0; i < TOTAL_THREADS; i++) {
42: if (threads[i].isAlive())
43: return true;
44: }
45: return false;
46: }
47:
48: public static class TestRunner implements Runnable {
49: private ThreadSafetyTests tst;
50: private int threadNumber;
51:
52: public TestRunner(ThreadSafetyTests tst, int threadNumber) {
53: this .tst = tst;
54: this .threadNumber = threadNumber;
55: }
56:
57: public void run() {
58: System.out.println("Thread " + threadNumber + " started.");
59:
60: for (int i = 0; i < RUNCOUNT; i++) {
61: tst.testMultiIterator();
62: }
63:
64: System.out.println("Thread " + threadNumber + " ended.");
65: }
66:
67: }
68:
69: public void testMultiIterator() {
70: Map m = new HashMap();
71:
72: List x = new LinkedList();
73: x.add("foo");
74: x.add("bar");
75:
76: List y = new LinkedList();
77: y.add("FOO");
78: y.add("BAR");
79:
80: m.put("x", x);
81: m.put("y", y);
82:
83: TemplateInterpreter
84: .eval(
85: "@foreach{x as item1, y as item2}X:@{item1};Y:@{item2}@end{}",
86: m);
87:
88: }
89: }
|