aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes muti-thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "muti-thread question" Watch "muti-thread question" New topic
Author

muti-thread question

Woo Hwang
Greenhorn

Joined: Aug 07, 2001
Posts: 13
public class SyncTest{
private int x;
private int y;
private synchronized void SetX(int i){
x=i;}
private synchronized void SetY(int i){
y=i;}
public void setXY(int i){
SetX(i);
SetY(i);
}
public synchronized boolean check(){
return x!=y;
}
Question is under what condition does check() return true when called from different class?
1.check() can never return true.
2.check() can return true when setXY() is called by multiple thread.
3.check() can return true when multiple thread calls setX() and setY() separately.
4.check() can only return true if SynchTest is changed to allow x and y to be set separately.
I think answers are 1,4 though different people have different opinions. My answer comes like this:
Because setX and setY have private class, it can't? be accessed outside of this class and the only way to reach these methods is by accessing setXY. But if one thread( let's assume there are two threads) reaches setX to modify x value, because setX and setY are synchronized, another thread can't run Synchtest while the first thread is working on setX or setY.
Someone says that it could be possible that right after the first thread finishes setting x, it stops and the second thread starts run and set x with different value from the first thread?. Then the 2nd thread stops, and the the 1st continues its own operation setting y and call check() . In this case, x and y can have different values and number 3 could be an answer. But I don't? think that's?general answer. As I know ,OS with time-sliced model doesn't? follow that rule, and when I had made out whole code and ran, thread didn't? go like the preposition above. It's? more like one thread starts and finishes, and do this operation several times or let another thread go.
Therefore I think, without modifying the code above to set x and y separately, check() never return true.
But it's still confusing to me. Thread is soooooo difficult... That's why I post this to clear this confusion. What do you think the answer is ?
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
Hi, Woo, I have tested with the following code:

Here is part of the output:

It seemed that your opinion is right. But I still have some doubt over the output. Since setXY is not synchronized, it is possibly that one thread gives up the ownership of the object at the end of statement ////A and let other thread access the setXY and modify the x, y value.
I am not sure, anyone has better idea?
Guoqiao
Originally posted by Woo Hwang:


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Woo Hwang
Greenhorn

Joined: Aug 07, 2001
Posts: 13
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: muti-thread question