def synchronized(obj): """ Creates a resource lock for a Java object. Produces a monitor object. During the lifespan of the monitor the Java will not be able to acquire a thread lock on the object. This will prevent multiple threads from modifying a shared resource. This should always be used as part of a Python ``with`` startment. Arguments: obj: A valid Java object shared by multiple threads. Example: .. code-block:: python with synchronized(obj): # modify obj values # lock is freed when with block ends """ try: return _jpype.PyJPMonitor(obj.__javavalue__) except AttributeError as ex: pass raise TypeError("synchronized only applies to java objects")
def testMonitorOnNull(self): value = jpype.JObject(None) with self.assertRaises(TypeError): _jpype.PyJPMonitor(value.__javavalue__)
def testMonitorStr(self): obj = jpype.java.lang.Object() monitor = _jpype.PyJPMonitor(obj.__javavalue__) self.assertIsInstance(str(monitor), str)
def testMonitorInitBad2(self): with self.assertRaises(TypeError): _jpype.PyJPMonitor(None)
def testMonitorOnPrim(self): value = jpype.JInt(1) with self.assertRaises(TypeError): _jpype.PyJPMonitor(value.__javavalue__)
def testMonitorOnString(self): value = jpype.JString("foo") with self.assertRaises(TypeError): _jpype.PyJPMonitor(value.__javavalue__)