Java之【线程通信】--标志位练习2

定义一个线程A,输出1 ~ 10之间的整数,定义一个线程B,逆序输出1 ~ 10之间的整数,要求线程A和线程B交替输出

方法一:非标志位方法

package Homework;
//1  定义一个线程A,输出1 ~ 10之间的整数,定义一个线程B,逆序输出1 ~ 10之间的整数,要求线程A和线程B交替输出
public class Test1 {
    public static void main(String[] args){
        Object obj=new Object();
        A a=new A(obj);
        B b=new B(obj);
        a.start();
        b.start();
    }

}

class A extends Thread{
    Object obj;

    public A() {
        super();
    }

    public A(Object obj) {
        super();
        this.obj = obj;
    }

    //正序打印
    @Override
    public void run() {
        synchronized (obj) {
            for(int i=1;i<11;i++){
                System.out.print(i+",");
                obj.notify();
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }
}
class B extends Thread{
    Object obj;
    public B() {
        super();
    }

    public B(Object obj) {
        super();
        this.obj = obj;
    }

    //逆序打印
    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        synchronized (obj) {
            for(int i=10;i>0;i--){
                System.out.print(i+",");
                obj.notify();
                if(i>1){
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

    }
}

运行结果:
这里写图片描述

方法二:采用标志位

package Homework;
//1  定义一个线程A,输出1 ~ 10之间的整数,定义一个线程B,逆序输出1 ~ 10之间的整数,要求线程A和线程B交替输出
public class Test2 {
    public static void main(String[] args){
        C c=new C();
        A1 a1=new A1(c);
        B1 b1=new B1(c);
        a1.start();
        b1.start();
    }
}
class A1 extends Thread{
    C c;

    public A1(C c) {
        super();
        this.c = c;
    }

    public A1() {
        super();
    }
    @Override
    public void run() {

        synchronized (c) {
            for(int i=1;i<11;i++){
                if(c.flag==true){
                    try {
                        c.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                c.print(i);
                c.flag=true;
                c.notify();
            }
        }

    }

}
class B1 extends Thread{
    C c;

    public B1(C c) {
        super();
        this.c = c;
    }

    public B1() {
        super();
    }
    @Override
    public void run() {
        synchronized (c) {
            for(int i=10;i>0;i--){
                if(c.flag==false){
                    try {
                        c.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                c.print(i);
                c.flag=false;
                c.notify();
            }

        }

    }
}
class C {
    boolean flag=false;//true A已经打印,B为打印 A等待

    public void print(int i){
        System.out.print(i+",");
    }
}

运行结果
这里写图片描述

原文地址:https://www.cnblogs.com/TCB-Java/p/6797615.html