要求:
借助同步机制,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个线程的有序交替执行
---------------------

最新文章

  1. JVM虚拟机结构
  2. Android自定义控件7--自定义开关--绘制界面内容
  3. tablediff使用方法
  4. add number
  5. Apache+Tomcat构建Tomcat负载均衡集群
  6. UWP开发中的流媒体
  7. jquery 设置css样式
  8. switch… case 语句的用法(二)
  9. Excel 代码
  10. JavaScript高级程序设计之location对象
  11. PHP生成验证码
  12. Spring Boot 文件上传原理
  13. TortoiseSVN--clearup清理失败解决办法
  14. xss的一般防护措施(及CreateDefaultBuilder源码)
  15. C# WebSocket
  16. SQL注入之Sqli-labs系列第四十七关,第四十八关,第四十九关(ORDER BY注入)
  17. Python Django 实用小案例2
  18. MyBatis(四):mybatis中使用in查询时的注意事项
  19. 51job_selenium测试2
  20. 26.如何获得select被选中option的value和text

热门文章

  1. group by语句,聚合函数与其他语句(关系,执行过程)
  2. [bzoj1342][Baltic2007]Sound静音问题_单调队列
  3. Javascript如何实现继承?
  4. 高仿京东APP首页“京东快报”自己主动向上滚动的广告条
  5. [转]supervisor 安装、配置、常用命令
  6. Android之QQ授权登录获取用户信息
  7. @Repository @Service 和@Autowired 的使用
  8. Extjs TabPanel页签转换事件
  9. mysql-5.5 for linux源码安装
  10. pattern matching is C# 7.0