线程的死锁问题
- 死锁
- 不同的线程分别占用对方需要的同步资源不放弃,都在等对方放弃自己需要的同步资源,就形成了线程的死锁
- 出现死锁后,不会出现异常,不会出现提示,只是所有的线程都处于阻塞状态,无法继续
- 解决方法
- 专门的算、原则
- 尽量减少同步资源的定义
- 尽量避免嵌套同步

| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class WindowTest5 { |
| public static void main(String[] args) { |
| Window5 w = new Window5(); |
| Thread t1 = new Thread(w); |
| Thread t2 = new Thread(w); |
| Thread t3 = new Thread(w); |
| t1.start(); |
| t2.start(); |
| t3.start(); |
| } |
| |
| } |
| class Window5 implements Runnable{ |
| private int ticket = 100; |
| |
| private ReentrantLock lock = new ReentrantLock(); |
| @Override |
| public void run() { |
| while (true){ |
| try { |
| |
| lock.lock(); |
| try { |
| Thread.sleep(100); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| if (ticket>0){ |
| System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票"); |
| ticket--; |
| }else { |
| break; |
| } |
| } finally { |
| |
| lock.unlock(); |
| |
| } |
| } |
| } |
| } |
线程的通信
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class CommunicationTest { |
| public static void main(String[] args) { |
| Number n = new Number(); |
| Thread t1 = new Thread(n); |
| Thread t2 = new Thread(n); |
| t1.start(); |
| t2.start(); |
| } |
| } |
| class Number implements Runnable{ |
| private int number = 1; |
| @Override |
| public void run() { |
| |
| while (true){ |
| synchronized (this) { |
| notify(); |
| if (number<=100){ |
| try { |
| Thread.sleep(100); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println(Thread.currentThread().getName()+":"+number); |
| number++; |
| }else{ |
| break; |
| } |
| try { |
| |
| wait(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Clerk{ |
| private int productCount =0; |
| |
| public synchronized void produceProduct() { |
| if (productCount<20){ |
| productCount++; |
| System.out.println(Thread.currentThread().getName()+":开始生产第"+productCount+"产品"); |
| notify(); |
| }else { |
| try { |
| wait(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| public synchronized void consumeProduct() { |
| if (productCount>0){ |
| System.out.println(Thread.currentThread().getName()+":开始消费第"+productCount+"产品"); |
| productCount--; |
| notify(); |
| }else { |
| try { |
| wait(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| class Producer extends Thread{ |
| private Clerk clerk; |
| public Producer(Clerk clerk){ |
| this.clerk = clerk; |
| } |
| |
| @Override |
| public void run() { |
| System.out.println(getName()+":开始生产商品"); |
| while (true){ |
| try { |
| Thread.sleep(10); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| clerk.produceProduct(); |
| } |
| } |
| } |
| class Consumer extends Thread{ |
| private Clerk clerk; |
| |
| public Consumer(Clerk clerk) { |
| this.clerk = clerk; |
| } |
| |
| @Override |
| public void run() { |
| System.out.println(getName()+":开始消费产品"); |
| while (true){ |
| try { |
| Thread.sleep(10); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| clerk.consumeProduct(); |
| } |
| } |
| } |
| public class ProductTest { |
| public static void main(String[] args) { |
| Clerk clerk = new Clerk(); |
| Producer p1 = new Producer(clerk); |
| p1.setName("生产者1"); |
| Consumer c1 = new Consumer(clerk); |
| c1.setName("消费者1"); |
| p1.start(); |
| c1.start(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现