Skip to content

Instantly share code, notes, and snippets.

@suxue
Created March 11, 2014 04:56
Show Gist options
  • Save suxue/9479712 to your computer and use it in GitHub Desktop.
Save suxue/9479712 to your computer and use it in GitHub Desktop.
simple MT in rhino/nashorn
Mutex = java.util.concurrent.locks.ReentrantLock
Thread = java.lang.Thread
mutex = new Mutex
sum = 0
adder = (c) ->
-> while c > 0
c -= 1
mutex.lock()
sum += 1
mutex.unlock()
newThread = (func, name) ->
new Thread(->
print "thread " + name + " started"
func()
print "thread " + name + " ended"
)
threads = ( newThread(adder(t), "t" + t) for t in [1, 10, 100, 1000, 10000, 100000, 1000000] )
t.start() for t in threads
t.join() for t in threads
print "sum = " + sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment