Concurrency and Multithreading
Author |
Message |
Zren
|
Posted: Sun Aug 12, 2012 7:17 pm Post subject: Concurrency and Multithreading |
|
|
Anybody recommend a certain tutorial / book? Something with plenty of (practical) examples. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
|
|
|
|
mirhagk
|
Posted: Mon Aug 13, 2012 3:41 pm Post subject: RE:Concurrency and Multithreading |
|
|
I know of a good C# tutorial on it, which has some pretty powerful multithreading support, but I don't know of any java ones. In case you can't find anything,here's a really good C# one:
http://www.albahari.com/threading/
It's important you know what race conditions are, what synchronization options are available, and how much your algorithm/program will benefit from multi-threading (hint, it isn't equal to the number of cores you have) before you even start to code any multi-threaded code.
I'd suggest looking for the Java equivalent to stuff like C#'s lock keyword, Tasks, ThreadPool, volatile, Thread.MemoryBarrier, Semaphores, Interlocked, PLINQ, Parrallel.For, Parrallel.Foreach. Basically just find an article like this one for Java[/url] |
|
|
|
|
|
2goto1
|
|
|
|
|
DemonWasp
|
Posted: Mon Aug 13, 2012 9:40 pm Post subject: Re: RE:Concurrency and Multithreading |
|
|
mirhagk @ Mon Aug 13, 2012 3:41 pm wrote: ...Java equivalent to stuff like C#'s lock keyword, Tasks, ThreadPool, volatile, Thread.MemoryBarrier, Semaphores, Interlocked, PLINQ, Parrallel.For, Parrallel.Foreach. Basically just find an article like this one for Java
Roughly speaking,
C# -> Java
lock -> synchronized
System.Threading.Tasks.Task -> java.util.concurrent.FutureTask
ThreadPool -> ThreadPoolExecutor
volatile -> volatile
Thread.MemoryBarrier -> (atomic data types, or synchronized methods)
Semaphore -> Semaphore
Interlocked -> (atomic data types, such as AtomicLong, AtomicReference, etc)
Only two without nearly-exact mappings:
PLINQ -> Java doesn't have anything (built-in) like LINQ, so Parallel LINQ also doesn't exist.
Parallel.For -> No mapping; you can write methods like this yourself if you are so inclined. I imagine Apache probably has some library that does this too.
Really though, you should also learn about blocking and non-blocking data structures and I/O. |
|
|
|
|
|
mirhagk
|
Posted: Tue Aug 14, 2012 8:19 am Post subject: RE:Concurrency and Multithreading |
|
|
@Demonwasp, how does java do atomic data types? Because Thread.MemoryBarrier isn't so much about non-atomic variables as it is about optimizations, caches and stale data to a thread. Calling Thread.MemoryBarrier forces a refresh of the cache, which also prevent optimizations that could be disastrous.
In order to really take advantage of multi-threading you need to know ALL the pitfalls, and potential situations for disaster and different methods to overcome them.
Task based threading is one of my favourites, however Parrallel.For provides an easy way to instantly parrallelize a regular for loop, however you need to know what you're doing, and use non-blocking data structures (in C#, concurrent bag is probably the best) |
|
|
|
|
|
DemonWasp
|
Posted: Tue Aug 14, 2012 6:16 pm Post subject: RE:Concurrency and Multithreading |
|
|
Atomic data types just guarantee atomic behaviour -- you won't see "partial" updates, etc. I probably should have said "volatile data types".
Volatility in the JVM guarantees that "A write to a volatile variable ... synchronizes-with all subsequent reads of v by any thread...". This Wikipedia article is helpful: http://en.wikipedia.org/wiki/Java_Memory_Model
Because Java is designed to run on different operating systems, different JVMs, and different processors, a lot of effort went into making sure that language semantics actually cover things like memory barriers implicitly. |
|
|
|
|
|
|
|