两个线程交替打印奇偶

要求描述

两个线程,其中一个线程打印奇数,另外一个线程打印偶数。两个线程交替打印,输入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(); } }


    推荐阅读