KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jepexamples > ThreadTest


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 package org.nfunk.jepexamples;
11
12 /**
13  * This class tests the thread safety of the JEP package. 1000 threads
14  * are started, and each one invokes the evaluate method. The evaluate method
15  * creates 10 JEP instances.
16  * <p>
17  * Thanks to Matthew Baird and Daniel Teng for this code.
18  */

19 public class ThreadTest {
20     
21     static long time = 0;
22     
23     /**
24      * Start 1000 threads.
25      */

26     public static void main(String JavaDoc[] args) {
27         ThreadTest test = new ThreadTest();
28         
29         for (int i = 0; i < 1000; i++) {
30             ThreadTestThread t = new ThreadTestThread(test);
31             t.start();
32         }
33     }
34     
35     public ThreadTest() {
36     }
37
38     /**
39      * Perform a simple evaluation using a new JEP instance. This method
40      * is called by all ThreadTestThreads at very much the same time.
41      */

42     public void evaluate() {
43         for (int i = 0; i < 10; i++) {
44             org.nfunk.jep.JEP myParser = new org.nfunk.jep.JEP();
45             String JavaDoc fooValue = null;
46             Math.random();
47
48             if (Math.random()> 0.5) {
49                 fooValue="NLS";
50             } else {
51                 fooValue="NLT";
52             }
53
54             myParser.addVariable("foo",fooValue);
55             myParser.parseExpression("foo==\""+fooValue + "\"");
56
57             if (myParser.getValue() < 1.0)
58                 System.out.println("ERROR");
59         }
60     }
61 }
62
Popular Tags