目录
二、java.util.concurrent.locks中Condition接口中的方法
一、java.long.object中的方法
wait(),使当前线程等待
notify(),唤醒一个等待线程
notifyAll(),唤醒所有等待线程
以上方法只能在同步方法或同步代码块中使用且需要抛出异常,通过同步监视器调用。
生产者消费者代码
public class Test {
public static void main(String[] args) {
Middle m = new Middle();
Producter productT = new Producter(m);
productT.setName("生产线程1");
Customer customT = new Customer(m);
customT.setName("消费线程1");
customT.start();
productT.start();
}
}
class Producter extends Thread {
Middle middle;
public Producter(Middle m) {
middle = m;
}
@Override
public void run() {
while (true) {
try {
sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
middle.product();
}
}
}
class Customer extends Thread {
Middle middle;
public Customer(Middle m) {
middle = m;
}
@Override
public void run() {
while (true) {
try {
sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
middle.custom();
}
}
}
class Middle {
private int nums = 0;
public synchronized void product() {
if (nums < 100) {
nums++;
System.out.println(Thread.currentThread().getName() + " 生产 " + nums);
notify();
} else {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public synchronized void custom() {
if (nums > 0) {
System.out.println(Thread.currentThread().getName() + " 消费 " + nums);
nums--;
notify();
} else {
try {
wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
二、java.util.concurrent.locks中Condition接口中的方法
await(),使当前线程等待
signal(),唤醒一个等待线程
signalAll(),唤醒所有等待线程
以上方法只能在Lock对象中使用。通过调用Lock对象中的newCondition()方法创建一个Condition对象,通过该Condition对象调用以上方法。
生产者消费者代码
public class Test {
public static void main(String[] args) {
Middle m = new Middle();
Producter productT = new Producter(m);
productT.setName("生产线程1");
Customer customT = new Customer(m);
customT.setName("消费线程1");
customT.start();
productT.start();
}
}
class Producter extends Thread {
Middle middle;
public Producter(Middle m) {
middle = m;
}
@Override
public void run() {
while (true) {
try {
sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
middle.product();
}
}
}
class Customer extends Thread {
Middle middle;
public Customer(Middle m) {
middle = m;
}
@Override
public void run() {
while (true) {
try {
sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
middle.custom();
}
}
}
class Middle {
private int nums = 0;
private Lock lock = new ReentrantLock();
private Condition empCondition = lock.newCondition();
private Condition fulCondition = lock.newCondition();
public void product() {
lock.lock();
if (nums < 100) {
nums++;
System.out.println(Thread.currentThread().getName() + " 生产 " + nums);
fulCondition.signal();
} else {
try {
empCondition.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
lock.unlock();
}
public void custom() {
lock.lock();
if (nums > 0) {
System.out.println(Thread.currentThread().getName() + " 消费 " + nums);
nums--;
empCondition.signal();
} else {
try {
fulCondition.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
lock.unlock();
}
}