要求:
借助同步机制,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多线程--线程交替的更多相关文章

  1. Java多线程——线程之间的协作

    Java多线程——线程之间的协作 摘要:本文主要学习多线程之间是如何协作的,以及如何使用wait()方法与notify()/notifyAll()方法. 部分内容来自以下博客: https://www ...

  2. java 多线程—— 线程让步

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  3. java 多线程—— 线程等待与唤醒

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  4. Java多线程--线程及相关的Java API

    Java多线程--线程及相关的Java API 线程与进程 进程是线程的容器,程序是指令.数据的组织形式,进程是程序的实体. 一个进程中可以容纳若干个线程,线程是轻量级的进程,是程序执行的最小单位.我 ...

  5. Java多线程-线程的同步(同步方法)

    线程的同步是保证多线程安全访问竞争资源的一种手段.线程的同步是Java多线程编程的难点,往往开发者搞不清楚什么是竞争资源.什么时候需要考虑同步,怎么同步等等问题,当然,这些问题没有很明确的答案,但有些 ...

  6. Java多线程——线程的优先级和生命周期

    Java多线程——线程的优先级和生命周期 摘要:本文主要介绍了线程的优先级以及线程有哪些生命周期. 部分内容来自以下博客: https://www.cnblogs.com/sunddenly/p/41 ...

  7. Java多线程——线程的创建方式

    Java多线程——线程的创建方式 摘要:本文主要学习了线程的创建方式,线程的常用属性和方法,以及线程的几个基本状态. 部分内容来自以下博客: https://www.cnblogs.com/dolph ...

  8. Java多线程——线程的死锁

    Java多线程——线程的死锁 摘要:本文主要介绍了Java多线程中遇到的死锁问题. 部分内容来自以下博客: https://www.cnblogs.com/wy697495/p/9757982.htm ...

  9. Java多线程——线程之间的同步

    Java多线程——线程之间的同步 摘要:本文主要学习多线程之间是如何同步的,如何使用volatile关键字,如何使用synchronized修饰的同步代码块和同步方法解决线程安全问题. 部分内容来自以 ...

随机推荐

  1. ZooKeeper可以用来做什么(转)

    在ZooKeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...

  2. android程序在调试时出现了套接字异常“java.net.SocketException: Permission denied”该如何解决

    Socket不能对外连接,错误不会被报出,调试的时候,能看到Exception, 一般是抛出 java.net.socketexception permission denied这个异常.只要你的程序 ...

  3. firedac数据集控件的公共祖先类——TFDAdaptedDataSet

    firedac数据集控件的公共祖先类——TFDAdaptedDataSet TFDQuery = class(TFDCustomQuery)TFDCustomQuery = class(TFDRdbm ...

  4. CF #324 DIV2 C题

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  5. 一篇文章贯穿ACE各种发送接收组件 1.2版

    TCP通信过程介绍 首先介绍一下socket通信的基本过程:这里先如果有两个家伙在通信,一个是S.还有一个叫C (1)S打开port监听本地的port看看有没有人来连接: (2)与此同一时候C试图去连 ...

  6. erlang Unicode 处理

    最近在使用erlang做游戏服务器,而字符串在服务器编程中的地位是十分重要的,于是便想仔细研究下字符编码,以及erlang下的字符串处理.先从Unicode开始吧.... [Unicode] Unic ...

  7. Python3.4 远程操控电脑(开关机)

    import poplib import sys import smtplib from email.mime.text import MIMEText import os from email.he ...

  8. bzoj4737: 组合数问题

    终于过了肝了一天啊,怎么我最近都在做细节码农题啊 (这种水平NOIP凉凉??) luacs大家都可以想到用吧,一开始我的思路是把所有在p以内的%p==0的组合数预处理出来,那C(n/p,m/p)任取, ...

  9. 【POJ 2676】 Sudoku

    [题目链接] http://poj.org/problem?id=2676 [算法] 深度优先搜索 [代码] #include <algorithm> #include <bitse ...

  10. java jdk 管理工具

    官网:http://www.jenv.be/ 安装: Linux / OS X $ git clone https://github.com/gcuisinier/jenv.git ~/.jenv M ...