Thanks, Charles. When I was stepping through in debug mode, I notice that there is a RubyThread.adopt method, which seems to adopt a foreign thread.
As I was debugging, I realized that all Ruby threads are daemon (silly me for not realizing this earlier), so I just took my own block->Runnable adoption out, which eliminated the problem. However, this used to work in previous releases of JRuby, so it may be a regression in some way. Peter -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Charles Oliver Nutter Sent: Tuesday, May 22, 2007 9:02 PM To: [email protected] Subject: Re: [jruby-dev] Can blocks be safely run in different Java threads? Peter K Chan wrote: > I am wondering if there is any potential problem with running a Ruby block > in a different native java.lang.Thread? > > My code converts a block into a java.lang.Runnable and starts it directly > using the Java thread constructor. I am not using RubyThread, because I need > to start a daemon thread. > > I am seeing some sporadic NPE in my code. About one-third of the time, when > starting a block in a different (native) Java thread, I see error below (1.0 > RC3 revision 3714): > > Exception in thread "NET2" java.lang.NullPointerException > at org.jruby.runtime.Block.call(Block.java:174) > at org.jruby.RubyProc.call(RubyProc.java:173) > at org.jruby.RubyProc.call(RubyProc.java:148) > at org.jruby.javasupport.Java$1.invoke(Java.java:853) > at $Proxy0.run(Unknown Source) > at java.lang.Thread.run(Thread.java:595) > > Any tip on this would be greatly appreciated. Hmm...well to start, blocks are not generally supposed to be called across threads, but procs can be. Procs/lambdas handle the local scoping and framing a bit differently so that multiple callers don't interfere with each other. I would recommend that if you're going to memoize the block and call it across threads, that you first turn it into a proc or lambda. - Charlie --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email
