[jruby-user] Call a method in a thread

7 views
Skip to first unread message

Robin McKay

unread,
Jan 25, 2013, 7:29:07 AM1/25/13
to us...@jruby.codehaus.org
I guess it's possible to run an entire JRuby program in a thread (let's
call it thread B) if the thread "requires" a .rb file that contains the
program with its classes and methods...

Is it possible from outside the thread (i.e from thread A) to call one
of the methods in thread B? And if so, how?

...R

--
Posted via http://www.ruby-forum.com/.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Keith Bennett

unread,
Jan 25, 2013, 10:50:47 AM1/25/13
to us...@jruby.codehaus.org
Robin -

Basically, any code can be run in any thread (though the wisdom of doing so might be questionable). There's no need for a separate file. In case it's helpful, slide #28 of my JRuby slideshow at https://speakerdeck.com/keithrbennett/jruby-the-synergy-of-ruby-and-the-jvm shows an example of threading that has a block specified inline in the call to Thread.new.

One way of having a different thread execute a function is to create a queue from which the worker thread receives "job assignments". The dispatching thread posts a message to that queue that communicates what it is that needs to be done, and any data it may need. When the worker thread gets around to reading that message (there may be other messages posted before it), it executes the requested job.

If this doesn't help, can you provide a specific example of when you'd want to do this?

- Keith

---
Keith R. Bennett
http://about.me/keithrbennett
Work Status: Available for Consulting, Direct Employment

Robin McKay

unread,
Jan 26, 2013, 4:07:28 AM1/26/13
to us...@jruby.codehaus.org
Thanks Keith,
Very interesting presentation which I've bookmarked. May I make a few
comments in another Topic?

===anyway, back to this Topic=======

I guess I wasn't very clear in my opening post.

Suppose I have a file called prog1.rb containing (inter alia)

...
def showStuff(stuff)
puts stuff
end
...

I would like to be able to run

tt = Thread.new do
require 'prog1.rb'
end

And then in another thread I would like to be able to do something which
is the same as showStuff("This is a test").

I am thinking of a concept a bit like the way ScriptingContainer can
call methods in a script - but all entirely in JRuby.


...R


>Keith B. wrote in post #1093771:
> Robin -
>
> Basically, any code can be run in any thread (though the wisdom of doing
> so might be questionable). There's no need for a separate file.
> - Keith

cootcraig

unread,
Jan 26, 2013, 11:38:59 AM1/26/13
to jruby...@googlegroups.com, us...@jruby.codehaus.org, li...@ruby-forum.com


On Friday, January 25, 2013 5:29:07 AM UTC-7, Robin McKay wrote:
I guess it's possible to run an entire JRuby program in a thread (let's
call it thread B) if the thread "requires" a .rb file that contains the
program with its classes and methods...

Is it possible from outside the thread (i.e from thread A) to call one
of the methods in thread B? And if so, how?


Have you looked at this thread library?
https://github.com/celluloid/celluloid/wiki/Basic-usage
 

Keith Bennett

unread,
Jan 26, 2013, 4:18:52 PM1/26/13
to us...@jruby.codehaus.org
Robin -


---
Keith R. Bennett
http://about.me/keithrbennett

On Jan 26, 2013, at 4:07 AM, Robin McKay <li...@ruby-forum.com> wrote:

> Thanks Keith,
> Very interesting presentation which I've bookmarked.

Thanks. If you'd like the audio of the presentation, the audio and slideshow are both at http://www.bbs-software.com/blog/2012/09/04/jruby-presentation-northern-virginia-ruby-user-group/. Nothing special, but there might be some useful pieces of information. More below...


> May I make a few
> comments in another Topic?
>
> ===anyway, back to this Topic=======
>
> I guess I wasn't very clear in my opening post.
>
> Suppose I have a file called prog1.rb containing (inter alia)
>
> ...
> def showStuff(stuff)
> puts stuff
> end
> ...
>
> I would like to be able to run
>
> tt = Thread.new do
> require 'prog1.rb'
> end
>
> And then in another thread I would like to be able to do something which
> is the same as showStuff("This is a test").
>
> I am thinking of a concept a bit like the way ScriptingContainer can
> call methods in a script - but all entirely in JRuby.
>

It sounds like what you're really looking to do is eval or load, not require, right? Require will load the file once per Ruby VM lifetime, so subsequent uses would have no effect.

As I understand it, you want to evaluate an arbitrary chunk of code, and do it in its own thread. You could do that, but doing that in threads as you describe is not as effective in isolating the jobs from each other as would be running each in a ScriptingContainer. Any classes you load (or, even worse, modify) would be shared by all threads. And there could be variables that would be shared as well (class variables, static variables, etc.).

For more protection, you'd want each job to have its own Ruby virtual machine, not just its own thread. If, on the other hand, you had control over the types of jobs that were requested, sharing the Ruby VM might not be a bad thing.

- Keith

Roger Pack

unread,
Jan 26, 2013, 7:47:49 PM1/26/13
to jruby...@googlegroups.com
On 1/26/13, cootcraig <cr...@coot.net> wrote:
>
>
> On Friday, January 25, 2013 5:29:07 AM UTC-7, Robin McKay wrote:
>>
>> I guess it's possible to run an entire JRuby program in a thread (let's
>> call it thread B) if the thread "requires" a .rb file that contains the
>> program with its classes and methods...
>>
>> Is it possible from outside the thread (i.e from thread A) to call one
>> of the methods in thread B? And if so, how?

Are you talking a separate jruby thread, or separate java thread?

Robin McKay

unread,
Jan 27, 2013, 3:58:00 AM1/27/13
to us...@jruby.codehaus.org
Thanks Keith,
It sounds like what I thought of doing is not practical or more trouble
than it's worth.

...R

>Keith B. wrote in post #1093911:
> Robin -
>
> as would be running each in a ScriptingContainer. Any classes you load
> (or, even worse, modify) would be shared by all threads. And there
> could be variables that would be shared as well (class variables, static
> variables, etc.).

Reply all
Reply to author
Forward
0 new messages