class Gate
{
private int counter = 0;
private String name="nobody";
private String address = "nowhere";
public synchronized void pass(String name,String address)
{
this.counter++;
this.name = name;
this.address = address;
check();
}
public synchronized String toString()
{
return "No."+counter+":"+name+","+address;
}
private void check()
{
if(name.charAt(0)!=address.charAt(0))
{
System.out.println("BROKEN"+this.toString());
System.out.println("name[0]"+name.charAt(0));
System.out.println("address[0]"+address.charAt(0));
}else
{
System.out.println("yes");
}
}
}
class UserThread extends Thread
{
private final Gate gate;
private final String myName;
private final String myAddress;
public UserThread(Gate gate,String myName,String myAddress)
{
this.gate = gate;
this.myAddress = myAddress;
this.myName = myName;
}
public void run()
{
System.out.println(myName+"BEGIN");
while(true)
{
gate.pass(myName, myAddress);
}
}
}
public class Test {
public synchronized static void main(String[] args) throws UnsupportedEncodingException, IOException, InterruptedException {
Gate gate = new Gate();
new UserThread(gate,"Alice","Alaska").start();
new UserThread(gate,"Bobby","Brazil").start();
new UserThread(gate,"Chris","Canda").start();
}
}
{
private int counter = 0;
private String name="nobody";
private String address = "nowhere";
public synchronized void pass(String name,String address)
{
this.counter++;
this.name = name;
this.address = address;
check();
}
public synchronized String toString()
{
return "No."+counter+":"+name+","+address;
}
private void check()
{
if(name.charAt(0)!=address.charAt(0))
{
System.out.println("BROKEN"+this.toString());
System.out.println("name[0]"+name.charAt(0));
System.out.println("address[0]"+address.charAt(0));
}else
{
System.out.println("yes");
}
}
}
class UserThread extends Thread
{
private final Gate gate;
private final String myName;
private final String myAddress;
public UserThread(Gate gate,String myName,String myAddress)
{
this.gate = gate;
this.myAddress = myAddress;
this.myName = myName;
}
public void run()
{
System.out.println(myName+"BEGIN");
while(true)
{
gate.pass(myName, myAddress);
}
}
}
public class Test {
public synchronized static void main(String[] args) throws UnsupportedEncodingException, IOException, InterruptedException {
Gate gate = new Gate();
new UserThread(gate,"Alice","Alaska").start();
new UserThread(gate,"Bobby","Brazil").start();
new UserThread(gate,"Chris","Canda").start();
}
}