针对线程的同步,java提供了很多种方法,比如synchronized关键字,lock方法等,都可以实现对临界区资源的访问保护,防止多个线程并发操作临界区资源而导致的问题。有的时候,光有同步还不够,还需要加入线程之间的通信机制,比如典型的消费者生产者问题。这里,基于lock+condition(条件锁)来实现典型的消费者生产者问题,例子是一个环形数组,当数组满或者空的时候就等待,当非满或者非空就通知其他线程。
代码中采用了两个条件变量,一般的情况下,我们使用一个条件变量,一个条件变量时,若发出singal,还是没有办法正确的通知到线程,也许通知的还是要等待的线程,但两个条件变量,则能够保证每次通知的线程都是能正常执行的,这种实现方式实际是优化了线程的调度效率,值得推荐和学习。
public class BasciSingal {
public static void main(String[] args) {
final BoundedBuffer buff = new BoundedBuffer();
Runnable take = new Runnable() {
@Override
public void run() {
try {
buff.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Runnable put = new Runnable() {
@Override
public void run() {
try {
Random r = new Random();
buff.put(r.nextInt());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ExecutorService execute = Executors.newCachedThreadPool();
for (int i = 0; i < 11; i++) {
execute.submit(take);
}
for (int i = 0; i < 11; i++) {
execute.submit(put);
}
execute.shutdown();
}
}
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr = 0;
int takeptr = 0;
int count = 0;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length) {
System.out.printf("%s put wait\n", Thread.currentThread().getName());
notFull.await();
}
System.out.printf("%s put %s \n", Thread.currentThread().getName(), x.toString());
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0) {
System.out.printf("%s take wait\n", Thread.currentThread().getName());
notEmpty.await();
}
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
System.out.printf("%s take %s \n", Thread.currentThread().getName(), x.toString());
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}