Java中的死锁和饥饿

僵局 :死锁是两个线程相互等待,等待永远不会结束的情况。在这里,两个线程都不能完成它们的任务。

null

JAVA

// Java program to illustrate Deadlock situation
class DeadlockDemo extends Thread {
static Thread mainThread;
public void run()
{
System.out.println( "Child Thread waiting for" +
" main thread completion" );
try {
// Child thread waiting for completion
// of main thread
mainThread.join();
}
catch (InterruptedException e) {
System.out.println( "Child thread execution" +
" completes" );
}
}
public static void main(String[] args)
throws InterruptedException
{
DeadlockDemo.mainThread = Thread.currentThread();
DeadlockDemo thread = new DeadlockDemo();
thread.start();
System.out.println( "Main thread waiting for " +
"Child thread completion" );
// main thread is waiting for the completion
// of Child thread
thread.join();
System.out.println( "Main thread execution" +
" completes" );
}
}


输出:

Main thread waiting for Child thread completionChild Thread waiting for main thread completion

饥饿 : 在饥饿中,线程也在互相等待。但在这里,等待时间并不是无限的,在一段时间间隔之后,等待线程总是获得执行thread run()方法所需的任何资源。

JAVA

// Java program to illustrate Starvation concept
class StarvationDemo extends Thread {
static int threadcount = 1 ;
public void run()
{
System.out.println(threadcount + "st Child" +
" Thread execution starts" );
System.out.println( "Child thread execution completes" );
threadcount++;
}
public static void main(String[] args)
throws InterruptedException
{
System.out.println( "Main thread execution starts" );
// Thread priorities are set in a way that thread5
// gets least priority.
StarvationDemo thread1 = new StarvationDemo();
thread1.setPriority( 10 );
StarvationDemo thread2 = new StarvationDemo();
thread2.setPriority( 9 );
StarvationDemo thread3 = new StarvationDemo();
thread3.setPriority( 8 );
StarvationDemo thread4 = new StarvationDemo();
thread4.setPriority( 7 );
StarvationDemo thread5 = new StarvationDemo();
thread5.setPriority( 6 );
thread1.start();
thread2.start();
thread3.start();
thread4.start();
// Here thread5 have to wait because of the
// other thread. But after waiting for some
// interval, thread5 will get the chance of
// execution. It is known as Starvation
thread5.start();
System.out.println( "Main thread execution completes" );
}
}


输出:

Main thread execution starts1st Child Thread execution startsChild thread execution completes2st Child Thread execution startsChild thread execution completes3st Child Thread execution startsChild thread execution completes4st Child Thread execution startsChild thread execution completesMain thread execution completes5st Child Thread execution startsChild thread execution completes

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享