Using key word synchronization: threadsync_synchronize.htm 1-2-3

import java.util.*;
import java.io.*;
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 name as worker : ");
String str = br.readLine();
System.out.println("Welcome " + str+ " to Voting Zone ");
Booth booth = new Booth();
String s;
for(int i = 1; i < 8; i++)
{
s = str + i;
Booth_sync booth_sync = new Booth_sync(booth, s, i);

try {
booth_sync.t_booth.join();
}
catch(InterruptedException e)
{
// customized messages
}}}}

/////
class Booth_sync implements Runnable
{
Booth booth;
String name;
Thread t_booth;
private int voter_no;
public Booth_sync(Booth booth, String name, int voter_no)
{
this.name = name;
this.booth = booth;
this.voter_no= voter_no;
t_booth = new Thread(this);
t_booth.setName(name);
t_booth.start();
}
public void run()
{
synchronized(booth)
{
booth.voterin(voter_no);
//System.out.println("voter in local : " + booth.voterin(voter_no));
booth.voterout(voter_no);
//System.out.println("\t\tvoter out");
}}}
class Booth
{
private int voter_no ;
public void voterin( int voter_in)
{
this.voter_no = voter_in;
System.out.println("voter in server : " + voter_no);
}
public int voterout(int vouter_out)
{
this.voter_no= vouter_out;
System.out.println("\t\tvoter out server : " + voter_no);
return voter_no;
}
}

 

 
 
 

Eclipse

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();
String s;
for(int i = 1; i < 6; i++)
{
s = str + i;
Booth_sync booth_sync = new Booth_sync(booth, s, i);

try {
booth_sync.t_booth.join();
}
catch(InterruptedException e)
{
//custom message etc
}}}
}
class Booth_sync implements Runnable
{
Booth booth;
String name;
Thread t_booth;
private int voter_no;
public Booth_sync(Booth booth, String name, int voter_no)
{
this.name = name;
this.booth = booth;
this.voter_no= voter_no;
t_booth = new Thread(this);
t_booth.setName(name);
t_booth.start();
}
public void run()
{
synchronized(booth)
{
booth.voterin(voter_no);

System.out.println("voter-in sync : " + voter_no + " : " +Thread.currentThread().getId());
booth.voterout(voter_no);
System.out.println("\t\tvoter-out sync : " + voter_no+ " : " +Thread.currentThread().getId());
}}
}
class Booth
{
private int voter_no ;
public void voterin( int voter_in)
{
this.voter_no = voter_in;
System.out.println("Thread-ID at Booth :" + Thread.currentThread().getId());
System.out.println("voter-in at Booth : " + voter_no);
}
public int voterout(int vouter_out)
{
this.voter_no= vouter_out;
System.out.println("\t\tThread-ID at Booth : " + Thread.currentThread().getId());
System.out.println("\t\tvoter-out at Booth : " + voter_no);
return voter_no;
}
}
 

In this example we intend to view the followings.
  •