두 라인, 각각 [1,3,5]와[2,4,6]를 인쇄하고 프로그램을 작성한다[1,2,3,4,5,6].

4174 단어 라인
두 라인, 각각 [1,3,5]와[2,4,6]를 인쇄하고 프로그램을 작성한다[1,2,3,4,5,6].방안 1은synchronized와wait notify 방안 2와Reentrant Lock과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());
	};

}


좋은 웹페이지 즐겨찾기