aspose file tools*
The moose likes Threads and Synchronization and the fly likes use-case for parallel/concurrent programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "use-case for parallel/concurrent programming" Watch "use-case for parallel/concurrent programming" New topic
Author

use-case for parallel/concurrent programming

paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 177
Hi,
What do you consider a good use case for parallel/concurrent programming?

Thanks,
Paul

[Devaka: Edited to add a meaningful subject line]
Sergey Babkin
author
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
paul nisset wrote:
What do you consider a good use case for parallel/concurrent programming?


I'd say, whenever it either improves the performance by a noticeable amount (if something is fast enough single-threaded, why bother with multi-threading?) or makes the programming easier. Basically, if there is some natural parallelism in the program that can be exploited, and it's worth the effort.
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 177
Thank you. I guess what I was asking is there a class of applications that lend themselves more naturally to Parallelism? For example a web service or a heavily used web app where each user session might get routed into one of several threads from a pool of threads ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

paul nisset wrote:... For example a web service or a heavily used web app where each user session might get routed into one of several threads from a pool of threads ?

In general each request for a web application is handled by a separate thread, this is implemented by the application/web servers.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Sergey Babkin
author
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
Yes, it's a good example. That's why the cloud computing can use a lot of parallelism: it handles a lot of inherently parallel requests.

Sometimes you can add the parallelism by partitioning the data in some obvious way. For example, if you handle the stock quotes, each stock symbol is handled independently from the others. So you can separate them into multiple threads, each one handling its own subset of symbols. As long as some data is independent from the others, it can always be separated into another thread. The difficulties start when the dependencies in the data processing appear.
Mike Peters
Ranch Hand

Joined: Oct 10, 2009
Posts: 67

You can find parallelism everywhere. Some examples:
- If you have a user interface which is not responsive, then this might be due to a long running task. A nice way to handle such a thing is to keep that long running task out of the main user interface thread and run it in the background. Then you may be able to only block a small part of the user interface while that task is running. This "pattern" is used a lot in the Eclipse IDE user interface for example.
- If you want to speed up a long running server task, then often such a task can be broken up in some parallel tasks. In such a case I draw the server process in a diagram (e.g. UML activity diagram). If you do not think parallel, then often you draw a sequential diagram, but often a lot of these sequential steps are unrelated to each other and may be performed in parallel. For example:
Sequential:
Start -> Validate order -> Persist order -> Update customer -> Inform logistics -> Send customer e-mail -> End

Parallel (the update customer, inform logistics and send customer e-mail can be executed in parallel):
Start -> Validate order -> Persist order -> (Update customer | Inform logistics | Send customer e-mail) -> End
- The example which has been already mentioned, a system (e.g. web application) with multiple input channels (e.g. user sessions) which may not interfere.

Finally don't over-parallelize, as Sergey indicated, there is no need to complicate things if there is nothing to gain. On the other hand I advise you to experiment with it, because a lot of things look complicated when you have never done them before. And as you get more and more experienced with the subject and your tool set grows, you will be able to handle more difficult problems with more ease.


Mike Peters
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use-case for parallel/concurrent programming