Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Threads and synchronization!!

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain how threads and synchronization works? What I understand is when you have a block of synchronized code, you must acquire the lock of the object to get into the synchronized code. But how do you know if a thread can acquire it or not? If it can't, does it hang around outside of the synchornized code until it gets the lock? Once it acquires the lock, where does it resume, into the synchronized block of code? For example.
synchronized (Data) {
<<write code here>>
}
I assume which ever thread that comes into this block of code first gets the lock, and other thread gets blocked because they're not able to acquire the lock, am I right? The thread that enter this synchronize code can call the wait() method. Would this result in the thread giving up the lock? What happens to this thread after? In what situation would ever have to invoke the notifyAll() method?
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hung Tang,
i am also new to the forum but i have read some discussions of the forum on the lock()/unLock().
====================
I assume which ever thread that comes into this block of code first gets the lock, and other thread gets blocked because they're not able to acquire the lock, am I right?
=============================
yes, you are correct.
=============================
The thread that enter this synchronize code can call the wait() method.
=============================
yes, to invoke wait(), thread needs lock of object.
=============================
Would this result in the thread giving up the lock?
=============================
yes again, invoking wait() means giving up the lock.
=============================
What happens to this thread after? In what situation would ever have to invoke the notifyAll() method?
=============================
first ques. is in which situation you will call wait(), when a record (resource) will not be available.
when the record will be available in that situation you will call notifyAll().
to implement the above, you can maintain a collection API, say HashSet, when thread will execute syn. code first step will be to check if the record no. is in the HashSet, if yes, then call wait().
if it is not in the HashSet, put it in the HashSet (for other thread which want the same record will check HashSet and call wait()), continue to excecute. after that call notifyAll().
i have not implement the procedure, so wait for ans. from someone who have implement it.
best of luck.
regards,
Bhuvan.
[ March 31, 2002: Message edited by: Bhuvan ]
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic