Welcome Guest | Sign in | Register
Threads - Discussion Page For Q.2954 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Threads > Questions and Answers

Q: What is the output for the below code ?

class A extends Thread{
int count = 0;
public void run(){
System.out.println("run");
synchronized (this) {
for(int i =0; i < 50 ; i++){
count = count + i;
}
notify();
}
}
}
public class Test{
public static void main(String argv[]) {
A a = new A();
a.start();
synchronized (a) {
System.out.println("waiting");
try{
a.wait();
}catch(InterruptedException e){
}
System.out.println(a.count);
}
}

A. waiting run 1225 B. waiting run 0
C. waiting run and count can be anything D. Compilation fails

Answer and Explanation

Answer:waiting run 1225

Explanation:
a.wait(); put thread on wait until not get notifed. A thread gets on this waiting list by
executing the wait() method of the target object. It doesn't execute any further
instructions until the notify() method of the target object is called. A thread to call wait() or notify(), the thread has to be the owner of the lock for that object.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.