Two ways of Sequence Printer implementation

题目:两个线程,分别打印[1,3,5]和[2,4,6],写一个程序,打印[1,2,3,4,5,6]。
下面列出两种解法,分别用同步代码块和锁,具体参见程序。

package com.freelemon.concurrency.threads;

public class SeqPrinter1 {
    private Object obj = new Object();
    private boolean isThread1 = true;

    public static void main(String[] args){
        SeqPrinter1 printer1 = new SeqPrinter1();
        printer1.printSequence();
    }

    public void printSequence() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj){
                    System.out.println(1);
                    isThread1 = false;
                    obj.notify();

                    while (!isThread1){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.println(3);
                    isThread1 = false;
                    obj.notify();

                    while (!isThread1){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.println(5);
                    isThread1 = false;
                    obj.notify();

                }
            }
        }, "Thread-1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj){
                    while (isThread1){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.println(2);
                    isThread1 = true;
                    obj.notify();

                    while (isThread1){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.println(4);
                    isThread1 = true;
                    obj.notify();

                    while (isThread1){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    System.out.println(6);
                    isThread1 = true;
                    obj.notify();

                }
            }
        }, "Thread-2").start();

    }

}
package com.freelemon.concurrency.threads;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SeqPrinter2 {
    private Lock lock = new ReentrantLock();
    private Condition thread1Ready = lock.newCondition();
    private Condition thread2Ready = lock.newCondition();

    public static void main(String[] args){
        SeqPrinter2 printer2 = new SeqPrinter2();
        printer2.printSequence();
    }

    public void printSequence(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    lock.lock();

                    thread1Ready.await();

                    System.out.println(1);

                    thread2Ready.signal();
                    thread1Ready.await();


                    System.out.println(3);

                    thread2Ready.signal();
                    thread1Ready.await();


                    System.out.println(5);

                    thread2Ready.signal();
                    thread1Ready.await();

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


            }
        }, "Thread-1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    lock.lock();

                    thread1Ready.signal();
                    thread2Ready.await();

                    System.out.println(2);

                    thread1Ready.signal();
                    thread2Ready.await();

                    System.out.println(4);

                    thread1Ready.signal();
                    thread2Ready.await();

                    System.out.println(6);

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


            }
        }, "Thread-2").start();

    }


}