# Misunderstanding of jruby threads?

**URL:** <https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908>\
**Category:** JRuby\
**Created:** [October 14, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908 "2008-10-14T21:21:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/1 "2008-10-14T21:21:16Z")

</div>

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:in`initialize’

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
```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/2 "2008-10-14T21:54:48Z")

</div>

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](mailto: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](http://xircles.codehaus.org/manage_email)

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/3 "2008-10-14T22:34:42Z")

</div>

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](mailto: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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/4 "2008-10-14T23:13:24Z")

</div>

> 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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/5 "2008-10-14T22:41:45Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> > From: Maurício Linhares [removed\_email\_address@domain.invalid](mailto: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](http://xircles.codehaus.org/manage_email)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/6 "2008-10-14T23:26:53Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> To unsubscribe from this list, please visit:
> 
> [http://xircles.codehaus.org/manage\_email](http://xircles.codehaus.org/manage_email)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 12:31am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/7 "2008-10-15T00:31:27Z")

</div>

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](mailto: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](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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 1:59am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/8 "2008-10-15T01:59:13Z")

</div>

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:in `initialize’

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](mailto:removed_email_address@domain.invalid) wrote:

> [http://jira.codehaus.org/browse/JRUBY-3057](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](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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 2:22am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/9 "2008-10-15T02:22:38Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 12:48am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/10 "2008-10-15T00:48:25Z")

</div>

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](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](mailto:removed_email_address@domain.invalid) wrote:

> > From: Matthew C. [removed\_email\_address@domain.invalid](mailto: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](http://xircles.codehaus.org/manage_email)

–  
Blog: [http://www.bloglines.com/blog/ThomasEEnebo](http://www.bloglines.com/blog/ThomasEEnebo)  
Email: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) , [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 6:48am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/11 "2008-10-15T06:48:12Z")

</div>

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:in `initialize’  
> jruby t1.rb  
> null:1:in `const_missing': uninitialized constant JRuby::PathHelper (NameError) from <script>:1 from :1:in `initialize’  
> 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](mailto: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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 8:54am UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/12 "2008-10-15T08:54:00Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 15, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/misunderstanding-of-jruby-threads/150908/13 "2008-10-15T21:04:25Z")

</div>

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](mailto: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

```
