Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads

 
Prasanna Vijaya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
these are the questions from a mock exam. i'm unable to find the right answer. can any body help me find right answers and with little explanation on how it works.
Question no 1
class A {
private static int x = 0;
synchronized int increment() {
int n=x;
x = n + 1;
return n;
}
}
Is class A thread-safe? Why or why not?
Choice 1
No, because increment() operates on variables outside its scope
Choice 2
No, because "x" is static, and vulnerable to threads operating on different instances
Choice 3
Yes, because its only method, increment(), must acquire the object's monitor
Choice 4
Yes, because increment() is synchronized
Choice 5
Yes, because "x" is private and not available to outside threads
question no 2
How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object?
Choice 1
place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor
Choice 2
use Thread.wait() and Thread.notify()
Choice 3
dynamically control the priorities of the threads within the method
Choice 4
use Thread.yield() to switch between threads at set points
Choice 5
use Thread.sleep()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic