• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 quick thread verifications

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I posted this question before but It wasn��t clear what the correct answers are. i'd appreciate it if someone can quickly shed light on this. Thank you.
public class SyncTest
2. private int x;
3. private int y;
4. private synchronized void set X (int i) x=i;
5. private synchronized void set Y (int i) y=i;
6. public void setXY (int i) set X(i); set Y(i);
7. public synchronized boolean check() return X != Y;
8.
Under which conditions will check() return when called from a different class
A. check() can never return true
B. check() can return true when set XY is called by multiple threads
C. check() can true when multiple threads call set X and set Y separately
D. check() can only return true if synchTest is changed to allow x and y to be set
Separately
ans): a,d ???
Q2)
Conside below

public static void main(String args[] ){
X that = new X();
X hey = new X();
(new Thread(that)).start();
(new Thread(hey)).start();
}
public synchronized void run(){
for( ; ; ){
x++;
y++;
System.out.println("x=" + x + ", y=" +y);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}//end of for loop
}//end of run()
}//end of class
Output?
I thought by using synchronized run(), only one Object thread is run and that the other Object thread("hey) must wait till the first one("that") is finished. thus producing
1 1
2 2.. and so on..
but when i ran it, it output
1 1
1 1
2 2
2 2 ... Why? I think I'm getting very confused with the thread and how the idea of synchronization works..
Can Someone give a really Easy and Clear explanation of synchronization??? on methods and blocks??
Thank you so much!! =))
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one.I think the answer should be B.If i am wrong ple let me know.
The second one.I think you just ignored that there are two instances of X, and each one have a lock.so the "synchronized "is just a red herring. Wish this will help you.

Originally posted by kevin goon:
Hi, I posted this question before but It wasn��t clear what the correct answers are. i'd appreciate it if someone can quickly shed light on this. Thank you.
public class SyncTest
2. private int x;
3. private int y;
4. private synchronized void set X (int i) x=i;
5. private synchronized void set Y (int i) y=i;
6. public void setXY (int i) set X(i); set Y(i);
7. public synchronized boolean check() return X != Y;
8.
Under which conditions will check() return when called from a different class
A. check() can never return true
B. check() can return true when set XY is called by multiple threads
C. check() can true when multiple threads call set X and set Y separately
D. check() can only return true if synchTest is changed to allow x and y to be set
Separately
ans): a,d ???
Q2)
Conside below

public static void main(String args[] ){
X that = new X();
X hey = new X();
(new Thread(that)).start();
(new Thread(hey)).start();
}
public synchronized void run(){
for( ; ; ){
x++;
y++;
System.out.println("x=" + x + ", y=" +y);
try
{
Thread.sleep(1000);
}
catch(Exception e){}
}//end of for loop
}//end of run()
}//end of class
Output?
I thought by using synchronized run(), only one Object thread is run and that the other Object thread("hey) must wait till the first one("that") is finished. thus producing
1 1
2 2.. and so on..
but when i ran it, it output
1 1
1 1
2 2
2 2 ... Why? I think I'm getting very confused with the thread and how the idea of synchronization works..
Can Someone give a really Easy and Clear explanation of synchronization??? on methods and blocks??
Thank you so much!! =))


 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kevin goon:
A. check() can never return true
B. check() can return true when set XY is called by multiple threads
C. check() can true when multiple threads call set X and set Y separately
D. check() can only return true if synchTest is changed to allow x and y to be set
Separately
ans): a,d ???
Thank you so much!! =))


Hi Kevin, the code of the first question doesn't run, and i changed it to:
*********************************************
public class SyncTest {
int i=1;
private int x;
private int y;
boolean t;
public SyncTest(){
setX(i);
setY(i);
check();
}
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 void check() {
System.out.println(" The value is = "+ (x != y));
}
public static void main(String []a){
SyncTest b = new SyncTest();
}
}
i think a and d are correct. only setXY are public and thus can be invoked by clients, while setX and setY can only be used by setXY for their modifiers are private. Therefore, the values of x and y will always be equal for setXY is synchronized.
Experts come and confirm or correct me!!!
Someone else is needed to answer your second Q.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin, I'll stand by my response to your original post unless someone convinces me otherwise (http://www.javaranch.com/ubb/Forum24/HTML/010384.html)
I concur with William's answer to your second question -- you've created 2 separate objects for the 2 threads to act upon, so they are acting completely independently of each other and the synchonization of the run() method is irrelevant in this example. If you point both threads to either hey or that, then you would get the output you were expecting.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Appleton:
Kevin, I'll stand by my response to your original post unless someone convinces me otherwise (http://www.javaranch.com/ubb/Forum24/HTML/010384.html)
I concur with William's answer to your second question -- you've created 2 separate objects for the 2 threads to act upon, so they are acting completely independently of each other and the synchonization of the run() method is irrelevant in this example. If you point both threads to either hey or that, then you would get the output you were expecting.


Hi Scott,
First of all, I think the sample code and the wording of question1 is not very good. Anyway, it's ok.
Then I think I agree with you. B should be the correct answer. But it's hard to get 'true' output, isn't it? I tried and tried but all outputs are 'false' until I changed the setXY method a little bit (maybe it still outputs 'false' on other people's computers). Anyway, it has the potential to output 'true', right?
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);
try{
Thread.sleep(1000);
setY(i);
}
catch(InterruptedException ie) {}
}

public synchronized boolean check() {return x != y;}
}
class SyncTest1 {
static SyncTest st=new SyncTest();

static class T1 extends Thread {
public void run() {
st.setXY(1);
}
}

static class T2 extends Thread {
public void run() {
System.out.println(st.check());
}
}
public static void main(String[] args) {
new T1().start();
new T2().start();
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic