]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/test/Test.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / test / Test.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.threads.test;\r
17 \r
18 import java.util.Random;\r
19 \r
20 import org.simantics.utils.threads.ThreadUtils;\r
21 import org.simantics.utils.threads.WorkerThread;\r
22 \r
23 \r
24 public class Test {\r
25 \r
26         /**\r
27          * @param args\r
28          */\r
29         public static void main(String[] args) {\r
30                 final WorkerThread list[] = new WorkerThread[10];\r
31                 for (int i =0; i<list.length; i++)\r
32                         list[i] = new WorkerThread();\r
33                 \r
34                 for (WorkerThread t : list)\r
35                         t.start();\r
36                 \r
37                 final Random r = new Random();\r
38                 \r
39                 ExampleObject obj = new ExampleObject();\r
40                 \r
41                 ExampleListener l = new ExampleListener() {\r
42                         @Override\r
43                         public void onMessage(ExampleObject sender, String msg) {\r
44                                 try {\r
45                                         Thread.sleep(1000);\r
46                                 } catch (InterruptedException e) {}\r
47                                 System.out.println(Thread.currentThread()+": "+msg);\r
48                                 ThreadUtils.syncExec(\r
49                                                 list[r.nextInt(list.length)],\r
50                                                 new Runnable() {\r
51                                                         @Override\r
52                                                         public void run() {\r
53                                                                 try {\r
54                                                                         Thread.sleep(1000);\r
55                                                                 } catch (InterruptedException e) {}\r
56                                                                 System.out.println("blaa");\r
57                                                                 try {\r
58                                                                         Thread.sleep(1000);\r
59                                                                 } catch (InterruptedException e) {}\r
60                                                         }});\r
61                                 try {\r
62                                         Thread.sleep(1000);\r
63                                 } catch (InterruptedException e) {}\r
64                         }};\r
65                 \r
66                 for (WorkerThread t : list)                                             \r
67                         obj.addListener(t, l);\r
68                 \r
69                 obj.fireMessage("Test");\r
70                 System.out.println("jeps");\r
71 \r
72                 for (WorkerThread t : list)\r
73                         t.stopDispatchingEvents(true);\r
74         }\r
75 \r
76 }\r