2つのスレッドが交互に実行されるいくつかの方法


1.synchronized,notify,waitを用いる
package com.program;

public class TestThread {

    public static void main(String[] args) {
        final Object object = new Object();
        Runnable runnable = new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    synchronized (object) {
                        object.notify();
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        Runnable runnable1 = new Runnable() {
            public void run() {
                for (int i = 6; i <= 10; i++) {
                    synchronized (object) {
                        object.notify();
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        new Thread(runnable).start();
        new Thread(runnable1).start();
    }

}

2.2つ目はロック、condition、signal、await
package com.program;

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

public class TestLock {

    public static void main(String[] args) {
        final Lock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            public void run() {
                try {
                    for (int i = 1; i <= 5; i++) {
                        lock.lock();
                        condition.signal();
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        condition.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });
        executorService.execute(new Runnable() {
            public void run() {
                try {
                    for (int i = 6; i <= 10; i++) {
                        lock.lock();
                        condition.signal();
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        condition.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });
    }

}

3.3つ目は、スレッドと組み合わせて渋滞キューを使用する
package com.program;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TestBlocking {

    public static void main(String[] args) throws InterruptedException {
        final BlockingQueue blockingQueue = new ArrayBlockingQueue(10);
        final BlockingQueue blockingQueue1 = new ArrayBlockingQueue(10);
        new Thread(new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    try {
                        blockingQueue.put(i);
                        System.out.println(blockingQueue1.take());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                for (int i = 6; i <= 10; i++) {
                    try {
                        System.out.println(blockingQueue.take());
                        blockingQueue1.put(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}