Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes deadlock with single thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "deadlock with single thread" Watch "deadlock with single thread" New topic
Author

deadlock with single thread

vadz
Greenhorn

Joined: May 23, 2005
Posts: 6
Can there be a deadlock situation with single thread, if so, how to avoid that.

Thanks
vadz
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
There can still be a deadlock with only 1 thread, but only with resources used by other processes in the system (whether on the machine or anywhere else in the net). The methods to avoid them are the same as with multiple threads in one process.

Check out
this page if you are interested in a quick explaination of the concepts, but for a thorough discussion on the topic, I would recommend "Modern Operating Systems" from Tanenbaum
vadz
Greenhorn

Joined: May 23, 2005
Posts: 6
Thanks Marks, The link u provided is indeed helpful for brushing the basics
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147
Timmy,

I dont think you can use the same tools to prevent dead lock of ur thread with a process in OS.

The only build in process that I know of to prevent dead lock is wait/notify in a synchronized block. But synchronized only exist in JVM process and does not effect anything else.

Shared IO should be managed by JVM and OS.

So, I dont see any way to dead lock single threaded java application.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
I dont think you can use the same tools to prevent dead lock of ur thread with a process in OS.


The tools you use are the same, even though they have a different nomenclature. We use mutexes (the equivalent of an object lock) and semaphores (which are basically just fancy mutexes) to synchronize the use of resources, and shared memories or message queues to transfer information between processes. It is probably not what the original poster wanted to know, and maybe I was going a bit too deep with my answer, but it is technically possible to create a deadlock if your application only has one thread, by starting multiple processes. Vadz, you probably don't need to worry about deadlocks in a single threaded application, unless your app communicates with other processes.

Yaroslav, wait() and notify() will not automatically prevent deadlocks in your application. From core Java 2 Volume 2:

Consider the following situation:
Account 1 : $2000
Account 2 : $3000

Thread 1 : Transfer $3000 from Account 1 to Account 2
Thread 2 : Transfer $4000 from Account 2 to Account 1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: deadlock with single thread
 
Similar Threads
Confusion between Single Thread and Multi thread.
Deadlock
Threads
pipe transformation to another transformation