This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threading q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threading q" Watch "Threading q" New topic
Author

Threading q

kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
Can abstract method or class be static??
Also, Consider the following Thread coding1.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) setX(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 treads
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
Correct answer(s)??
kevin goon
Ranch Hand

Joined: Jun 12, 2001
Posts: 62
hi kevin
please if anyone can figure this one out, explanation would be greatly appreciated =)
thanks!!
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
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) setX(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
this question was discussed earlier.
A.check()can never return true
false,it can return true.
B.check() can return true when set XY is called by multiple treads //it can be true.since one thread can execute setXY method and in between stops, and another thread execute check method.
C.check() can true when multiple threads call set X and set Y separately //since setX and setY are private,cannot be accessed from another class
D.check() can only return true if synchTest is changed to allow x and y to be set separately //is it the only way???
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Suppose Thread A calls setXY and executes the first command, setX. It obtains the lock to the Object and sets the value of x. When it exits the setX method, it gives up the lock to the Object.
Now suppose that Thread b has been waiting to execute the check() method (because it is synchronized, and Thread A has the lock). If Thread B grabs the lock as soon as Thread A returns from the setX method, Thread B can execute the check method. At this point in time, x != y may well return true (assuming that the parameter i passed to the setXY method in Thread A was not the same value as x and y already had).
The closest correct answer is B (the above shows A and D to be incorrect, and C can't be correct as long as the methods are private). However, in this example it wasn't even necessary to have multiple threads call setXY(). Even a single thread calling setXY() while another thread is calling check() can produce a true result.
As stated, B is still true even though the part about multiple threads is a red herring, so I would go with that one on the exam.
April.Johnson
Ranch Hand

Joined: May 02, 2001
Posts: 48
To address the first question:
Abstract methods can't be final, static, native, or synchronized.
Top-level classes cannot be static. Inner classes can be so that they can be accessed without having to have an instance of the enclosing class.
April
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threading q