Maison  >  Article  >  Java  >  Classe d'interface de thread coopérative de condition en Java

Classe d'interface de thread coopérative de condition en Java

迷茫
迷茫original
2017-03-26 16:01:252049parcourir

Dans l'interface Condition de Java, il existe plusieurs méthodes qui correspondent à wait(), waitall(), wait(time ^) dans Synchronized Ces méthodes correspondent une à une, mais dans Au retour de. Lock.newCondition(), vous pouvez spécifier le thread spécifié à libérer et attendre les ressources.

Veuillez consulter l'exemple [Réveiller un seul fil] :

package TestThread.TestCondition;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.*;

public class TestCondition {
	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Condition condition = lock.newCondition();
		Test1 th1 = new Test1(lock, condition, "线程1");
		Test1 th2 = new Test1(lock, condition, "线程2");
		Test1 th3 = new Test1(lock, condition, "线程3");
		Test2 t2 = new Test2(condition, lock, "我是唤醒线程");
		th1.start();
		th2.start();
		th3.start();
		try {
			Thread.sleep(1000);
			t2.start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class Test1 extends Thread {
	private ReentrantLock lock;
	private Condition condition1;

	public Test1(ReentrantLock lock, Condition condition1, String name) {
		super(name);
		this.lock = lock;
		this.condition1 = condition1;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				lock.lock();// 加锁
				if (i == 5) {
					try {
						condition1.await();
						System.out.println(Thread.currentThread().getName() + "当前线程获取到的值为:++++++++++++" + i);
					} catch (InterruptedException e) {
						System.out.println(Thread.currentThread().getName() + ":线程被中断了!");
					}
				} else {
					System.out.println(Thread.currentThread().getName() + ":当前线程获取到的值为===》" + i);
				}
			} catch (Exception e) {
				System.out.println("系统发生异常!");
			} finally {
				lock.unlock();// 解锁
			}
		}
	}
}

class Test2 extends Thread {
	private Condition condition;
	public ReentrantLock lock;

	/**
	 * @param condition协作对象
	 * @param lock锁对象
	 * @param name线程名称
	 */
	public Test2(Condition condition, ReentrantLock lock, String name) {
		super(name);
		this.condition = condition;
		this.lock = lock;
	}

	@Override
	public void run() {
		try {
			lock.lock();// 加锁
			condition.signal();// 唤醒启动单个线程
			System.out.println(Thread.currentThread().getName() + ":已经唤醒了一个线程!");
		} catch (Exception e) {
			System.out.println(Thread.currentThread().getName() + ":线程挂了!");
		} finally {
			lock.unlock();// 解锁
		}
	}
}

Résultat d'exécution : 2017-03-2501 :16:17

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Résumé des cours JAVAArticle suivant:Résumé des cours JAVA