Java之【线程通信】--标志位练习
生活随笔
收集整理的這篇文章主要介紹了
Java之【线程通信】--标志位练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
* 寫兩個線程,一個線程打印1-52,另一個線程答應字母A-Z。
* 打印順序為12A34B56C……5152Z。通過使用線程之間的通信協調關系。
- 注:分別給兩個對象構造一個對象o,數字每打印兩個或字母每打印一個就執行o.wait()。
- 在o.wait()之前不要忘了寫o.notify()
代碼:
方法一:直接寫
package Homework;public class Test2 {public static void main(String[] args) {O o=new O();Threadnum threadnum=new Threadnum(o);Threadabc threadabc=new Threadabc(o);Thread thread=new Thread(threadnum);Thread thread2=new Thread(threadabc);thread.start();thread2.start();} }//資源類線程O class O{public synchronized void num(){/*for(int i=1;i<=52;i++){System.out.print(i);if (i % 2 == 0) {this.notify();try {this.wait();} catch (InterruptedException e) {// TODO 待完善e.printStackTrace();}}*/for(int i=0;i<26;i++){ this.notify();for(int j=1+2*i;j<2*i+3;j++){System.out.print(j);}try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public synchronized void abc(){for(int i=65;i<91;i++){this.notify();System.out.print((char)(i));//終止程序,執行完最后一次,不需要在等待了if(i<90){try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}} }//打印數字線程 class Threadnum implements Runnable{O o;public Threadnum(O o){this.o=o;}@Overridepublic void run() {o.num();} }//打印字母線程 class Threadabc implements Runnable{O o;public Threadabc(O o){this.o=o;}@Overridepublic void run() {try {Thread.sleep(20);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}o.abc();} }運行結果:
方法二:采用標志位
資源類:
package com.qf.demo2; // 12A34B56C /*** 線程間通信 需要放在線程同步中* 1 資源類中方法 是同步的* 2 boolean標志 標志狀態* 3 方法里面 先判斷狀態(是否需要等待)* 4 如果不需要等待 執行 打印 加減操作* 5 執行完操作以后需要切換狀態* 6 喚醒對象線程 **/ public class Resource {boolean flag = true; // true 打印了字母還沒有打印數字 字母等著 數字打印// false 打印了數字了 還沒有打印字母 數字等著 字母打印// 先打印兩個數字public synchronized void printNum(int num){if(flag==false){// 打印了數字 還沒打印字母try {this.wait();// 數字等著} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// true 打印了字母 還沒打印數字 打印數字System.out.print(num+""+(num+1));// 1 12 34//已經打印完了數字了 切換狀態flag = false;//喚醒字母線程this.notify();}// 在打印一個字母public synchronized void printLetter(int letter){if(flag == true){//打印了字母還沒有打印數字 try {this.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// false 打印了數字還沒打印字母 打印字母System.out.print((char)(letter+65));// 打印完了字母了 ,需要切換狀態//切換成 打印完了字母,還沒打印數字 trueflag = true;// 喚醒對方線程this.notify();} }測試類:
package com.qf.demo2;public class Test {public static void main(String[] args) {Resource resource = new Resource();Number number = new Number(resource);Letter letter = new Letter(resource);Thread thread = new Thread(number);Thread thread2 = new Thread(letter);thread.start();thread2.start();} }class Number implements Runnable{Resource resource ;public Number(Resource resource) {this.resource = resource;}@Overridepublic void run() {for (int i = 1; i < 52; i+=2) {// 1 3 5 7 9 resource.printNum(i);}} }class Letter implements Runnable{Resource resource ;public Letter(Resource resource) {this.resource = resource;}@Overridepublic void run() {for (int i = 0; i < 26; i++) {resource.printLetter(i);}} }總結
以上是生活随笔為你收集整理的Java之【线程通信】--标志位练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java之线程同步练习
- 下一篇: Java之【线程通信】--标志位练习2