在学习Lock实现生产者和消费者遇到的阻塞问题
产品类:
public class Value {
public static String value = "";//“”没有产品,非空有产品
}
生产者:
public class Producer {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void setValue() {
try {
lock.lock();
if (!Value.value.equalsIgnoreCase("")) {
condition.await();
}
String value = System.currentTimeMillis() + "_" + System.nanoTime();
Value.value = value;
System.out.println(Thread.currentThread().getName()+"生产了value为:" + value);
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
消费者:
public class Customer {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void getValue() {
try {
lock.lock();
if (Value.value.equalsIgnoreCase("")){
condition.await();}
System.out.println(Thread.currentThread().getName()+"消费了value,为:" + Value.value);
Value.value = "";
condition.signal();
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
测试方法:
public class TestMain {
public static void main(String[] args) throws Exception {
Producer producer = new Producer();
Customer customer = new Customer();
Runnable producerRunable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <Integer.MAX_VALUE; i++) {
producer.setValue();
}
}
};
Runnable customerRunable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <Integer.MAX_VALUE; i++) {
customer .getValue();
}
}
};
Thread t1 = new Thread(producerRunable);
Thread t2 = new Thread(customerRunable);
t1.start();
t2.start();
}
出现了错误:
两个线程都是wating,没有达到线程之间通信(加锁,释放锁)的目的。
这个写法,用语法层面synchronized是没错误的。
解决办法:
生产者和消费者使用同一把锁,即同一类中。
public class ProducerAndCustomer {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void setValue() {
try {
lock.lock();
if (!Value.value.equalsIgnoreCase("")) {
condition.await();
}
String value = System.currentTimeMillis() + "_" + System.nanoTime();
Value.value = value;
System.out.println(Thread.currentThread().getName()+"生产了value为:" + value);
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
synchronized ()
}
public void getValue() {
try {
lock.lock();
if (Value.value.equalsIgnoreCase("")){
condition.await();}
System.out.println(Thread.currentThread().getName()+"消费了value,为:" + Value.value);
Value.value = "";
condition.signal();
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
测试方法:
public class TestMain {
public static void main(String[] args) throws Exception {
ProducerAndCustomer producerAndCustomer = new ProducerAndCustomer ();
Runnable producerRunable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <Integer.MAX_VALUE; i++) {
producerAndCustomer .setValue();
}
}
};
Runnable customerRunable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <Integer.MAX_VALUE; i++) {
producerAndCustomer .getValue();
}
}
};
Thread t1 = new Thread(producerRunable);
Thread t2 = new Thread(customerRunable);
t1.start();
t2.start();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8cbc8c51cb898c413b250af6f7d22583.png)
问题原因:没有使用同一个Lock,生产者和消费者都是使用了各自的Lock。所以释放锁和加锁,各自不影响,导致程序阻塞。