Java学习路线-16:多线程综合案例

举报
彭世瑜 发表于 2021/08/14 01:15:34 2021/08/14
2.3k+ 0 0
【摘要】 第6 章 : 多线程综合案例 23 数字加减 4个线程,2个线程加,2个线程减 循环出现 加一个,减一个 // 资源 class Resource { private int count = 0; // 为false可以增加,加完了设置为true, // 为true可以减少,减完了设置为false private boolean flag = false; pu...

第6 章 : 多线程综合案例

23 数字加减

4个线程,2个线程加,2个线程减
循环出现 加一个,减一个


// 资源
class Resource { private int count = 0; // 为false可以增加,加完了设置为true, // 为true可以减少,减完了设置为false private boolean flag = false; public synchronized void add() { if (this.flag == true) { try { super.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.count++; System.out.println(Thread.currentThread().getName() + " count=" + count); this.flag = true; super.notifyAll(); } public synchronized void sub() { if (this.flag == false) { System.out.println(this.flag); try { super.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.count--; System.out.println(Thread.currentThread().getName() + " count=" + count); this.flag = false; super.notifyAll(); }
}

// 加法线程
class AddThread implements Runnable { private Resource resource; public AddThread(Resource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { this.resource.add(); } }
}
// 减法线程
class SubThread implements Runnable { private Resource resource; public SubThread(Resource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { this.resource.sub(); } }
}
class Demo { public static void main(String[] args) { Resource resource = new Resource(); AddThread at = new AddThread(resource); SubThread st = new SubThread(resource); new Thread(at, "加法线程-A").start(); new Thread(at, "加法线程-B").start(); new Thread(st, "减法线程-X").start(); new Thread(st, "减法线程-Y").start(); }
}

  
 

并没有出现一加一减的现象

24 生产电脑

生产一台搬运一台

消费者生产者模型

class Computer{ private static int count; public Computer() { count++; } @Override public String toString() { return "电脑编号:" + count; }
}
class Resource{ private Computer computer; public synchronized void make(){ if(computer != null){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.computer = new Computer(); System.out.println("生产:" + this.computer); notifyAll(); } public synchronized void get(){ if(computer == null){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("取走:" + this.computer); this.computer = null; notifyAll(); }
}

class Producer implements Runnable{ private Resource resource; public Producer(Resource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { resource.make(); } }
}
class Consumer implements Runnable{ private Resource resource; public Consumer(Resource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { resource.get(); } }
}

class Demo{ public static void main(String[] args) { Resource resource = new Resource(); new Thread(new Producer(resource)).start(); new Thread(new Consumer(resource)).start(); }
}
  
 

25 竞争抢答

3个抢答线程,同时发出抢答指令
成功和失败都给与提示

有数据返回,采用Callable方式

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

class MyThread implements Callable<String> { private boolean flag = false; @Override public String call() throws Exception { synchronized (this) { // 如果没有人抢答成功则可以抢答 if (this.flag == false) { this.flag = true; return Thread.currentThread().getName() + "抢答成功!"; } else { return Thread.currentThread().getName() + "抢答失败!"; } } }
}

class Demo { public static void main(String[] args) throws Exception { MyThread t = new MyThread(); FutureTask<String> task1 = new FutureTask<String>(t); FutureTask<String> task2 = new FutureTask<String>(t); FutureTask<String> task3 = new FutureTask<String>(t); new Thread(task1, "抢答者A").start(); new Thread(task2, "抢答者B").start(); new Thread(task3, "抢答者C").start(); System.out.println(task1.get()); System.out.println(task2.get()); System.out.println(task3.get()); }
}

  
 

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/103059843

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。