两个线程,分别打印[1,3,5]和[2,4,6],写一个程序,打印[1,2,3,4,5,6]。

两个线程,分别打印[1,3,5]和[2,4,6],写一个程序,打印[1,2,3,4,5,6]。
方案1 用synchronized 配合wait notify
方案2 用ReentrantLock配合condition
方案3 对线程数据取余,直接调用线程的run方法

方案1不能实现超过2个线程的要求,因为notify是不能保证唤醒哪个线程
下面分别是3种实现的代码


package com.eyu.ahxy.module.common.config;

import java.util.concurrent.TimeUnit;
import static com.eyu.ahxy.module.common.config.OneTwoOneTwoTest1.*;

public class OneTwoOneTwoTest1 {
	static int NUM = 0;
	static int MAX = 6;
	static Object LOCK = new Object();

	public static void main(String[] args) throws InterruptedException {

		Thread thread1 = new ThreadTest1();
		thread1.start();
		Thread thread2 = new ThreadTest1();
		thread2.start();
		TimeUnit.SECONDS.sleep(1);
		synchronized (LOCK) {
			LOCK.notify();
		}
		thread1.join();
		thread2.join();
	}

}

class ThreadTest1 extends Thread {

	public void run() {
		while (true) {
			synchronized (LOCK) {
				try {
					LOCK.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				NUM = NUM + 1;
				System.err.println(NUM + "        ====" + Thread.currentThread());
				if (NUM >= MAX) {
					break;
				}
				LOCK.notify();
			}

		}

	};

}



package com.eyu.ahxy.module.common.config;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import static com.eyu.ahxy.module.common.config.OneTwoOneTwoTest2.*;

public class OneTwoOneTwoTest2 {
	static int NUM = 0;
	static int MAX = 6;

	static ReentrantLock LOCK = new ReentrantLock();
	static Condition thread1Ready = LOCK.newCondition();
	static Condition thread2Ready = LOCK.newCondition();

	public static void main(String[] args) throws InterruptedException {

		Thread thread1 = new ThreadTest2(thread1Ready, thread2Ready);
		thread1.start();
		Thread thread2 = new ThreadTest2(thread2Ready, thread1Ready);
		thread2.start();
		TimeUnit.SECONDS.sleep(1);

		LOCK.lock();
		thread1Ready.signal();
		LOCK.unlock();

		thread1.join();
		thread2.join();

	}

}

class ThreadTest2 extends Thread {
	Condition self;
	Condition next;

	ThreadTest2(Condition self, Condition next) {
		this.self = self;
		this.next = next;
	}

	public void run() {
		while (true) {
			try {
				LOCK.lock();
				self.await();
				NUM = NUM + 1;
				System.err.println(NUM + "        ====" + Thread.currentThread());
				if (NUM >= MAX) {
					break;
				}
				next.signal();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				LOCK.unlock();
			}
		}
	};

}



package com.eyu.ahxy.module.common.config;

import static com.eyu.ahxy.module.common.config.OneTwoOneTwoTest3.NUM;

import java.util.ArrayList;
import java.util.List;

public class OneTwoOneTwoTest3 {
	static int NUM = 0;
	static int MAX = 6;
	static int THREAD_NUM = 2;

	public static void main(String[] args) throws InterruptedException {
		List<Thread> list = new ArrayList<>();
		for (int i = 0; i < THREAD_NUM; i++) {
			list.add(new ThreadTest3());
		}

		for (int i = 0; i < MAX; i++) {
			int num = i % THREAD_NUM;
			list.get(num).run();;
		}

	}

}

class ThreadTest3 extends Thread {

	public void run() {
		NUM = NUM + 1;
		System.err.println(NUM + "        ====" + Thread.currentThread());
	};

}



猜你喜欢

转载自huangyunbin.iteye.com/blog/2204562