예제 #1
0
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")
예제 #2
0
 def testMonitorOnNull(self):
     value = jpype.JObject(None)
     with self.assertRaises(TypeError):
         _jpype.PyJPMonitor(value.__javavalue__)
예제 #3
0
 def testMonitorStr(self):
     obj = jpype.java.lang.Object()
     monitor = _jpype.PyJPMonitor(obj.__javavalue__)
     self.assertIsInstance(str(monitor), str)
예제 #4
0
 def testMonitorInitBad2(self):
     with self.assertRaises(TypeError):
         _jpype.PyJPMonitor(None)
예제 #5
0
 def testMonitorOnPrim(self):
     value = jpype.JInt(1)
     with self.assertRaises(TypeError):
         _jpype.PyJPMonitor(value.__javavalue__)
예제 #6
0
 def testMonitorOnString(self):
     value = jpype.JString("foo")
     with self.assertRaises(TypeError):
         _jpype.PyJPMonitor(value.__javavalue__)