死锁:共享资源的循环等待,引起的线程无限等待。
解决死锁的方法:将环破解。
class Book{}
class Pen{}
public class Test {
private static Book book=new Book();
private static Pen pen =new Pen();
public static void main(String[] args) {
Thread bookThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized(book) {
System.out.println("我是书,给我笔!");
synchronized (pen) {
System.out.println("我想要书");
}
}
}
});
Thread penThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized(pen) {
System.out.println("我是笔,给我书!");
synchronized (book) {
System.out.println("我想要笔");
}
}
}
});
bookThread.start();
penThread.start();
}
}
博客介绍了死锁是共享资源循环等待导致线程无限等待的情况,同时给出解决死锁的方法是将环破解,聚焦于信息技术中死锁相关知识。
708

被折叠的 条评论
为什么被折叠?



