Home > Uncategorized > PingPong in Java

PingPong in Java

While I was trying to learn Scala Actors, I came across PingPong problem. It is the simplest form of two different threads communicating each other. For Scala version checkout here – http://www.scala-lang.org/node/54. Below is the pingpong in java with two different implementation. One using locks and other one using classic thread programming.

package test;

public class PingPong {
	private final int MAX = 10;
	private volatile int counter = 0;
	private volatile boolean ponged = true;

	public synchronized void ping() {
		try {
			if (ponged) {
				counter++;
				System.out.println("Ping");
				ponged = false;
			}
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized void pong() {
		if (!ponged) {
			counter++;
			System.out.println("Pong");
			ponged = true;
		}
		notifyAll();
	}

	public static void main(String... arg) {
		final PingPong pingpong = new PingPong();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.ping();
			}
		}.start();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.pong();
			}
		}.start();
	}
}
package test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PingPongLock {
	private final int MAX = 10;
	private volatile int counter = 0;
	private Lock lock = new ReentrantLock();
	private Condition pinged;
	private Condition ponged;

	public PingPongLock() {
		pinged = lock.newCondition();
		ponged = lock.newCondition();
	}

	public void ping() {
		try {
			lock.lock();
			counter++;
			System.out.println("Ping");
			pinged.signalAll();
			if (counter < MAX)
				ponged.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void pong() {
		try {
			lock.lock();
			counter++;
			System.out.println("Pong");
			ponged.signalAll();
			if (counter < MAX)
				pinged.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public static void main(String... arg) {
		final PingPongLock pingpong = new PingPongLock();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.ping();
			}
		}.start();
		new Thread() {
			public void run() {
				while (pingpong.counter < pingpong.MAX)
					pingpong.pong();
			}
		}.start();
	}
}

Advertisements
Categories: Uncategorized
  1. anonymous
    April 15, 2010 at 10:34 pm

    Can you explain what to learn from this????

  2. Jugal Shah
    March 9, 2011 at 2:23 pm

    A lucid post! Thanks for sharing the information.

  3. Antalas
    October 10, 2012 at 9:54 am

    There’s room for improvement of listing 1: “pong” eats CPU cycles just like that, because it loops in the cycle where it could have waited for notification;

    The other listing, if rewritten with wait/notify idioms, lacks the flaw.

    BTW, you are among the top 5 links in Google for “java concurrency ping pong”

  4. Sanay
    February 21, 2013 at 1:59 pm

    find the more the accurate PingPong with wait and notify
    ——————-
    public class PingPong {

    private volatile boolean ponged = true;

    public synchronized void ping() {

    try {

    if (ponged) {

    System.out.print(“Ping “);

    ponged = false;

    }
    else
    {
    wait();
    }
    notifyAll();
    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    }

    public synchronized void pong() {

    try {
    if (!ponged)
    {

    System.out.println(“Pong “);

    ponged = true;

    }
    else
    {
    wait();
    }
    notifyAll();
    }
    catch (InterruptedException e)
    {

    }
    }

    public static void main(String arg[])
    {

    final PingPong pingpong = new PingPong();

    new Thread() {

    public void run() {

    while (true)

    pingpong.ping();

    }

    }.start();

    new Thread() {

    public void run() {

    while (true)

    pingpong.pong();

    }

    }.start();

    }

    }

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: