• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parallel Programming in Java

 
Rinku Gambhir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please share some books,links for doing parallel programming in Java,
how to print a simple message like my name , number of times I have cores in my processor(Multi-core processors).
Does Java support OpenMP like directives so that I can easily parallelize my existing serial code.
 
Stephan van Hulst
Bartender
Posts: 6587
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've heard a really good book is Java Concurrency in Practice by Brian Goetz. I haven't read it myself yet though.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rinku Gambhir wrote:Please share some books,links for doing parallel programming in Java.

I think you need to explain what you mean by "parallel programming".

Java's primary unit of execution is a Thread (or Runnable), and after that you basically leave it up to the JVM to decide how to do things; so if you're expecting the kind of control over processors you (presumably) have in C or C++ you're likely to be disappointed. There is some level of tuning available with different types of locks and concurrent structures, but these are fairly esoteric and introduce a level of complexity that most Java programmers rarely need.

For example:
how to print a simple message like my name , number of times I have cores in my processor(Multi-core processors)

It's possible that you can do it, but equally possible that you can't, because things like "number of processors" is simply not the sort of thing that the Java language (or a Java programmer) is interested in.

That doesn't mean you can't do multi-threaded programming though - indeed Java makes it simpler than most; mainly because it doesn't concern itself with such low-level things as "directives" (unless you count 'synchronized' as a directive ).

HIH

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:It's possible that you can do it, but equally possible that you can't, because things like "number of processors" is simply not the sort of thing that the Java language (or a Java programmer) is interested in.

I disagree with that the number of processors is something you'd not be interested in as a Java developer. If you'll want to tune your multi-threaded Java application to run as efficiently as possible you'll want to take the number of processors into account, so that you don't create too few or too many threads.

Java has a method to get the number of processors available to the JVM: Runtime.getRuntime().availableProcessors()
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I disagree with that the number of processors is something you'd not be interested in as a Java developer. If you'll want to tune your multi-threaded Java application to run as efficiently as possible you'll want to take the number of processors into account, so that you don't create too few or too many threads.

But that assumes I can control the number of Threads I need. If I have an algorithm that can be "parallelized" (ugh), it seems unlikely that its optimum is going to be based on the number of processors that my computer has, so why not just spawn as many Threads as it tells me to and let the JVM (which I would expect to know how many processors.there are) deal with running them?

I take your point however - and thanks for the info; I didn't know about it - I just wonder how often you need that level of control.

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you need this kind of control depends on what you're doing exactly. If it's a web application, you generally should not be creating threads yourself - you should let the webapp container do that for you. The container will decide how many threads to create for handling requests concurrently.

If you're writing a piece of software that does parallel calculations, for example, then you might want to start up a number of threads yourself, or use a thread pool (ExecutorService) with a specific number of worker threads. If you want your whole multi-core CPU to be occupied with calculation tasks, then it's often a good idea to create as many threads as the processor has cores. If you create less threads, then you're not using all cores, and creating more threads isn't useful either because if the processor has N cores, it can't execute more than N threads concurrently anyway.

Another thing that you can use for parallel processing is the fork/join framework, a framework for running computation tasks that can be split up into smaller tasks that can be executed in parallel, and where the results of the subtasks are then joined to finally come to the final result of the calculation.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be the link below would be useful to know more about the parallel operation added in Java 8
https://www.youtube.com/watch?v=lFNnpfMBiCE&list=PLMod1hYiIvSZL1xclvHcsV2dMiminf19x&index=19
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the decision about how many threads you allocate to concurrent processing is rather nuanced.

Sometimes a block of code that is too aggressive in grabbing CPU resources will interfere with other operations. For example, consider an application that has a user interface. Jesper de Jong mentioned Java's Runtime.getRuntime().availableProcessors() method. If you have a heavy weight process that runs as many threads as there are available processors, the contention for resources will result in an unresponsive user interface. This happened to me recently. To address the problem, I simply limited the number of threads I assigned to that particular operation to "availableProcessors minus 1".

On the other hand, if the process that you are running frequently blocks on access to external resources (network or file I/O operations), then having an abundance of threads can be helpful because your application isn't delayed by unnecessary pauses.

Last week, I was testing some code that does heavy weight image processing. In my case, it was fairly easy to parallelize the work by assigning different parts of the image to separate threads. I wrote a test application to measure the reduction in overall runtime accomplished by using multiple threads. In order to eliminate the overhead for creating new threads from the time measurements, I ran the test multiple times in a loop reusing the same Java ThreadPoolExecutor instance for each iteration. I was running on an aging laptop with an i3 CPU. The Java runtime indicated that there were four available processors. I don't have my notes with me, so I am quoting the following run times from memory and they are not perfect, but they illustrate the principle:

Time to complete all processing:
1 thread: 16 seconds
2 threads: 9 seconds
3 threads: 7 seconds
4 threads: 6 seconds

Now, the particular code I was running was very specific to my particular problem. Your results may vary. But I think it is relevant that the improvement in throughput was not linear and at a certain point, adding more threads did not improve things all that much.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Another thing that you can use for parallel processing is the fork/join framework,..

Which - at least from it's name - sounds like the sort of thing I'm most used to from my days with C and Unix. Except back then it wasn't used to maximise multiple cores, because they didn't exist.

Gary W. Lucas wrote:Sometimes a block of code that is too aggressive in grabbing CPU resources will interfere with other operations.,..

And this is basically my point. It seems to me rather like hacking around with nice levels in Unix - something we were warned as sysadmins not to do unless we (a) had a really good reason, and (b) knew exactly what we were doing, and what the side-effects were.

My assumption - perhaps naive - is that "the JVM knows best", and that I'm going to have to work pretty darn hard to better it. And "super tuning" Threads to me runs the same risks as any other form of optimization: namely that it either (a) doesn't actually speed things up at all because it's targeted on only one aspect of the problem, or (b) results in the sorts of problems Gary describes; particularly if the machine is not a dedicated Java server.

Winston
 
Rinku Gambhir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all , for sharing the possible ways of parallel programming in Java
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rinku Gambhir wrote:Thank you all , for sharing the possible ways of parallel programming in Java

You're most welcome. Hope our differing viewpoints didn't confuse you.

Winston
 
Gary W. Lucas
Ranch Hand
Posts: 65
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many, many ways to manage parallel processing. The code below gives an example of an approach based on the idea of running a set of "jobs" to process the work. It is roughly in the form of the test program I mentioned in my earlier post, though I stripped out a lot of the test instrumentation and it doesn't do any actual work [grin]. The idea is that it parcels out the processing into a set of multiple jobs and uses a "status board" to keep track of work in progress.

In the past, I've found it advantageous to use this approach because it gives me a formal model for managing data concurrency issues without adding a lot of overhead for synchronized method calls. But as I said, this is just one possible approach among many. Also, I welcome any comments or suggestions on how to improve the thing.

Gary

 
Dave Tolls
Ranch Hand
Posts: 2285
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last time I had to write any (non-canned) multi-threaded code I used Akka.
It has various ways of defining the number of Actors available for tasks, IIRC.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!