多种方法实现两个线程交替打印奇偶数

交替打印奇偶数

问题描述:

  •  构造两个线程,交替打印1-100之间的数字,其中线程1打印奇数,线程2打印偶数。 
    

方式1:采用synchronized同步锁和wait,notify线程通信机制来实现。

public class jiaotidayinshuzi {
    public static void main(String[] args){
        PrintNum p=new PrintNum();
        Thread td1=new Thread(p);
        Thread td2=new Thread(p);
        td1.setName("线程1");
        td2.setName("线程2");
        td1.start();
        td2.start();
    }
}
//定义打印函数,通过wait()和notify()交替打印奇偶数
class PrintNum implements Runnable{
    int num=1;
    @Override
    public void run() {
        synchronized (this){
            while (true){
                notify();//唤醒wait()的一个或者所有的线程
                if (num<=100){
                    System.out.println(Thread.currentThread().getName()+":"+num);
                    num++;
                }else {
                    break;
                }
                try {
                    wait();//释放当前的锁,另一个线程就进来
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

方式2:采用标志位来实现交替打印

将共享资源定义为类(counter),共享资源类中包含待打印的变量和标志位。

public class jiaotidayinshuzi02 {
    public static void main(String[] args) {
        Counter counter=new Counter();
        new Thread(new PrintOdd(counter),"线程1(打印奇数)").start();
        new Thread(new PrintEven(counter),"线程2(打印偶数)").start();
    }
}

class Counter {
    public int value = 1;
    public boolean odd = true;
}

class PrintOdd implements Runnable {
    public Counter counter;

    public PrintOdd(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        while (counter.value <= 100) {
            synchronized (counter) {
                if (counter.odd) {
                    System.out.println(Thread.currentThread().getName()+":"+counter.value);
                    counter.value++;
                    counter.odd = !counter.odd;
                    //唤醒打印偶数的线程
                    counter.notify();
                }
                try {
                    counter.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
class PrintEven implements Runnable{
    public  Counter counter;

    public PrintEven(Counter counter) {
        this.counter = counter;
    }

    @Override
    public void run() {
        while (counter.value<=100){
            synchronized (counter){
                if(!counter.odd){
                    System.out.println(Thread.currentThread().getName()+":"+counter.value);
                    counter.value++;
                    counter.odd=!counter.odd;
                    counter.notify();
                }
                try {
                    counter.wait();
                }catch (InterruptedException e){
                }
            }
        }
    }
}

方式3:不加锁,采用while和boolean标识位来实现

public class jiaotidayinshuzi03 {
    volatile static boolean open = true;
    volatile static int count = 1;
    static Thread td1 = new Thread(new PrintOdd(), "线程1(打印奇数)");
    static Thread td2 = new Thread(new PrintEven(), "线程2(打印偶数)");
    public static void main(String[] args) {
        td1.start();
        td2.start();
    }
    //定义打印奇数的类,实现Runnable接口,重写run()方法。静态内部类
    static class PrintOdd implements Runnable {

        @Override
        public void run() {
            while (count <= 100) {
                //通过标志位open来判断是否执行打印函数
                if (open) {
                    System.out.println(Thread.currentThread().getName() + ":" + count);
                    count++;
                    //执行完打印过程后变更标志位
                    open = false;
                }
            }
        }
    }
    //定义打印偶数的类,实现Runnable接口,重写run()方法
    static class PrintEven implements Runnable {

        @Override
        public void run() {
            while (count <= 100) {
                if (!open) {
                    System.out.println(Thread.currentThread().getName() + ":" + count);
                    count++;
                    open = true;
                }
            }
        }
    }
}


你可能感兴趣的:(Java开发,并发编程)