I was reading @Ashish Lahoti coment in this post: https://stackoverflow.com/a/64077645/15456334 and in "codingnconcepts" web page, I was traying to get a consistent output like:
ping 1
pong 1
ping 2
pong 2
.
.
.
ping 500
pong 500
Always 'ping' and then 'pong' not possible get two same word ping ping or pong pong.
But if I change the time repetitions to 500 and I change the time in "conditionMet.await(2, TimeUnit.SECONDS);" to 0 "conditionMet.await(0, TimeUnit.SECONDS):" I get the next output:
ping 1
pong 1
ping 2
pong 2
.
.
.
Ping 493
Ping 494
Ping 495
Ping 496
Ping 497
Ping 498
Ping 499
Ping 500
As you see in the output exist ping ping repetitions.
I use the wait and notify method And always is OK with the oputput, I have the expected output:
package pingponghilos;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Principal {
public static void main(String[] args) {
Runnable ping = new PingPong("PING");
Runnable pong = new PingPong("pong");
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(ping);
executorService.submit(pong);
System.out.println("hola desde el main");
}
}
class PingPong implements Runnable {
private static final Object locker = new Object(); // Locker util
private String name;
public PingPong(String name) {
this.name = name;
}
@Override
public void run() {
while (true) {
synchronized (locker) {
System.out.println(name);
locker.notifyAll();
try {
locker.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
But I am trying to understand the Reentrant lock condition to implement the same as I got with wait() and notify().
Any suggestions? Thanks in advance.