Forum: JRuby Atomic and Threadify

Ab7126737dc26c07393d69d7fb6571e9?d=identicon&s=25 Alex Gutteridge (Guest)
on 2013-10-01 13:35
(Received via mailing list)
I'm trying to use the Atomic and Threadify libraries, but I'm either
misunderstanding something somewhere or they don't play nice together.
The output of the one liner below should be 166650, but the Atomic
updates look like they are being randomly missed sometimes:

jruby -rthreadify -ratomic -e
't=Atomic.new(0);(0..100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value'
166727
jruby -rthreadify -ratomic -e
't=Atomic.new(0);(0..100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value'
167145
jruby -rthreadify -ratomic -e
't=Atomic.new(0);(0..100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value'
166814
jruby -rthreadify -ratomic -e
't=Atomic.new(0);(0..100).to_a.threadify(1){|n| n.times{|x|
t.update{|v|v+x}}};p t.value'
166650

Doing it without Threadify also seems to work:

jruby -ratomic -e
't=Atomic.new(0);a=(0..100).to_a;th=[];a.each{|n|th<<Thread.new(n){n.times{|x|t.update{|v|v+x}}}};th.each{|ths|
ths.join};p t.value'
166650

--
Alex Gutteridge
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2013-10-01 14:59
(Received via mailing list)
Seems as thread-ify is not "really" thread-safe.

I would try reporting if you really want to use it ... but it's not
really
battle tested (did not find tests).

Also you can simply use a java.util.atomic.AtomicInteger value if you
only
need to count atomically.

K.
Ab7126737dc26c07393d69d7fb6571e9?d=identicon&s=25 Alex Gutteridge (Guest)
on 2013-10-01 16:28
(Received via mailing list)
Yeah, I always liked Threadify for super-simple threadification, but
it's pretty old now. Using plain old Threads works for me, just wanted
to make sure I wasn't misunderstanding something around Atomic's
concurrency model.

AG

On 01.10.2013 13:58, Karol Bucek wrote:
>  On Tue, Oct 1, 2013 at 1:34 PM, Alex Gutteridge  wrote:
>> t.update{|v|v+x}}};p t.value'
>> 't=Atomic.new(0);(0..100).to_a.threadify(1){|n| n.times{|x|
> Links:
> ------
> [1] http://xircles.codehaus.org/manage_email
> [2] mailto:alexg@ruggedtextile.com

--
Alex Gutteridge
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.