Java学习路线-16:多线程综合案例
【摘要】 第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(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
并没有出现一加一减的现象
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(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
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()); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/103059843
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)