From what threading problem does the program suffer?
A.
deadlock
B.
livelock
C.
starvation
D.
race condition
Explanation:
A thread often acts in response to the action of another thread. If the other thread’s action is also a
response tothe action of another thread, then livelock may result. As with deadlock, livelockedthreads are unable to makefurther progress.
However, the threads are not blocked — they are simply too busy responding to each other to
resume work.
This is comparable to two people attempting to pass each other in a corridor: Alphonse moves to
his left to let
Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still
blocking eachother, Alphone moves to his right, while Gaston moves to his left. They’restill
blocking each other, so.
I would vote for A.
I think the answer is A too, deadlock.
In this blog there’s a explanation:
http://1z0-804.blogspot.com.br/2013/11/q31.html
Indeed it’s A.
A livelock is something that is very different from what is shown here.
/**
*
* @author Humberto
*/
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();
}
}
The threads should be still running in livelock. In this case, both threads are waiting each other to proceed. Thus, it should be deadlock
These threads have lock in different objects so Deadlock should not happen.
only B is correct.
A
https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
public static void main(String[] args) {
final Friend alphonse =
new Friend(“Alphonse”);
final Friend gaston =
new Friend(“Gaston”);
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}