多线程按顺序打印abc

使用synchronized 实现

synchronized原理

public class ThreeThreads {

    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();

        ThreadPrint threadPrint1 = new ThreadPrint("a", c,a);
        ThreadPrint threadPrint2 = new ThreadPrint("b", a,b);
        ThreadPrint threadPrint3 = new ThreadPrint("c", b,c);


        new Thread(threadPrint1).start();
        Thread.sleep(100);
        new Thread(threadPrint2).start();
        Thread.sleep(100);
        new Thread(threadPrint3).start();
        Thread.sleep(100);

    }


    public static class ThreadPrint implements Runnable{
        private String name;
        private Object prev;
        private Object self;

        public ThreadPrint(String name, Object prev, Object self) {
            this.name = name;
            this.prev = prev;
            this.self = self;
        }

        @Override
        public void run() {

            while (true){
                synchronized (prev){
                    synchronized (self){
                        System.out.print(name);
                        self.notifyAll();
                    }
                    try{
                        prev.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}

使用Lock 实现

Lock原理
ReentrantLock原理

public class ThreeThreads {

    private static Lock lock = new ReentrantLock();
    private static int state = 0;


    static class ThreadA extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    lock.lock();
                    while (state % 3 == 0) {
                        System.out.print("a");
                        state++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    static class ThreadB extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    lock.lock();
                    while (state % 3 == 1) {
                        System.out.print("b");
                        state++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    static class ThreadC extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    lock.lock();
                    while (state % 3 == 2) {
                        System.out.print("c");
                        state++;
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }

}

使用Condition 实现

Condition原理

public class ThreeThreads {

    private static Lock lock = new ReentrantLock();
    private static Condition ca = lock.newCondition();
    private static Condition cb = lock.newCondition();
    private static Condition cc = lock.newCondition();

    private static int count = 0;

    static class ThreadA extends Thread{

        @Override
        public void run() {
            try {
                lock.lock();
                while (true){
                    while (count%3 != 0){
                        ca.await();
                    }
                    System.out.print("a");
                    count ++;
                    cb.signal();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }

    static class ThreadB extends Thread{

        @Override
        public void run() {
            try {
                lock.lock();
                while (true){
                    while (count % 3 != 1){
                        cb.await();
                    }
                    System.out.print("b");
                    count++;
                    cc.signal();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }

    static class ThreadC extends Thread{
        @Override
        public void run() {
            try {
                lock.lock();
                while (true){
                    while (count % 3 != 2){
                        cc.await();
                    }
                    System.out.print("c");
                    count++;
                    ca.signal();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }
}

使用Semaphore实现

Semaphore

 public class ThreeThreads {
    private static Semaphore sa = new Semaphore(1);
    private static Semaphore sb = new Semaphore(0);
    private static Semaphore sc = new Semaphore(0);

    static class ThreadA extends Thread{
        @Override
        public void run() {
            try {
                while (true){
                    sa.acquire();
                    System.out.print("a");
                    sb.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class ThreadB extends Thread{
        @Override
        public void run() {
            try {
                while (true){
                    sb.acquire();
                    System.out.print("b");
                    sc.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    static class ThreadC extends Thread{
        @Override
        public void run() {
            try {
                while (true){
                    sc.acquire();
                    System.out.print("c");
                    sa.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new ThreadA().start();
        new ThreadB().start();
        new ThreadC().start();
    }
    }

你可能感兴趣的:(#,java并发编程,java,并发)