0

Given this synchronized method that works on a resource:

int resource = 5;
public synchronized void myMethod(){
   resource = resource +5;

And a thread that execute that method as task:

Thread myThread = new Thread(() -> myMethod());
myThread.start;

If there are multiple threads like this one, how exactly is this logic handled when the thread starts?

if(another thread is already working on the synchronized method){
  wait for the synchronized method to be available;
}
else{
  execute the synchronized method;
}

I know about the wait() and notify() methods but I don't fully understand the logic implementation.

Spyromancer
  • 355
  • 2
  • 9

0 Answers0