package com.help4j.core.thread; public class PingPong { public static void main(String[] args) { Object LOCK_OBJECT = new Object(); Thread ping = new Thread(new PingPongThread(LOCK_OBJECT, "Ping")); Thread pong = new Thread(new PingPongThread(LOCK_OBJECT, "Pong")); ping.start(); pong.start(); } } class PingPongThread implements Runnable{ private Object LOCK_OBJECT; private String name; public PingPongThread(Object LOCK_OBJECT, String name) { this.LOCK_OBJECT = LOCK_OBJECT; this.name = name; } @Override public void run() { synchronized (LOCK_OBJECT) { while(true) { System.out.println(name); try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } LOCK_OBJECT.notify(); try { LOCK_OBJECT.wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
Thursday, 4 July 2019
Ping Pong using wait notify
Subscribe to:
Posts (Atom)
Top CSS Interview Questions
These CSS interview questions are based on my personal interview experience. Likelihood of question being asked in the interview is from to...
-
Hi Readers, click here for updated kafka notes If you are planning or preparing for Apache Kafka Certification then this is the right ...
-
Implementation of Elevator/ Lift has been asked in many interviews. I have tried to implement it using muti-threading and TreeSet. TreeSet...
-
Avro Primitive Types null boolean int (32 bit) long (64 bit) float (32 bit) double (64 bit) byte[] (8 bit) string (char squ...