« Return to Thread: Jython concurrency in the PythonInterpreter class

Re: Jython concurrency in the PythonInterpreter class

by Charlie Groves :: Rate this Message:

Reply to Author | View in Thread

Hi Ted,

On Wed, Jul 8, 2009 at 1:32 PM, Ted Larson
Freeman<freeman@...> wrote:
> In my first attempt, using threads, "test.py" included code
> like this:
>
> from threading import Thread
> Thread(JavaClassA.methodA()).start()
> Thread(JavaClassB.methodB()).start()

Check out the docs for the Thread object:
http://docs.python.org/library/threading.html#thread-objects  The
first argument is the thread group, so you're setting the thread group
for your threads with the results of methodA and methodB.  Since
you're calling methodA and methodB to create arguments for Thread,
they're run in the main thread.

What you want to do is set the target of the Thread to the function or
method you want invoked on the new thread, and then call start.  For
example, the code below calls add on an ArrayList on a separate
thread:

Jython 2.5.0+ (trunk:6503:6505M, Jul 8 2009, 18:36:14)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_13
Type "help", "copyright", "credits" or "license" for more information.
>>> from threading import Thread
>>> from java.util import ArrayList
>>> l = ArrayList()
>>> Thread(target=l.add, args=(5,)).start()
>>> l
[5]

The args keyword arg is a tuple of positional arguments to pass to the
target method.

With your examples, you'd want to do
Thread(target=JavaClassA.methodA).start() and
Thread(target=JavaClassB.methodB).start().

Hope this helps,
Charlie

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

 « Return to Thread: Jython concurrency in the PythonInterpreter class