nerdexam
Oracle

1Z0-809 · Question #255

Given: ``java class Counter extends Thread { int i = 10; public synchronized void display(Counter obj) { try { Thread.sleep(5); obj.increment(this); System.out.println(i); } catch…

The correct answer is B. deadlock. You've hit your session limit · resets 5:20pm (America/New_York)

Question

Given:
class Counter extends Thread {
 int i = 10;
 public synchronized void display(Counter obj) {
 try {
 Thread.sleep(5);
 obj.increment(this);
 System.out.println(i);
 } catch (InterruptedException ex) { }
 }
 public synchronized void increment (Counter obj) {
 i++;
 }
}
public class Test {
 public static void main(String[] args) {
 final Counter obj1 = new Counter();
 final Counter obj2 = new Counter();
 new Thread(new Runnable() {
 public void run() { obj1.display(obj2); }
 }).start();
 new Thread(new Runnable() {
 public void run() { obj2.display(obj1); }
 }).start();
 }
}
From what threading problem does the program suffer?

Options

  • Arace condition
  • Bdeadlock
  • Cstarvation
  • Dlivelock

How the community answered

(22 responses)
  • A
    14% (3)
  • B
    77% (17)
  • C
    5% (1)
  • D
    5% (1)

Explanation

You've hit your session limit · resets 5:20pm (America/New_York)

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice