Java多线程--线程交替
要求:
借助同步机制,sleep()方法,join()方法,实现动画显示;
甲线程:1、3、5、7、9
乙线程:2、4、6、8、10
丙线程:a、b、c、d、e
main()线程输出:线程开始,线程结束
输出结果:线程开始,1-a-2## 3-b-4## 5-c-6## …
思考:
使用多个判断标记,模拟(消费者-生产者)每线程输出一个后就等待,然后改变自己的标记
临界资源–使用多个== putX() == 方法,判断属于自己的标记(== isEmptyX ==)然后输出
使多个线程有序的交替执行
代码:
class Resource{
private boolean isEmpty01 = true;
private boolean isEmpty02 = false;
private boolean isEmpty03 = false;
//每个put方法对应一个输出,每输出一个就等待,等待其他人的唤醒
public void put1(){
while(!isEmpty01){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
//输出后
isEmpty01 = false;
isEmpty02 = true;
notifyAll();
}
public void put2(){
while(!isEmpty02){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
isEmpty02 = false;
isEmpty03 = true;
notifyAll();
}
public void put3(){
while(!isEmpty03){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
isEmpty03 = false;
isEmpty01 = true;
notifyAll();
}
}
class Player01 implements Runnable{
private Resource res;
private String[] arr;
Player01(){}
Player01(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}
public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
//错误的点
//61,62,这两句不能交换顺序
res.put1();
System.out.print(arr[i]+"-");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
class Player02 implements Runnable{
private Resource res;
private String[] arr;
Player02(){}
Player02(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}
public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
res.put2();
System.out.print(arr[i]+"-");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
class Player03 implements Runnable{
private Resource res;
private String[] arr;
Player03(){}
Player03(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}
public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
res.put3();
System.out.print(arr[i]+"## ");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
class Test08{
public static void main(String[] args){
String[] arr1 = {"1","3","5","7","9"};
String[] arr2 = {"a","b","c","d","e"};
String[] arr3 = {"2","4","6","8","0"};
Resource res = new Resource();
Player01 p1 = new Player01(arr1,res);
Player02 p2 = new Player02(arr2,res);
Player03 p3 = new Player03(arr3,res);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(p3);
t1.start();
t2.start();
t3.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
执行结果:
重要的是:
这种利用标记可以实现超过2个线程的有序交替执行
---------------------
Java多线程--线程交替的更多相关文章
- Java多线程——线程之间的协作
Java多线程——线程之间的协作 摘要:本文主要学习多线程之间是如何协作的,以及如何使用wait()方法与notify()/notifyAll()方法. 部分内容来自以下博客: https://www ...
- java 多线程—— 线程让步
java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...
- java 多线程—— 线程等待与唤醒
java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...
- Java多线程--线程及相关的Java API
Java多线程--线程及相关的Java API 线程与进程 进程是线程的容器,程序是指令.数据的组织形式,进程是程序的实体. 一个进程中可以容纳若干个线程,线程是轻量级的进程,是程序执行的最小单位.我 ...
- Java多线程-线程的同步(同步方法)
线程的同步是保证多线程安全访问竞争资源的一种手段.线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源.什么时候需要考虑同步,怎么同步等等问题,当然,这些问题没有很明确的答案,但有些 ...
- Java多线程——线程的优先级和生命周期
Java多线程——线程的优先级和生命周期 摘要:本文主要介绍了线程的优先级以及线程有哪些生命周期. 部分内容来自以下博客: https://www.cnblogs.com/sunddenly/p/41 ...
- Java多线程——线程的创建方式
Java多线程——线程的创建方式 摘要:本文主要学习了线程的创建方式,线程的常用属性和方法,以及线程的几个基本状态. 部分内容来自以下博客: https://www.cnblogs.com/dolph ...
- Java多线程——线程的死锁
Java多线程——线程的死锁 摘要:本文主要介绍了Java多线程中遇到的死锁问题. 部分内容来自以下博客: https://www.cnblogs.com/wy697495/p/9757982.htm ...
- Java多线程——线程之间的同步
Java多线程——线程之间的同步 摘要:本文主要学习多线程之间是如何同步的,如何使用volatile关键字,如何使用synchronized修饰的同步代码块和同步方法解决线程安全问题. 部分内容来自以 ...
随机推荐
- BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
http://www.lydsy.com/JudgeOnline/problem.php?id=2097 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: ...
- BZOJ——T 4563: [Haoi2016]放棋子
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 387 Solved: 247[Submit][Status][Discuss] Descriptio ...
- 搭建strom 的开发环境 - local mode
Setting Up a Development Environment This page outlines what you need to do to get a Storm developme ...
- apache禁止訪问某些文件或文件夹的方法
[apache配置禁止訪问] 1. 禁止訪问某些文件/文件夹 添加Files选项来控制,比方要不同意訪问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$&q ...
- 改动虚拟机镜像的rootpassword
有时从网上下载的虚拟机镜像.没有rootpassword,必须通过秘钥登录.然后秘钥又须要麻烦的注入到里面去.想用,却无法登录.非常头痛.本文提供一种通过改动虚拟机镜像里面的/etc/shadow文件 ...
- linux下查看监听port相应的进程
使用netstat查看进程PID [root@test ~]# netstat -anp|grep 5001 tcp 0 0 :::5001 :::* LISTEN 12886/java 之后各位看官 ...
- C++常用字符串分割方法实例汇总
投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-08我要评论 这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数.STL.Boost等常用 ...
- hdu 1213(并查集模版题)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- svn for vs
现在官方下载需要注册一堆的东西,为方便群众使用在这里提供一个新版的下载. http://files.cnblogs.com/wfcfan/AnkhSvn-2.5.12266.rar
- Cent OS 6/7 中通过yum安装软件时提示cannot find a valid baseurl...的解决方法
目录 1 问题描述 2 解决方法一 (Cent OS 7中有效) 3 解决方法二 (Cent OS 7中无效) 1 问题描述 新申请了虚拟机, 系统版本是Cent OS 7.2. 在安装软件的过程中, ...