Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading q

 
kevin goon
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kevin
please if anyone can figure this one out, explanation would be greatly appreciated =)
thanks!!
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic