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 8bd86c9207eb90a544dc6a193db0641c796e6e4a..2ec265c5409edd171e65eca8809e94c832a45017 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 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;
        }