From: rtrimana Date: Tue, 1 Nov 2016 21:55:14 +0000 (-0700) Subject: Adding lock/synchronization to make sure that RMI calls are thread safe X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a6a87a931041fdbdf1a9f0f85ea2059714ecdbed;p=iot2.git Adding lock/synchronization to make sure that RMI calls are thread safe --- diff --git a/iotjava/iotrmi/Java/IoTRMICall.java b/iotjava/iotrmi/Java/IoTRMICall.java index 8bd86c9..2ec265c 100644 --- a/iotjava/iotrmi/Java/IoTRMICall.java +++ b/iotjava/iotrmi/Java/IoTRMICall.java @@ -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 diff --git a/iotjava/iotrmi/Java/sample/TestClass.java b/iotjava/iotrmi/Java/sample/TestClass.java index 07a1679..f23ffd0 100644 --- a/iotjava/iotrmi/Java/sample/TestClass.java +++ b/iotjava/iotrmi/Java/sample/TestClass.java @@ -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; }