import java.util.*;
import java.io.*;
//javac thread_synchro.java
public class test
{
//public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type location : ");
String str = br.readLine();
System.out.println("Welcome to" + str+ " Voting Zone ");
Booth booth = new Booth();
for(int i = 1; i < 6; i++)
{
new Voter_out(booth, i);
new Voter_in(booth, i);
}
}
}
class Booth
{
int voter_no;
boolean voter_present;
///
synchronized void voterin(int voter_no)
{
if(voter_present)
{
try
{
wait();
}
catch(InterruptedException e)
{
//
}
}
this.voter_no = voter_no;
voter_present = true;
notify();
System.out.println("Voter_in : " + voter_no);
System.out.println("Thread : " + Thread.currentThread().getId());
}
//
synchronized void voterout()
{
if(!voter_present)
{
try
{
wait();
}
catch(InterruptedException e)
{
//
}
}
this.voter_no = voter_no;
voter_present = false;
notify();
System.out.println("\t\tVoter_out : " + voter_no);
System.out.println("\t\tThread : " + Thread.currentThread().getId());
}
}
class Voter_out implements Runnable
{
Booth booth;
Thread t_out;
int voter_no;
public Voter_out(Booth booth, int voter_no)
{
this.booth = booth;
this.voter_no = voter_no;
t_out = new Thread(this);
t_out.start();
}
public void run()
{
booth.voterout();
}
}
class Voter_in implements Runnable
{
Booth booth;
Thread t_in;
int voter_no;
public Voter_in( Booth booth, int voter_no)
{
this.booth = booth;
this.voter_no = voter_no;
t_in = new Thread(this);
t_in.start();
}
public void run()
{
booth.voterin(voter_no);
}
}