Adding lock/synchronization to make sure that RMI calls are thread safe
[iot2.git] / iotjava / iotrmi / Java / sample / TestClass.java
index 07a167987a43bbd48b5c56946621647040ae34f6..f23ffd05ff7a585a50e4cd600f5dfcc9552b1818 100644 (file)
@@ -103,13 +103,45 @@ public class TestClass implements TestClassInterface {
 
        public int callBack() {
 
-               int sum = 0;
+               /*int sum = 0;
                for (CallBackInterface cb : cblist) {
                        sum = sum + cb.printInt();
                }
-               //sum = cblist.get(1).printInt();
-
-               return sum;
+               */
+               final CallBackInterface cb1 = cblist.get(1);
+               final CallBackInterface cb2 = cblist.get(2);
+
+               Thread thread1 = new Thread() {
+                       public void run() {
+                   try{
+                                       for(int i = 0; i < 10; i++) {
+                                               cb1.printInt();
+                                               Thread.sleep(1000);
+                                       }
+                               } catch (Exception ex){
+                                       ex.printStackTrace();
+                                       throw new Error("Error running thread!");
+                   }
+               }
+           };
+               thread1.start();
+
+               Thread thread2 = new Thread() {
+                       public void run() {
+                   try{
+                                       for(int i = 0; i < 10; i++) {
+                                               cb2.printInt();
+                                               Thread.sleep(1000);
+                                       }
+                               } catch (Exception ex){
+                                       ex.printStackTrace();
+                                       throw new Error("Error running thread!");
+                   }
+               }
+           };
+               thread2.start();
+
+               return 1;
        }