by bcoates · 12y ago · 0 comments
n = 0;
avg = 0;
function twothings() {
thing1(function(ret) { ++n; avg += (ret - avg)/n; });
thing2(function(ret) { ++n; avg += (ret - avg)/n; });
}
I want to dispatch two I/O operations to run concurrently and have their completions modify some shared state in a serializable fashion, which can be read at any stage in the process and have a valid result. I shouldn't need to give up on nondeterministic simultaneous execution or faff about with locking mechanisms to get this.In Java, you have access to real threads and can actually run two pieces of code concurrently. This introduces a lot of potential issues, but also allows you to take advantage of multiple cores.
If all you want is non-blocking IO (what node gives you), there are a handful of great libraries that provide that on the JVM.