Thread : How to use wait() and notify() methods to handle synchronisation between threads

Java example


This example shows how to use notify() and wait() methods of Thread class.

Get the sources of the example from the following GitHub url : https://github.com/javaspringexamples/ThreadNotifyWait.git

Or Download a .zip file : https://github.com/javaspringexamples/ThreadNotifyWait/archive/master.zip

Thread-1 is waiting...
Thread-0: Says the field soccer is no more available.P1 please gets in my player and have fun.
Thread-3 is waiting...
P1 shoots ball 1
Thread-2 is waiting...
P1 shoots ball 2
P1 shoots ball 3
P1 shoots ball 4
P1 shoots ball 5
P1 shoots ball 6
P1 shoots ball 7
P1 shoots ball 8
P1 shoots ball 9
P1 shoots ball 10
Thread-0: Says the field soccer is available.
Dear threads get notified...!
Thread-1 is notified...
Thread-1: Says the field soccer is no more available.P2 please gets in my player and have fun.
P2 shoots ball 1
P2 shoots ball 2
P2 shoots ball 3
P2 shoots ball 4
P2 shoots ball 5
P2 shoots ball 6
P2 shoots ball 7
P2 shoots ball 8
P2 shoots ball 9
P2 shoots ball 10
Thread-1: Says the field soccer is available.
Dear threads get notified...!
Thread-3 is notified...
Thread-3: Says the field soccer is no more available.P4 please gets in my player and have fun.
P4 shoots ball 1
P4 shoots ball 2
P4 shoots ball 3
P4 shoots ball 4
P4 shoots ball 5
P4 shoots ball 6
P4 shoots ball 7
P4 shoots ball 8
P4 shoots ball 9
P4 shoots ball 10
Thread-3: Says the field soccer is available.
Dear threads get notified...!
Thread-2 is notified...
Thread-2: Says the field soccer is no more available.P3 please gets in my player and have fun.
P3 shoots ball 1
P3 shoots ball 2
P3 shoots ball 3
P3 shoots ball 4
P3 shoots ball 5
P3 shoots ball 6
P3 shoots ball 7
P3 shoots ball 8
P3 shoots ball 9
P3 shoots ball 10
Thread-2: Says the field soccer is available.
Dear threads get notified...!
All balls shooted.
package javaspringexamples.ThreadExamples;

/**
 * This example shows how to use notify() and wait() methods of Thread class.
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class Player extends Thread {

	private String name;
	private SoccerField sf;

	public Player(String _name, SoccerField _sf) {
		name = _name;
		sf = _sf;
	}

	/**
	 * If the soccer field is available, then the player gets in to shoot balls.
	 */
	public void shootBalls() {
		sf.setAvailable(false);
		System.out.println(getName() + ": Says the field soccer is no more available." + name
				+ " please gets in my player and have fun.");
		for (int i = 1; i <= 10; i++)
			System.out.println(name + " shoots ball " + i);
		System.out.println(getName() + ": Says the field soccer is available.");
		sf.setAvailable(true);
	}

	@Override
	public void run() {
		sf.getSoccerField();
		shootBalls();
	}

	/**
	 * The main method.
	 */
	public static void main(String[] args) throws InterruptedException {
		SoccerField sf = new SoccerField();
		Player p1 = new Player("P1", sf);
		Player p2 = new Player("P2", sf);
		Player p3 = new Player("P3", sf);
		Player p4 = new Player("P4", sf);
		p1.start();
		p2.start();
		p3.start();
		p4.start();
		// Waiting for threads terminating their jobs.
		p1.join();
		p2.join();
		p3.join();
		p4.join();
		System.out.println("All balls shooted.");
	}
}
package javaspringexamples.ThreadExamples;

/**
 * This example shows how to use notify() and wait() methods of Thread class.
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class SoccerField {
	private boolean available = true;

	/**
	 * If the soccer field is not available, then the thread will wait until
	 * being notified that the soccer field is available again.
	 */
	public synchronized void getSoccerField() {
		if (!available) {
			try {
				System.out.println(Thread.currentThread().getName() + " is waiting...");
				wait();
				System.out.println(Thread.currentThread().getName() + " is notified...");
			} catch (InterruptedException e) {
				// Do something to handle the InterruptedException
				e.printStackTrace();
			}
		}
	}

	/**
	 * When the soccer field is available, the other threads are notified to
	 * continue doing their jobs.
	 * 
	 */
	public synchronized void setAvailable(boolean a) {
		available = a;
		if (a) {
			notify();
			System.out.println("Dear threads get notified...!");
		}
	}

	public boolean isAvailable() {
		return available;
	}
}