Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Concurrency and Multithreading
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zren




PostPosted: 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
Sponsor
sponsor
rdrake




PostPosted: Sun Aug 12, 2012 10:18 pm   Post subject: RE:Concurrency and Multithreading

This is the only way I'll do it on the JVM:

http://clojure.org/concurrent_programming
mirhagk




PostPosted: 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




PostPosted: Mon Aug 13, 2012 4:06 pm   Post subject: Re: Concurrency and Multithreading

When I was doing J2EE development back in the day, Effective Java by Josh Bloch was probably one of the best written Java books. Lots of great best practices including some threading and concurrency. It is currently in 2nd edition, http://www.amazon.com/gp/product/0321356683?ie=UTF8&tag=guru990f-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0321356683.

I'm not familiar with the most up to date books, but Josh Bloch is a co-author of Java Concurrency in Practice, http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/ref=cm_cr_dp_asin_lnk, which seems to get very high reviews as well.
DemonWasp




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: