Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/java-thread-wait-notify-usage
Question 240 / 297:  What is the output of running the following program?
class Tester extends Thread {

int total;

public static void main(String[] args) throws Exception {

Tester t = new Tester();

t.start();

System.out.println("hi how are you:");

synchronized (t) {

System.out.println("waiting for t to complete");

t.wait();

System.out.println("total" + t.total);

}

}

synchronized public void run() {

for (int i = 0; i < 3; i++) {

total = total + i;

}

}

}

A  main thread will wait indefinitely.
B  total 0 is part of the output.
C  total 3 is part of the output .
D  it depends upon which thread has got the cpu first .
<< First < Previous Next > Last >>
Explanation:

It depends on the cpu which thread will be executed first.

also whenever a thread completes it call to notifyAll() so that all the waiting thread can get into.

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Madison Christian
Start Exam
Dionne Mahaffey
Start Quiz
Keyaira Braxton
Start Exam
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>