I am using Rails3 with Puma on JRuby 1.7 with threaded mode ( config.threadsafe!) enabled.
Now as the theory goes: For multiple client requests which come in, a new thread will be used to serve the user rather than a new rails process being started every time.
If I want to find the id of this new request thread, how can I do so?
... finding a "unique" thread id of a currently executing Thread the ruby-way :
Thread.current.object_id
this will work in all rubies however it might not be really "unique" ... see some servers (and puma might do that as well) might re-use "native" threads (e.g. using a fixed thread pool) thus the id might actually be the same for 2 requests executing one after the other.
depending on the implementation details it might be a different ruby Thread (although it's unlikely) object but under JRuby will be the same Java Thread ... you can get the "native" java.lang.Thread id using :
JRuby.reference(Thread.current).native_thread.id
in general if you need something truly unique per request basis you should base it on e.g. the (Rack/Rails) Request object itself which is not reused (recycled) by the application/server.
for this purpose, you will find following methods useful:
Thread.current to gets the of thread which is currently being executed
#<Thread:0x8301ef4 run>
Thread.list to get an array of Thread objects for all threads that are either runnable or stopped.
[
[0] #<Thread:0x8301ef4 run>
]
Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With