Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deadlock with single thread

 
vadz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can there be a deadlock situation with single thread, if so, how to avoid that.

Thanks
vadz
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marks, The link u provided is indeed helpful for brushing the basics
 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic