This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Tips to visualize multithreaded applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Tips to visualize multithreaded applications" Watch "Tips to visualize multithreaded applications" New topic

Tips to visualize multithreaded applications

java boyinredglass

Joined: Jun 16, 2012
Posts: 2
Hello internets,

I find it really hard to understand multithreading because I can't find a way to visualize threads in my mind. Do you have simple ways of visualizing the various elements (locks, joins, synchronization, etc.) in a multi-threaded application?
Charles Latka

Joined: Apr 19, 2012
Posts: 19

Well it's quite easy... Check this for locks.
Synchronization... well it's everywhere e.g Airplane can't take off untill all passengers are on board and flight controll give permission. Passengers won't board until a gate is open, flight control won't give permission until the runway is clear- those all are threads needed to be synchronized.
The reader and writer problem... you can't read the file, when some is editing it. So you need to reserve (block) the file for you when you want to work with this file. When you block the file, and forget to free it after work, no one will be allowed do do anything with it... Try to google "semaphore"
Threads can work pararell, e.q you can build a house by yourself (one thread) but you can do it much faster with help of other workers (more threads)

Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2749

I've found it helpful to keep swimlanes in my head. The way that many people think closely matches a collaboration diagram. THe conversation in their head goes like this:- "Foo calls Bar, Bar processes and creates thingamajigs. Bar sends the thingamajigs to doo-hickeys...." To me, this is thinking like you are drawing a collabration diagram. The problem is collabration diagram is not good at showing concurrent processes. It's good at showing which components are talking to which other components, but parallelism is lost. You might have an easier time if you think in swimlanes

Having said that I would like to add, the success of a highhly concurrent application lies in it's simplicity. If you are having problems following the code, you might want to rethink the design. Threads shouldn't interact with each other too much
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

You might find a UML Activity Diagram or Interaction Overview Diagram useful.
I agree. Here's the link:
subject: Tips to visualize multithreaded applications
It's not a secret anymore!