Consider the below code:-
public class ExecProgram {
private int count = 0;
private Lock lock = new ReentrantLock();
private Condition cond = lock.newCondition();
private void increment(String threadName) {
for (int i = 0; i < 5; i++) {
count++;
System.out.println("Incremented by "+threadName);
}
}
public void ThreadOne() throws InterruptedException {
lock.lock();
System.out.println("Waiting 1 ....");
cond.await();
System.out.println("Woken up!");
try {
increment("Thread1");
} finally {
System.out.println("Waiting done 1 ....");
lock.unlock();
}
}
public void ThreadTwo() throws InterruptedException {
Thread.sleep(1000);
lock.lock();
System.out.println("Waiting 2 ....");
System.out.println("Press the return key!");
new Scanner(System.in).nextLine();
System.out.println("Rcvd return key!");
cond.signal();
try {
increment("Thread2");
} finally {
System.out.println("Waiting done 2 ....");
//should be written to unlock Thread whenever signal() is called
lock.unlock();
}
}
public void finished() {
System.out.println("Count is: " + count);
}
}
The output which I have received as part of the above code:-
Waiting 1 ....
Waiting 2 ....
Press the return key!
Rcvd return key!
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Waiting done 2 ....
Woken up!
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Waiting done 1 ....
Count is: 10
Time taken 4112
My question is:- The first thread after acquiring the lock has printed Waiting 1 ….
Now the second thread will not be able to get the lock. But how come the second thread will be able to get the lock and print Waiting 2 ….
Since because, the first thread has been asked to wait on some condition, it was moved to wait state and the second thread has acquired the lock and proceeded to execution.
Is my above understanding correct?
If I change the threadOne and threadTwo methods as below:-
public void ThreadOne() throws InterruptedException {
lock.lock();
System.out.println("Waiting 1 ....");
// cond.await();
System.out.println("Woken up!");
try {
increment("Thread1");
} finally {
System.out.println("Waiting done 1 ....");
lock.unlock();
}
}
ThreadTwo
public void ThreadTwo() throws InterruptedException {
// Thread.sleep(1000);
lock.lock();
System.out.println("Waiting 2 ....");
/* System.out.println("Press the return key!");
new Scanner(System.in).nextLine();
System.out.println("Rcvd return key!");
cond.signal(); */
try {
increment("Thread2");
} finally {
System.out.println("Waiting done 2 ....");
//should be written to unlock Thread whenever signal() is called
lock.unlock();
}
}
Then I could see the output being printed as:-
Waiting 1 ....
Woken up!
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Incremented by Thread1
Waiting done 1 ....
Waiting 2 ....
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Incremented by Thread2
Waiting done 2 ....
Count is: 10
Time taken 11
Where the second thread has started execution after first thread has completed execution and I could see Waiting 2…. has been printed after the lock by thread 2 has been acquired.
Could someone please help me to know why the difference in first case alone? Is it because of that condition, the first thread was made to wait and the second thread has executed it and signaled the first thread to resume execution?