Misunderstanding of jruby threads?


#1

Hello!

I have searched google a bit and did some experiments, however, I am
unable to understand what is at issue with this example code listed.
Basically, I am trying to get some ruby code over to jruby so that I can
take advantage of threads and I anm running into lots of problems. Here
is what I have reduced one problem down to:

threadPool = []

list = %w(aS bS cS dS eS fS gS hS)

for sys in list
threadPool << Thread.new(sys) {|numericalSys|
while 1
res = %x[date]
puts “#{numericalSys}, #{res}\n”
sleep 2
end
}
end

threadPool.each {|eachThread| eachThread.join}

I am using jruby:

jruby --version
jruby 1.1.4 (ruby 1.8.6 patchlevel 114) (2008-08-28 rev 7570)
[sparc-java]

And here is the output of the process:

jruby t1.rb
null:1:in const_missing': uninitialized constant Config::CONFIG (NameError) from <script>:1 from :1:ininitialize’

Under ruby proper I get what I thought should be the correct output:

ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [sparc-solaris2.10]
ruby t1.rb
aS, Tue Oct 14 19:14:01 GMT 2008
bS, Tue Oct 14 19:14:01 GMT 2008
cS, Tue Oct 14 19:14:01 GMT 2008
dS, Tue Oct 14 19:14:01 GMT 2008
eS, Tue Oct 14 19:14:01 GMT 2008
fS, Tue Oct 14 19:14:01 GMT 2008
gS, Tue Oct 14 19:14:01 GMT 2008
hS, Tue Oct 14 19:14:01 GMT 2008
aS, Tue Oct 14 19:14:03 GMT 2008
bS, Tue Oct 14 19:14:03 GMT 2008
dS, Tue Oct 14 19:14:03 GMT 2008
cS, Tue Oct 14 19:14:03 GMT 2008
gS, Tue Oct 14 19:14:03 GMT 2008
fS, Tue Oct 14 19:14:03 GMT 2008
eS, Tue Oct 14 19:14:03 GMT 2008
hS, Tue Oct 14 19:14:03 GMT 2008

Perhaps I am not completely understanding how I need to do this in jruby
so any assistance would be most welcome!

Thanks in advance!
Phy


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#2

I’ve tried this and it has worked:

thread_pool = []

list = %w(aS bS cS dS eS fS gS hS)

for sys in list
t = Thread.new( sys ) do |n|
while true
res = Time.now
puts “#{n}, #{res}\n”
sleep 2
end
end
thread_pool << t
end

thread_pool.each {|th| th.join}

On Tue, Oct 14, 2008 at 4:16 PM, Phy P. removed_email_address@domain.invalid wrote:

while 1
I am using jruby:

gS, Tue Oct 14 19:14:01 GMT 2008

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
João Pessoa, PB, +55 83 8867-7208


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

So the example is to illustrate what I am trying to do within my code.
So as a representation, “%x[date]” in my code is an actual call to an
external application (c in this case). So while Time.now does
illustrate how to achieve the same example application output, it does
not follow the same code logic.

I have also tried using ticks, but that exhibits the same problem as
using %x syntax. If I remove the code segment to outside of a thread,
then it works as well. So there is something happening that I am not
aware of WRT threads and system calls. Can anyone shed some light on
this?

Thanks. Cheers,
Phy

— On Tue, 10/14/08, Maurício Linhares removed_email_address@domain.invalid
wrote:

threads and I anm running into lots of problems. Here is

 puts "#{numericalSys}, #{res}\n"

jruby 1.1.4 (ruby 1.8.6 patchlevel 114) (2008-08-28

eS, Tue Oct 14 19:14:01 GMT 2008
hS, Tue Oct 14 19:14:03 GMT 2008

http://xircles.codehaus.org/manage_email

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#4

across multiple threads without synchronization. Can the VM
itself
internally use multiple java threads?

Hmm, I am not sure I follow. In my code example, I had assumed jruby
would be issuing true (e.g. not green threads) for each new thread
instance. Is this incorrect?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#5

I’ve been confused about threads in JRuby, I couldn’t use the vm
across multiple threads without synchronization. Can the VM itself
internally use multiple java threads?

On Tue, Oct 14, 2008 at 4:24 PM, Phy P. removed_email_address@domain.invalid wrote:

From: Maurício Linhares removed_email_address@domain.invalid
for sys in list
thread_pool.each {|th| th.join}
what I have reduced one problem down to:

 sleep 2

rev 7570) [sparc-java]

Under ruby proper I get what I thought should be the
fS, Tue Oct 14 19:14:01 GMT 2008


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

#6

Well what I was doing was trying to have JRuby process multiple http
requests, and I needed a seperate JRuby instance per one of my java
http threads. I haven’t used it in the other direction (i.e the JRuby
VM creating the threads) I was just curious why it would work in that
direction but not the other.

On Tue, Oct 14, 2008 at 5:01 PM, Phy P. removed_email_address@domain.invalid wrote:

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

#7

Hmmm, that’s a great question. Does JRuby use a GIL? It looks like I
incorrectly assumed that it supported native threads and therefore I had
access to them via thread new. If what you say is true, then the
compelling point of getting real threads is lost. Can anyone confirm
this notion?

Also, it still does not explain why this example code will not run.
Even with ruby proper this runs, yet jruby barfs. Anyone have a
suggestion?

Chris

— On Tue, 10/14/08, Matthew C. removed_email_address@domain.invalid wrote:

VM creating the threads) I was just curious why it would

Hmm, I am not sure I follow. In my code example, I
http://xircles.codehaus.org/manage_email


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

#8

Mr. Enebo,

Thank you for your quick reply. Perhaps I have something wrong still:

threadPool = []

list = %w(aS bS cS dS eS fS gS hS)

for sys in list
threadPool << Thread.new(sys) {|numericalSys|
require ‘rbconfig’
require ‘jruby’
while 1
res = %x[date]
puts “#{numericalSys}, #{res}”
sleep 2
end
}
end

threadPool.each {|eachThread| eachThread.join}

The work around itself is not right, now the modified code will
partially work as in:

dS, Tue Oct 14 23:49:16 GMT 2008
aS, Tue Oct 14 23:49:18 GMT 2008
dS, Tue Oct 14 23:49:19 GMT 2008
aS, Tue Oct 14 23:49:20 GMT 2008
dS, Tue Oct 14 23:49:21 GMT 2008
aS, Tue Oct 14 23:49:22 GMT 2008
dS, Tue Oct 14 23:49:23 GMT 2008
aS, Tue Oct 14 23:49:24 GMT 2008
dS, Tue Oct 14 23:49:25 GMT 2008
aS, Tue Oct 14 23:49:26 GMT 2008
dS, Tue Oct 14 23:49:27 GMT 2008
aS, Tue Oct 14 23:49:28 GMT 2008
dS, Tue Oct 14 23:49:29 GMT 2008
aS, Tue Oct 14 23:49:30 GMT 2008
dS, Tue Oct 14 23:49:31 GMT 2008
aS, Tue Oct 14 23:49:32 GMT 2008
dS, Tue Oct 14 23:49:33 GMT 2008
aS, Tue Oct 14 23:49:34 GMT 2008
dS, Tue Oct 14 23:49:35 GMT 2008
^C>

Notice now how only 2 (aS, sD) numericalSystems are ever run. I have
run it 7 times and I always only get 2 different numericalSystems ever
run.

Other times it will fail to run with this message:

jruby t1.rb
t1.rb:7 warning: already initialized constant CONFIG
t1.rb:7 warning: already initialized constant RbConfig
t1.rb:7 warning: already initialized constant CROSS_COMPILING
t1.rb:7 warning: already initialized constant MAKEFILE_CONFIG
null:1:in const_missing': uninitialized constant JRuby (NameError) from <script>:1 from :1:ininitialize’

Then even more troubling, there is no alternate threads created. I had
assumed I was creating real threads with the above code?

— On Tue, 10/14/08, Thomas E Enebo removed_email_address@domain.invalid wrote:

http://jira.codehaus.org/browse/JRUBY-3057
GIL? It looks like I incorrectly assumed that it supported

Well what I was doing was trying to have JRuby
work in that

Hmm, I am not sure I follow. In my code

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

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#9

Phy P. wrote:

