Threading q

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

posted Jun 19, 2001 02:42:00

0

hi kevin please if anyone can figure this one out, explanation would be greatly appreciated =) thanks!!

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???

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.

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