Reputation: 303
I have this piece of code
public MultiThreadedSum(ArrayBuffer ArrayBufferInst)
{
this.ArrayBufferInst = ArrayBufferInst;
Sum = 0;
Flag = false;
StopFlag = false;
}
public synchronized void Sum2Elements()
{
while(Flag)
{
try {wait();}
catch (InterruptedException e){}
}
Flag = true;
if (StopFlag)
{
notifyAll();
return;
}
System.out.println("Removing and adding 2 elements.");
Sum = ArrayBufferInst.Sum2Elements();
notifyAll();
}
public synchronized void InsertElement()
{
while(!Flag)
{
try {wait();}
catch (InterruptedException e){}
}
Flag = false;
if (StopFlag)
{
notifyAll();
return;
}
System.out.println("Inserting the sum.");
ArrayBufferInst.InsertElement(Sum);
if (ArrayBufferInst.RetunrSize() == 1)
{
StopFlag = true;
}
System.out.println(ArrayBufferInst);
notifyAll();
}
As you can see, I set the Flag to be false first so one of the threads can enter the Sum2Elements method and change it to true and by that, making everyone wait.
I know that in synchronized code, only one thread can do its thing, well here I have two synchronized methods, does it mean that 2 threads are trying to conduct this methods after each notifyall?
And if so, is it not possible for one thread to enter Sum2Elements, change the flag to true before the other thread enters InsertElement, and by that skipping the while loop?
Thanks
Upvotes: 1
Views: 285
Reputation: 966
Locks are obtained on objects of a class & not on any particular synchronized method.
Both the methods are instance methods. So if one of the threads have entered any synchronized method for an object, A say, then any other thread cant enter any synchronized method for that object until the running thread doesnt call notifyAll()
method. At that stage all the waiting threads compete to become active but it depends on the thread scheduler to choose a thread which is to become active.
If you want that two different threads should access these synchronized methods simultaneously then the 2 threads should operate on 2 different objects of the class.
Upvotes: 0
Reputation: 7899
Only one thread can execute one of two method at a time because both are synchronized though order is undefined
As I said one method can be executed by one thread at a time only unless executing thread release the lock
by callingwait
method and other thread get the lock
and execute other synchronized
method which make your both the statements possible.
Upvotes: 0
Reputation: 68935
Only one thread can hold the lock of the object. And then it's only that thread that can enter the synchronized methods on that object.
The thread can however release the lock without returning from the method, by calling Object.wait().
So your code looks good!
does it mean that 2 threads are trying to conduct this methods after each notifyall?
Ans : It is very much possible for two threads to be in two of your synchronized methods since you are calling wait().
is it not possible for one thread to enter Sum2Elements, change the flag to true before the other thread enters InsertElement, and by that skipping the while loop?
Ans : Yes this is possible again for the same reason specified above.
Upvotes: 1