Last active
December 15, 2015 06:29
-
-
Save zerokarmaleft/5216502 to your computer and use it in GitHub Desktop.
Adventures in Functional Ruby, Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'jbundler' | |
require 'stunted' | |
java_import 'clojure.lang.Ref' | |
java_import 'clojure.lang.LockingTransaction' | |
java_import 'java.util.concurrent.Executors' | |
java_import 'java.util.concurrent.Callable' | |
module CoreBridge | |
extend Stunted::Defn | |
def sync(&block) | |
LockingTransaction::run_in_transaction(-> { block.call }) | |
end | |
defn :ref_set, -> ref, val { ref.set val } | |
defn :alter, -> ref, f, *args { ref.alter f, args } | |
end | |
include CoreBridge | |
class Task | |
include Callable | |
def initialize(refs) | |
@refs = refs | |
end | |
def call | |
10000.times do | |
@refs.each do |r| | |
sync { ref_set.(r, r.deref.next) } | |
end | |
end | |
end | |
end | |
refs = (0...10).map { Ref.new(0) } | |
pool = Executors::new_fixed_thread_pool 10 | |
tasks = (0...10).map { Task.new(refs) } | |
futures = pool.invoke_all(tasks) | |
futures.each { |f| f.get } | |
pool.shutdown | |
refs.map &:deref | |
#=> [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment