C
C
Core Java
Search…
Livelock
Imagine you’re talking with your friend on your mobile phone and the call drops. You try to reconnect with her, but her phone is busy (because she’s trying to call you!). You wait for a few moments and try to reconnect, only to discover that her phone is still busy, because she waited for exactly the same duration before trying to reconnect again. If you compare yourself and your friend with threads, you both are in a livelock. You and your friend aren’t blocked—both of you are responding but aren’t being able to do what you both intend to (talk with each other).

Threads in a livelock aren’t blocked; they’re responding to each other, but they aren’t able to move to completion.

Because a livelock depends on the exact time of execution of threads, the same set of threads might not always livelock.
Livelock describes situation where two threads are busy responding to actions of each other. They keep repeating a particular code so the program is unable to make further progress:
Thread 1 acts as a response to action of thread 2
Thread 2 acts as a response to action of thread 1
Unlike deadlock, threads are not blocked when livelock occurs. They are simply too busy responding to each other to resume work. In other words, the program runs into an infinite loop and cannot proceed further.

A Livelock Example:

1)
Let’s see an example: a criminal kidnaps a hostage and he asks for ransom in order to release the hostage. A police agrees to give the criminal the money he wants once the hostage is released. The criminal releases the hostage only when he gets the money. Both are waiting for each other to act first, hence livelock.
Here’s the code of this example.
Criminal class:
1
package com.gs.iilp.corejava.threads;
2
3
class Criminal {
4
private boolean hostageReleased = false;
5
6
public boolean isHostageReleased() {
7
return hostageReleased;
8
}
9
10
public void releaseHostage(Police police) {
11
while (!police.isMoneySent()) {
12
13
System.out.println("Criminal: waiting police to give ransom");
14
15
try {
16
Thread.sleep(1000);
17
} catch (InterruptedException ex) {
18
ex.printStackTrace();
19
}
20
}
21
22
System.out.println("Criminal: released hostage");
23
24
this.hostageReleased = true;
25
}
26
27
}
28
29
class Police {
30
private boolean moneySent = false;
31
32
public boolean isMoneySent() {
33
return moneySent;
34
}
35
36
public void giveRansom(Criminal criminal) {
37
38
while (!criminal.isHostageReleased()) {
39
40
System.out.println("Police: waiting criminal to release hostage");
41
42
try {
43
Thread.sleep(1000);
44
} catch (InterruptedException ex) {
45
ex.printStackTrace();
46
}
47
}
48
49
System.out.println("Police: sent money");
50
51
this.moneySent = true;
52
}
53
54
}
55
56
public class HostageRescueLiveLock {
57
58
static final Police police = new Police();
59
60
static final Criminal criminal = new Criminal();
61
62
public static void main(String[] args) {
63
new Thread(() -> {
64
police.giveRansom(criminal);
65
}).start();
66
67
new Thread(() -> {
68
criminal.releaseHostage(police);
69
}).start();
70
}
71
72
}
Copied!

2)

1
package com.gs.corejava.threads;
2
3
class Person_1 {
4
5
private boolean isBusy;
6
7
public boolean isBusy() {
8
return isBusy;
9
}
10
11
public void makeCall(Person_2 person_2) {
12
while (!person_2.isBusy()) {
13
System.out.println("Person 1 is calling Person 2 ...");
14
try {
15
Thread.sleep(1000);
16
} catch (InterruptedException e) {
17
e.printStackTrace();
18
}
19
}
20
21
// person 1 is free now
22
isBusy = true;
23
}
24
}
25
26
class Person_2 {
27
private boolean isBusy;
28
29
public boolean isBusy() {
30
return isBusy;
31
}
32
33
public void makeCall(Person_1 person_1) {
34
while (!person_1.isBusy()) {
35
System.out.println("Person 2 is calling Person 1 ...");
36
try {
37
Thread.sleep(1000);
38
} catch (InterruptedException e) {
39
e.printStackTrace();
40
}
41
}
42
// person 2 is free now
43
isBusy = true;
44
}
45
}
46
47
public class LiveLockExample1 {
48
49
static final Person_2 PERSON_2 = new Person_2();
50
static final Person_1 PERSON_1 = new Person_1();
51
52
public static void main(String[] args) {
53
new Thread(new Runnable() {
54
55
@Override
56
public void run() {
57
PERSON_1.makeCall(PERSON_2);
58
}
59
}).start();
60
new Thread(new Runnable() {
61
62
@Override
63
public void run() {
64
PERSON_2.makeCall(PERSON_1);
65
}
66
}).start();
67
}
68
}
69
Copied!
Last modified 2yr ago
Copy link