两个线程交替执行几种方式

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.第二种使用Lock,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(); }}

【两个线程交替执行几种方式】

    推荐阅读