交替打印数字 字母的两种通用方法

(一)Object的notifyAll和wait方法

public class AlternantPrint {
    private static boolean flag = false;
    private static Object lock = new Object();

    static class ThreadC extends Thread {
        @Override
        public void run() {
            for (int i = 0; i <= 9; i++) {
                synchronized (lock) {
                    System.out.print(i);
                    flag = true;
                    lock.notifyAll();
                    while (flag) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    static class ThreadD extends Thread {
        @Override
        public void run() {
            for (int i = 0; i <= 9; i++) {
                synchronized (lock) {
                    while (!flag) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print((char)(i + 65));
                    flag = false;
                    lock.notifyAll();
                }
            }
        }
    }

    public static void main(String[] args) {
        ThreadC threadC = new ThreadC();
        ThreadD threadD = new ThreadD();
        threadD.start();
        threadC.start();
    }
}

(二)LockSupport方法

import java.util.concurrent.locks.LockSupport;

public class LockSupportPrint {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i <= 1000000; i++) {
            ThreadA threadA = new ThreadA();
            ThreadB threadB = new ThreadB();
            threadA.setLockThread(threadB);
            threadB.setLockThread(threadA);
            //线程B先调用start()方法,否则当遍历次数尽可能大时,会发生线程B一直处于waiting状态的情况
            threadB.start();
            threadA.start();
            threadA.join();
            threadB.join();
            System.out.println();
        }
    }

    static class ThreadA extends Thread {
        private Thread lockThread;

        public void setLockThread(Thread lockThread) {
            this.lockThread = lockThread;
        }

        @Override
        public void run() {
            for (int i = 0; i <= 9; i++) {
                System.out.print(i);
                LockSupport.unpark(lockThread);
                LockSupport.park();
            }
        }
    }

    static class ThreadB extends Thread {
        private Thread lockThread;

        public void setLockThread(Thread lockThread) {
            this.lockThread = lockThread;
        }

        @Override
        public void run() {
            for (int i = 0; i <= 9; i++) {
                LockSupport.park();
                System.out.print((char)(65 + i));
                LockSupport.unpark(lockThread);
            }
        }
    }
}

你可能感兴趣的:(交替打印数字 字母的两种通用方法)