java基础---多线程之交替打印,等待唤醒机制
生活随笔
收集整理的這篇文章主要介紹了
java基础---多线程之交替打印,等待唤醒机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對于交替打印的線程問題:
方法1:非標志方法
package com.exam_1; /* 定義一個線程A,輸出1 ~ 10之間的整數,定義一個線程B,逆序輸出1 ~ 10之間的整數(10)要求線程A和線程B交替輸出*/ /* 非標志位方法*/ public class Demo04_pp {public static void main(String[] args){Object obj=new Object();//定義一個對象obj//將此對象分別傳給A,B,也就是說ab共用一個對象A2 a=new A2(obj);B2 b=new B2(obj);//啟動線程a.start();b.start();} }class A2 extends Thread{Object obj;public A2() {super();}//定義obj的構造方法public A2(Object obj) {super();this.obj = obj;}//正序打印@Overridepublic void run() {//此時obj就是主函數中定義的objsynchronized (obj) {for(int i=1;i<11;i++){System.out.print(i+",");obj.notify();//第一次程序走到這里時,這個并沒有什么用。try {//obj - B等待obj.wait();} catch (InterruptedException e) {e.printStackTrace();}}}} } class B2 extends Thread{Object obj;public B2() {super();}public B2(Object obj) {super();this.obj = obj;}//逆序打印@Overridepublic void run() {try {//先讓B的run方法sleep,這樣最先調用的就是A的run方法Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}//B中的obj也是主函數中定義的objsynchronized (obj) {for(int i=10;i>0;i--){System.out.print(i+",");obj.notify();//喚醒在此對象監視器上等待的單個線程。此時在這個對象監視器上等待的線程即為Aif (i > 1) {try {obj.wait();//當輸出一個數是將此線程進入wait狀態,這是A線程再進來} catch (InterruptedException e) {e.printStackTrace();}}}}} } package com.henu;import java.awt.font.NumericShaper;/* 1. 創建兩個線程,其中一個輸出1-52,另外一個輸出A-Z。輸出格式要求:12A 34B 56C 78D*/ public class Test01 {public static void main(String[] args) {Object obj = new Object();Number n = new Number(obj);Letter l = new Letter(obj);n.start();l.start();} }class Number extends Thread{Object obj;public Number() {}public Number(Object obj) {this.obj = obj;}@Overridepublic void run() {synchronized (obj){int i = 1;while(i <= 52){System.out.print(i);System.out.print(i+1);i += 2;obj.notify();try {obj.wait();} catch (InterruptedException e) {e.printStackTrace();}}}} }class Letter extends Thread{Object obj;public Letter() {}public Letter(Object obj) {this.obj = obj;}@Overridepublic void run() {try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}synchronized(obj){for (int i = 65; i <= 90; i++) {System.out.print((char) i+",");obj.notify();if (i < 90) {try {obj.wait();} catch (InterruptedException e) {e.printStackTrace();}}}}} }?2.采用標志方法
package com.henu; /* 編寫程序實現,子線程循環3次,接著主線程循環5次,接著再子線程循環3次,主線程循環5次,如此反復,循環3次.*/ public class Test03 {public static void main(String[] args) {final ThreadFunction2 f2 = new ThreadFunction2();// 子線程循環3次new Thread(new Runnable(){public void run(){for(int i=0;i<3;i++){f2.subFunction();}}}).start();// 主線程循環3次for(int i=0;i<3;i++){f2.mainFunction();}}}// 編寫功能類,實現子線程和主線程的功能 class ThreadFunction2 {private boolean flag = false;// 主線程要實現的功能public synchronized void mainFunction(){while(!flag){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}for(int i=0;i<5;i++){System.out.println("mainFunction"+i);}this.notify();flag = false;}// 子線程要實現的功能public synchronized void subFunction(){while(flag){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}for(int i=0;i<3;i++){System.out.println("subFunction"+i);}this.notify();flag = true;}}?
總結
以上是生活随笔為你收集整理的java基础---多线程之交替打印,等待唤醒机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础---Calendar类
- 下一篇: java线程----生产者和消费者问题