Deadlock Livelock thread starvation
2014-03-29 02:11
453 查看
public class Deadlock {
public static void main(String[ ] args) {
// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
int a=0;
// Here's the first thread. It tries to lock resource1 then resource2
Thread t1 = new Thread( ) {
public void run( ) {
// Lock resource 1
synchronized(resource1) {
System.out.println("Thread 1: locked resource 1");
// Pause for a bit, simulating some file I/O or
// something. Basically, we just want to give the
// other thread a chance to run. Threads and deadlock
// are asynchronous things, but we're trying to force
// deadlock to happen here...
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Now wait 'till we can get a lock on resource 2
synchronized(resource2) {
while(true){
System.out.println("Thread 1: locked resource 2");
try { Thread.sleep(10000); }
catch (InterruptedException e) { }
}
}
}
}
};
// Here's the second thread. It tries to lock resource2 then resource1
Thread t2 = new Thread( ) {
public void run( ) {
// This thread locks resource 2 right away
synchronized(resource2) {
System.out.println("Thread 2: locked resource 2");
// Then it pauses, just like the first thread.
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Then it tries to lock resource1. But wait! Thread
// 1 locked resource1, and won't release it 'till it
// gets a lock on resource2. This thread holds the
// lock on resource2, and won't release it 'till it
// gets resource1. We're at an impasse. Neither
// thread can run, and the program freezes up.
synchronized(resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
// Start the two threads. If all goes as planned, deadlock will occur,
// and the program will never exit.
t1.start( );
t2.start( );
}
}
Livelock!
A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further 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 each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so...
public class Livelock
{
static class Spoon
{
private Diner owner;
public Spoon(Diner d) { owner = d; }
public Diner getOwner() { return owner; }
public synchronized void setOwner(Diner d) { owner = d; }
public synchronized void use() { System.out.printf("%s has eaten!", owner.name); }
}
static class Diner
{
private String name;
private boolean isHungry;
public Diner(String n) { name = n; isHungry = true; }
public String getName() { return name; }
public boolean isHungry() { return isHungry; }
public void eatWith(Spoon spoon, Diner spouse)
{
while (isHungry)
{
// Don't have the spoon, so wait patiently for spouse.
if (spoon.owner != this)
{
try { Thread.sleep(1); } catch(InterruptedException e) { continue; }
continue;
}
// If spouse is hungry, insist upon passing the spoon.
if (spouse.isHungry())
{
System.out.printf("%s: You eat first my darling %s!%n", name, spouse.getName());
spoon.setOwner(spouse);
continue;
}
// Spouse wasn't hungry, so finally eat
spoon.use();
isHungry = false;
System.out.printf("%s: I am stuffed, my darling %s!%n", name, spouse.getName());
spoon.setOwner(spouse);
}
}
}
public static void main(String[] args)
{
final Diner husband = new Diner("Bob");
final Diner wife = new Diner("Alice");
final Spoon s = new Spoon(husband);
new Thread(new Runnable() { public void run() { husband.eatWith(s, wife); } }).start();
new Thread(new Runnable() { public void run() { wife.eatWith(s, husband); } }).start();
}
}
Starvation
Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.
public static void main(String[ ] args) {
// These are the two resource objects we'll try to get locks for
final Object resource1 = "resource1";
final Object resource2 = "resource2";
int a=0;
// Here's the first thread. It tries to lock resource1 then resource2
Thread t1 = new Thread( ) {
public void run( ) {
// Lock resource 1
synchronized(resource1) {
System.out.println("Thread 1: locked resource 1");
// Pause for a bit, simulating some file I/O or
// something. Basically, we just want to give the
// other thread a chance to run. Threads and deadlock
// are asynchronous things, but we're trying to force
// deadlock to happen here...
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Now wait 'till we can get a lock on resource 2
synchronized(resource2) {
while(true){
System.out.println("Thread 1: locked resource 2");
try { Thread.sleep(10000); }
catch (InterruptedException e) { }
}
}
}
}
};
// Here's the second thread. It tries to lock resource2 then resource1
Thread t2 = new Thread( ) {
public void run( ) {
// This thread locks resource 2 right away
synchronized(resource2) {
System.out.println("Thread 2: locked resource 2");
// Then it pauses, just like the first thread.
try { Thread.sleep(50); }
catch (InterruptedException e) { }
// Then it tries to lock resource1. But wait! Thread
// 1 locked resource1, and won't release it 'till it
// gets a lock on resource2. This thread holds the
// lock on resource2, and won't release it 'till it
// gets resource1. We're at an impasse. Neither
// thread can run, and the program freezes up.
synchronized(resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
// Start the two threads. If all goes as planned, deadlock will occur,
// and the program will never exit.
t1.start( );
t2.start( );
}
}
Livelock!
A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further 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 each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so...
public class Livelock
{
static class Spoon
{
private Diner owner;
public Spoon(Diner d) { owner = d; }
public Diner getOwner() { return owner; }
public synchronized void setOwner(Diner d) { owner = d; }
public synchronized void use() { System.out.printf("%s has eaten!", owner.name); }
}
static class Diner
{
private String name;
private boolean isHungry;
public Diner(String n) { name = n; isHungry = true; }
public String getName() { return name; }
public boolean isHungry() { return isHungry; }
public void eatWith(Spoon spoon, Diner spouse)
{
while (isHungry)
{
// Don't have the spoon, so wait patiently for spouse.
if (spoon.owner != this)
{
try { Thread.sleep(1); } catch(InterruptedException e) { continue; }
continue;
}
// If spouse is hungry, insist upon passing the spoon.
if (spouse.isHungry())
{
System.out.printf("%s: You eat first my darling %s!%n", name, spouse.getName());
spoon.setOwner(spouse);
continue;
}
// Spouse wasn't hungry, so finally eat
spoon.use();
isHungry = false;
System.out.printf("%s: I am stuffed, my darling %s!%n", name, spouse.getName());
spoon.setOwner(spouse);
}
}
}
public static void main(String[] args)
{
final Diner husband = new Diner("Bob");
final Diner wife = new Diner("Alice");
final Spoon s = new Spoon(husband);
new Thread(new Runnable() { public void run() { husband.eatWith(s, wife); } }).start();
new Thread(new Runnable() { public void run() { wife.eatWith(s, husband); } }).start();
}
}
Starvation
Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.
相关文章推荐
- deadlock vs livelock vs Starvation
- 死锁(deadlock)、活锁(livelock)与饿死(starvation)
- The Java™ Tutorials — Concurrency :Starvation and Livelock 饥饿和活锁
- CSharp - Working principles about Thread, Process, Multithreading, DeadLock, Lock
- Thread详解13:ReentrantLock的用法(一)
- UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
- Oracle 9i 升级中的bug-- Sys.Cdc_alter_ctable_before ORA-04020 deadlock detected while trying to lock object
- 构建多线程应用程序(Thread、lock锁定范围、Monitor、Interlocked、[Synchronization]、TimerCallback、ThreadPool线程池)
- Thread State and the Global Interpreter Lock
- uvalive 6600 - Spanning trees in a secure lock pattern
- Thread.sleep() 是否释放ReentrantLock?
- 化零为整WCF(13) - 并发控制(锁)(Mutex, Semaphore, Monitor, Lock, ThreadPool, Interlocked, ReaderWriterLock)
- Tried to obtain the web lock from a thread解决方法之一
- Thread and shared lock
- mysql 死锁 Deadlock found when trying to get lock; t
- 【解决】iOS开发,更新界面时崩溃,Tried to obtain the web lock from a thread other than the main thread or ……
- SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting
- Thread lock_guard 和 unique_lock
- boost库中thread多线程详解3——细说lock_guard
- 《多线程编程》学习之二:Thread.currentThread()、isAlive()、Thread.sleep()的使用