Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong
queue_size = 10
queue = ArrayBlockingQueue.new queue_size, true
counts = {"A"=> AtomicLong.new, "B" => AtomicLong.new}
producerA = Thread.new do
while true
queue.put "A"
end
end
producerB = Thread.new do
while true
queue.put "B"
end
end
consumer = Thread.new do
while true
# TODO fix int.times
i = 0
while i < queue_size
x = queue.take
counts[x].incrementAndGet
i += 1
end
Thread.sleep(100)
end
end
[consumer, producerA, producerB].each { |p| p.start }
while true
Thread.sleep(1000)
puts counts
end
[13:26][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah array_blocking_queue_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@7b32aba9:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@7b32aba9:1:1
{A=51, B=49}
{A=101, B=99}
{A=151, B=149}
{A=201, B=199}
{A=251, B=249}
{A=301, B=299}
{A=351, B=349}
{A=391, B=399}
{A=441, B=449}
{A=491, B=499}
{A=541, B=549}
{A=591, B=599}
{A=641, B=649}
{A=691, B=699}
{A=741, B=749}
{A=791, B=789}
{A=841, B=839}
{A=891, B=889}
{A=941, B=939}
{A=991, B=989}
{A=1041, B=1039}
{A=1091, B=1089}
{A=1141, B=1139}
{A=1181, B=1189}
{A=1231, B=1239}
{A=1281, B=1289}
{A=1331, B=1339}
^C[13:27][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah array_blocking_queue_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@496cc009:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@496cc009:1:1
{A=50, B=50}
{A=100, B=100}
{A=150, B=150}
{A=200, B=200}
{A=250, B=250}
{A=300, B=300}
{A=350, B=350}
{A=400, B=400}
{A=450, B=440}
{A=500, B=490}
{A=550, B=540}
{A=600, B=590}
{A=650, B=640}
{A=700, B=690}
{A=750, B=740}
{A=791, B=799}
{A=841, B=839}
{A=889, B=891}
{A=946, B=934}
{A=996, B=984}
{A=1046, B=1034}
{A=1096, B=1084}
^C[13:27][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah array_blocking_queue_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@67af9356:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@67af9356:1:1
{A=50, B=50}
{A=105, B=95}
{A=151, B=149}
{A=201, B=199}
{A=251, B=249}
{A=306, B=294}
{A=356, B=344}
{A=405, B=395}
{A=455, B=445}
{A=505, B=485}
{A=555, B=535}
{A=605, B=585}
{A=655, B=635}
{A=705, B=685}
{A=755, B=735}
{A=799, B=791}
{A=849, B=841}
{A=897, B=883}
{A=947, B=933}
{A=997, B=983}
{A=1050, B=1030}
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong
queue_size = 2
queue = LinkedBlockingQueue.new queue_size
counts = {"A"=> AtomicLong.new, "B" => AtomicLong.new}
producerA = Thread.new do
while true
queue.put "A"
end
end
producerB = Thread.new do
while true
queue.put "B"
end
end
consumer = Thread.new do
while true
# TODO fix int.times
i = 0
while i < queue_size
x = queue.take
counts[x].incrementAndGet
i += 1
end
Thread.sleep(100)
end
end
[consumer, producerA, producerB].each { |p| p.start }
while true
Thread.sleep(1000)
puts counts
end
[12:01][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah lbq_not_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@2b3b488:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@2b3b488:1:1
{A=19, B=1}
{A=39, B=1}
{A=59, B=1}
{A=79, B=1}
{A=99, B=1}
{A=106, B=14}
{A=106, B=34}
{A=106, B=54}
{A=106, B=74}
{A=106, B=94}
{A=106, B=114}
{A=106, B=134}
{A=106, B=154}
{A=106, B=172}
{A=106, B=192}
{A=106, B=212}
{A=106, B=232}
{A=106, B=252}
{A=106, B=272}
{A=106, B=292}
{A=106, B=312}
{A=106, B=332}
{A=106, B=352}
{A=106, B=372}
{A=106, B=392}
{A=106, B=412}
{A=106, B=432}
{A=106, B=450}
{A=106, B=470}
{A=106, B=490}
{A=106, B=510}
{A=106, B=530}
{A=106, B=550}
{A=106, B=570}
{A=124, B=572}
{A=144, B=572}
{A=164, B=572}
{A=184, B=572}
{A=204, B=572}
{A=224, B=572}
{A=244, B=572}
{A=262, B=572}
{A=282, B=572}
{A=302, B=572}
{A=322, B=572}
{A=342, B=572}
{A=350, B=584}
{A=350, B=604}
{A=350, B=624}
{A=350, B=644}
{A=350, B=664}
{A=350, B=684}
{A=362, B=692}
{A=381, B=693}
{A=401, B=693}
{A=419, B=693}
{A=439, B=693}
{A=459, B=693}
{A=479, B=693}
{A=499, B=693}
{A=519, B=693}
{A=539, B=693}
{A=559, B=693}
{A=579, B=693}
{A=599, B=693}
{A=619, B=693}
{A=639, B=693}
{A=659, B=693}
^C[12:03][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah lbq_not_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@85f515:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@85f515:1:1
{A=18, B=2}
{A=38, B=2}
{A=58, B=2}
{A=78, B=2}
{A=98, B=2}
{A=118, B=2}
{A=138, B=2}
{A=140, B=20}
{A=140, B=40}
{A=140, B=60}
{A=140, B=80}
{A=140, B=100}
{A=140, B=120}
{A=141, B=137}
{A=141, B=157}
{A=141, B=177}
{A=142, B=196}
{A=142, B=216}
{A=142, B=236}
{A=142, B=256}
{A=156, B=262}
{A=176, B=262}
{A=196, B=262}
{A=216, B=262}
{A=236, B=262}
{A=246, B=272}
{A=246, B=292}
{A=246, B=310}
{A=246, B=330}
{A=246, B=350}
{A=246, B=370}
{A=246, B=390}
{A=246, B=410}
{A=254, B=422}
^C[12:04][jvm-1.7_25][jruby-1.7.9@mirah_release_0_1_2][nick:~/Desktop]
$ mirah lbq_not_fair.mirah
WARNING: Misaligned end at mirah.lang.ast.StringCodeSource@2b3b488:5:1
NOTE: closes block at mirah.lang.ast.StringCodeSource@2b3b488:1:1
{A=18, B=2}
{A=38, B=2}
{A=58, B=2}
{A=78, B=2}
{A=98, B=2}
{A=118, B=2}
{A=138, B=2}
{A=158, B=2}
{A=178, B=2}
{A=198, B=2}
{A=218, B=2}
{A=238, B=2}
{A=258, B=2}
{A=276, B=2}
{A=296, B=2}
{A=316, B=2}
{A=318, B=20}
{A=318, B=40}
{A=318, B=60}
{A=318, B=80}
{A=318, B=100}
{A=318, B=120}
{A=318, B=140}
{A=318, B=160}
{A=318, B=180}
{A=318, B=200}
{A=318, B=220}
{A=318, B=238}
{A=318, B=258}
{A=318, B=278}
{A=322, B=294}
{A=342, B=294}
{A=362, B=294}
{A=382, B=294}
{A=402, B=294}
{A=422, B=294}
{A=442, B=294}
{A=462, B=294}
{A=482, B=294}
{A=500, B=294}
{A=520, B=294}
{A=540, B=294}
{A=560, B=294}
{A=580, B=294}
{A=600, B=294}
{A=610, B=304}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment