Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion between Single Thread and Multi thread.

 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Can any body help me in java threading.What is the Difference between single thread and multi thread?is it possible to access a resources simultaneously in multi threaded environment if yes then will it not cause deadlock and if it is causing deadlock then what is the advantage of multi thread?

Thanks.
Lokesh Kumar Pattajoshi
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the Difference between single thread and multi thread?


Simply put, single threded means you have only one thread which is running all the code in the application so, Multi-threaded has more than one thread involved and can be run concurrently (through time slicing). Actually if the system has only one core, at a time only one thread will be running (but the user will see as if all are running concurrently). But it has the advantage of letting multiple threads finish their work more efficiently (ex: if one thread need to wait for something say user input and other thread(s) can utilize the valuable resources like processor time etc..).
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to work through the chapter on concurrency in the Sun Java Tutorial: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html

is it possible to access a resources simultaneously in multi threaded environment

Principally, yes.
if yes then will it not cause deadlock and if it is causing deadlock then what is the advantage of multi thread?

Obviously, deadlocks are to be avoided - that's part of the art of concurrent programming. But just because a resource is shared between threads does not mean that it will cause a deadlock. One needs to analyze carefully the possible flows of control, and determine how the shared resource should be protected in order to avoid both deadlocks and inconsistent state.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic