要求描述
两个线程,其中一个线程打印奇数,另外一个线程打印偶数。两个线程交替打印,输入1,2,3,...,100基本思路
【两个线程交替打印奇偶】利用通知等待机制,第一个线程打印后,然后唤醒第二个线程,并释放锁。第二个线程执行同样的操作代码实现
package basicKnowledge.thread;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @基本功能:线程交替打印输出奇偶
* @program:summary
* @author:peicc
* @create:2019-09-26 09:11:15
**/
public class ThreadPrint {
staticObject object=new Object();
public static void main(String[] args) {
Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
synchronized (object){
for (int i = 1;
i <100 ;
i+=2) {
System.out.println(Thread.currentThread().getName()+": "+i);
object.notify();
//唤醒其他线程
try {
object.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
},"打印奇数线程");
Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
synchronized (object){
for (int i = 2;
i <=100 ;
i+=2) {
System.out.println(Thread.currentThread().getName()+": "+i);
object.notify();
//唤醒其他线程
try {
object.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
},"打印偶数线程");
thread1.start();
thread2.start();
}}
输出结果(后面的省略)
打印奇数线程: 1
打印偶数线程: 2
打印奇数线程: 3
打印偶数线程: 4
打印奇数线程: 5
打印偶数线程: 6
打印奇数线程: 7
打印偶数线程: 8
打印奇数线程: 9
打印偶数线程: 10
上述方法基本解决了我们的需求,但是有个问题,很严重,那就是打印结束后打印偶数的线程再次进入了等待状态,整个程序无法终止。
文章图片
代码升级版
package leetcode;
/**
* @基本功能:两个线程交替执行,一个输出偶数,一个输出奇数
* @program:summary
* @author:peicc
* @create:2019-07-29 12:56:49
**/
//线程执行完毕如何终止?
public class ThreadTurn {
static Object o=new Object();
static boolean flag=false;
//标志位public static void main(String[] args) {
ThreadTurn threadTurn=new ThreadTurn();
Thread oddThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized (threadTurn){
for (int i = 1;
i <100 ;
) {
if(!flag){
System.out.println("打印奇数线程"+i);
threadTurn.notify();
//唤醒等待线程
flag=true;
i+=2;
}else{
try {
threadTurn.wait();
//Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}}}
},"奇数线程");
Thread evenThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized (threadTurn){
for (int i = 2;
i <= 100;
) {
if(flag){
System.out.println("打印偶数线程"+i);
threadTurn.notify();
//唤醒等待线程
flag=false;
i+=2;
}else{
try {
threadTurn.wait();
//Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}}
}
}
},"偶数线程");
evenThread.start();
oddThread.start();
}
}
补充练习
两个线程,其中一个线程打印数字,另外一个线程打印字母。两个线程交替打印,输入1,2,a,3,4,b,5,6,c
package basicKnowledge.thread;
/**
* @基本功能:两个线程交替执行,一个输出偶数,一个输出奇数
* @program:summary
* @author:peicc
* @create:2019-07-29 12:56:49
**/
//线程执行完毕如何终止?
public class ThreadPrint2 {
static Object o=new Object();
static boolean flag=true;
//标志位public static void main(String[] args) {
ThreadPrint2 threadTurn=new ThreadPrint2();
Thread oddThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized (threadTurn){
int count=0;
for (int i = 1;
i <=52 ;
) {
if(count<2){
System.out.println("打印数字线程"+i);
threadTurn.notify();
//唤醒等待线程
count++;
i++;
}else{
try {count=0;
threadTurn.wait();
//Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}}}
},"奇数线程");
Thread evenThread=new Thread(new Runnable() {
@Override
public void run() {
synchronized (threadTurn){
for (int i = 0;
i <26;
) {
if(flag){
System.out.println("打印字母线程"+(char)(i+'a'));
threadTurn.notify();
//唤醒等待线程
flag=false;
i++;
}else{
try {flag=true;
threadTurn.wait();
//Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}}
}
}
},"偶数线程");
oddThread.start();
evenThread.start();
}
}
推荐阅读
- 并发编程|tomcat对AQS的扩展(使用LimitLatch控制连接数)
- java|java并发编程(顺序输出A、B、C循环10次)
- Java|LeetCode-1114. 按序打印(多线程)
- Java|LeetCode-1117. H2O 生成(多线程)
- Java|LeetCode-1116.打印零与奇偶数(多线程)
- JAVA8|Java8对于多线程并发的一些新支持-LongAdder
- 如何实现某个线程在其他线程执行完毕之后再执行?
- 编程题|多线程: 如何实现多个线程交替打印字符串?
- 浅谈ReentrantLock