require 'rbconfig'
require 'jruby'
while 1
  res = %x[date]
  puts "#{numericalSys}, #{res}"
  sleep 2
end

}
end

threadPool.each {|eachThread| eachThread.join}

Require them at the top of the script, not in the threads. The bug is
that our %x[…] logic tries to load both of those libraries, but does
it in a thread-unsafe way. By loading them before the threads start up,
you avoid that problem.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#10

We have a bug in our command spawning code. We end up require’ing
several files and those files are being concurrently loaded. I just
created a bug for this:

http://jira.codehaus.org/browse/JRUBY-3057

You can use the workaround I provided in it to move forward until we
fix this…

-Tom

On Tue, Oct 14, 2008 at 5:29 PM, Phy P. removed_email_address@domain.invalid wrote:

From: Matthew C. removed_email_address@domain.invalid
work in that
had assumed jruby would be issuing true (e.g. not green


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#11

Okay. So, with your suggestion I moved the libs up and now I still see
the same behavior, runs once but only displaying 2 data sources and then
won’t run again (I assume because the precompiled stuff is broken?):

cat t1.rb
require ‘rbconfig’
require ‘jruby’

threadPool = []

list = %w(aS bS cS dS eS fS gS hS)

for sys in list
threadPool << Thread.new(sys) {|numericalSys|
while 1
res = %x[date]
puts “#{numericalSys}, #{res}”
sleep 2
end
}
end

threadPool.each {|eachThread| eachThread.join}

jruby t1.rb
gS, Wed Oct 15 04:35:50 GMT 2008
aS, Wed Oct 15 04:35:50 GMT 2008
gS, Wed Oct 15 04:35:52 GMT 2008
aS, Wed Oct 15 04:35:52 GMT 2008
gS, Wed Oct 15 04:35:54 GMT 2008
aS, Wed Oct 15 04:35:54 GMT 2008
gS, Wed Oct 15 04:35:56 GMT 2008
aS, Wed Oct 15 04:35:56 GMT 2008
gS, Wed Oct 15 04:35:58 GMT 2008
aS, Wed Oct 15 04:35:58 GMT 2008
gS, Wed Oct 15 04:36:00 GMT 2008
aS, Wed Oct 15 04:36:00 GMT 2008
^C> jruby t1.rb
null:1:in const_missing': uninitialized constant JRuby::PathHelper (NameError) from <script>:1 from :1:ininitialize’
jruby t1.rb
null:1:in const_missing': uninitialized constant JRuby::PathHelper (NameError) from <script>:1 from :1:ininitialize’
jruby --version
jruby 1.1.4 (ruby 1.8.6 patchlevel 114) (2008-08-28 rev 7570)
[sparc-java]
java -version
java version “1.6.0_07”
Java™ SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot™ Server VM (build 10.0-b23, mixed mode)

So am I still missing something?

— On Tue, 10/14/08, Charles Oliver N. removed_email_address@domain.invalid
wrote:

threadPool = []
sleep 2
it in a thread-unsafe way. By loading them before the
threads start up,
you avoid that problem.

  • 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

#12

Phy P. wrote:

Okay. So, with your suggestion I moved the libs up and now I still see the same behavior, runs once but only displaying 2 data sources and then won’t run again (I assume because the precompiled stuff is broken?):

cat t1.rb
require ‘rbconfig’
require ‘jruby’

Sorry for the trouble with this whole mess…also add

require ‘jruby/path_helper’

And try it again. With that I ran it 10 times without any trouble on a
dual-core machine.

The reason it happened the second time was purely a coincidence; it
should be pretty random whether it fails or not. And just FYI, we do not
dump compiled output to disk for .rb files, so that would have no
effect.

Again, sorry for the trouble…it’s most definitely a bug.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#13

Awesome! That resolved the issue! Fantastic. Thanks much for all the
assistance on this.

WRT the “mess” of this issue, its not a problem at all. I can
completely understand bugs. I am just grateful the community is there
to help out!

On to the next problem!

Thanks again! Cheers,
Phy

— On Wed, 10/15/08, Charles Oliver N. removed_email_address@domain.invalid
wrote:

cat t1.rb

  • 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