Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

two threads waiting for one resource  RSS feed

 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai everybody..i am new to this forum
here's my problom
if two threads are waiting for one resource or data,if the data came,which thread will get that data?.how can i make the 'data' availeable for only one thread?.
or
andy and freddy is waiting for alice.if alice came,who'll get alice?.Or how can i assign alice to freddy?.
anybody plz
wishing everybody a better day..
basha
 
Cory Wilkerson
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your answer lies in Thread priorities, see, Thread.setPriority(int priority). Addtionally, it's worth nothing that Priority implementation (as with threads in general) varies from platform to platform.
[ January 05, 2003: Message edited by: Cory Wilkerson ]
 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
threads are in defferent priorities.. but deadlock occurs..in windows
all suggestions are welcome..
basha
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As corywilk said thread priority may not be honored by the scheduler implemented for the platform.
If two threads are started one after the other and if they are trying to access the same resource, deadlock occurance is highly likely. Although you cannot control who gets to run first, you can control how the shared object is accessed. If you synchronize the access to the shared object, no matter who gets first, the next thread will have to wait till the first one finishes the job.
For this you can use either synchronized methods or synchornized(obj) constructs.
 
Mark Kozel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajith Kallambella:
As corywilk said thread priority may not be honored by the scheduler implemented for the platform.
If two threads are started one after the other and if they are trying to access the same resource, deadlock occurance is highly likely. Although you cannot control who gets to run first, you can control how the shared object is accessed. If you synchronize the access to the shared object, no matter who gets first, the next thread will have to wait till the first one finishes the job.
For this you can use either synchronized methods or synchornized(obj) constructs.

Is it required to use the wait() and notify() Object methods with a synchornized resource?
-Mark
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it required to use the wait() and notify() Object methods with a synchornized resource?
No - but if you need to communicate between threads, they are a common technique. Any time one thread should not proceed until some task is completed by another thread, there's a good chance wait()/notify() is the best solution to the problem.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!