What I made out to return true is:
class
test {
public static void main(
String args[]){
synchtest s=new synchtest();
Thread1 A=new Thread1();
Thread2 B=new Thread2();
Thread t1=new Thread(A);
Thread t2=new Thread(B);
A.Init(s,t2);
/// To give handles of synchtest and thread of thread2 to thread1 B.Init(s,t1);
/// To give handles of synchtest and thread of thread1 to thread2 t1.start();
t2.start();
}
}
class Thread1 extends Thread{
synchtest s1;
Thread t2;
int i;
void Init(synchtest s,Thread t){
s1=s;
t2=t;
}
public void run(){
do {
System.out.println("Thread 1 goes "+i+++" time(s)");
try{
s1.setX(1);
System.out.println("thread1 sleeps after setting x 1");
System.out.println("now x="+s1.x+" y="+s1.y);
sleep(2000);
/// thread1 sleep to give thread2 a chance to run s1.setY(1);
System.out.println("thread1 sleeps after setting y 1");
System.out.println("now x="+s1.x+" y="+s1.y);
//sleep(2000);
Here, I didn't put sleep because setting y in here likely came after thread2's sleep after setting x. So x,y have different value each other }catch (InterruptedException e){
break;}
}while(s1.check()==false);
System.out.println("Thread1 finishes. x="+s1.x+" \t"+"y="+s1.y);
t2.interrupt();
// When this thread finishes, call interrupt() to another sleeping thread to wake up and break out of while root }}
class Thread2 extends Thread{
int j;
synchtest s2;
Thread t1;
void Init(synchtest s,Thread t){
s2=s;
t1=t;
}
public void run(){
do {
System.out.println("Thread 2 goes "+j+++" time(s)");
try{
s2.setX(2);
System.out.println("thread2 sleeps after setting x 2");
System.out.println("now x="+s2.x+" y="+s2.y);
sleep(2000);
s2.setY(2);
System.out.println("thread2 sleeps after setting y 2");
System.out.println("now x="+s2.x+" y="+s2.y);
sleep(2000);
}catch(InterruptedException e){
break;}
}while(s2.check()==false);
System.out.println("Thread 2 Finishes. x="+s2.x+" \t"+"y="+s2.y);
t1.interrupt();
}}
class synchtest {
public int x;
public int y;
public void setX(int i){
x=i;
}
public void setY(int i){
y=i;
}
public synchronized boolean check(){
return x!=y;}
}
And the result is:
Thread 2 goes 0 time(s)
thread2 sleeps after setting x 2
now x=2 y=0
Thread 1 goes 0 time(s)
thread1 sleeps after setting x 1
now x=1 y=0
thread2 sleeps after setting y 2
now x=1 y=2
thread1 sleeps after setting y 1
now x=1 y=1
Thread 1 goes 1 time(s)
thread1 sleeps after setting x 1
now x=1 y=1
Thread 2 goes 1 time(s)
thread2 sleeps after setting x 2
now x=2 y=1
thread1 sleeps after setting y 1
now x=2 y=1
Thread1 finishes. x=2 y=1
Thread 2 Finishes. x=2 y=1
Sorry. The code might seem to be a bit mess
As to SetXY method, it isn't synchronized but the only chance the thread scheduler let another thread go is when the thread running finishes setting one value and moves to set another one. But could it be possible? When I ran the code without sleep() method, the thread didn't stops until its process finished. Some functions are needed to put in between setX and setY to pause one thread and let another go. Without that function, we rarely ,scarcely, hardly, and seldom see one thread let another thread go in the middle of perfoming setXY method.
I need BIG discussion on this y'all
[This message has been edited by Woo Hwang (edited August 13, 2001).]
[This message has been edited by Woo Hwang (edited August 13, 2001).]