두 라인, 각각 [1,3,5]와[2,4,6]를 인쇄하고 프로그램을 작성한다[1,2,3,4,5,6].
                                            
 4174 단어  라인
                    
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());
	};
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 라인과synchronized 키워드를 깊이 있게 설명하다루틴이야말로 프로그램의 집행자이고 여러 루틴 간에 프로세스 중의 자원을 공유하고 있다.하나의 cpu는 동시에 하나의 라인만 실행할 수 있으며, 모든 라인은 하나의 타임 슬라이스가 있으며, 타임 슬라이스가 다 사용하면...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.