Adding lock/synchronization to make sure that RMI calls are thread safe
authorrtrimana <rtrimana@uci.edu>
Tue, 1 Nov 2016 21:55:14 +0000 (14:55 -0700)
committerrtrimana <rtrimana@uci.edu>
Tue, 1 Nov 2016 21:55:14 +0000 (14:55 -0700)
iotjava/iotrmi/Java/IoTRMICall.java
iotjava/iotrmi/Java/sample/TestClass.java

index 8bd86c9..2ec265c 100644 (file)
@@ -48,7 +48,7 @@ public class IoTRMICall {
        /**
         * remoteCall() calls a method remotely by passing in parameters and getting a return Object
         */
-       public Object remoteCall(int objectId, String methodSign, Class<?> retType, Class<?> retGenTypeKey, 
+       public synchronized Object remoteCall(int objectId, String methodSign, Class<?> retType, Class<?> retGenTypeKey, 
                        Class<?> retGenTypeVal, Class<?>[] paramCls, Object[] paramObj) {
 
                // Send method info
index 07a1679..f23ffd0 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;
        }