// wait and notify
class Signal {

	int s;
	Signal () {s=1;}
	public synchronized void Down () {
		try {
			if (s==0) wait();
			s--;
		} catch (Exception e) {} finally {}
	}

	public synchronized void Up () {
		s++;
		notify();
	}

}

class Process extends Thread {
Signal resource;
int pid;
	Process (int id, Signal s) {pid = id; resource = s;}

	public void run () {
	  while (true) {
		resource.Down ();
		for (int i=0; i<10; i++)   {
			System.out.print (pid);
			try {sleep (5*pid);} catch (Exception e) {}
		}
		System.out.println();
		resource.Up ();
		try {sleep (1);} catch (Exception e) {}
	  }
	}
}

class WaitNotifyDemo {

	public static void main (String args[]) {

		Signal s = new Signal ();

		Process p1 = new Process (1,s);
		Process p2 = new Process (2,s);
		Process p3 = new Process (3,s);
		Process p4 = new Process (4,s);
		Process p5 = new Process (5,s);

		p1.start();
		p2.start();
		p3.start();
		p4.start();
		p5.start();

	}
}


