使用传统JUC锁实现
public class ProducerAndConsumerTraditionalDemo {
class Data {
private int number = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void increment() throws Exception{
lock.lock();
try {
while (number != 0) {
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName() + "线程\t" + this.number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void decrement() throws Exception{
lock.lock();
try {
while (number == 0) {
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName() + "线程\t" + this.number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
Data data = new Data();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "B").start();
}
}
阻塞队列实现
public class BlockQueueDome {
private volatile boolean FLAG = true;
private AtomicInteger atomicInteger = new AtomicInteger();
private BlockingQueue<String> blockingQueue = null;
public BlockQueueDome(BlockingQueue blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void prod () throws InterruptedException {
System.out.println("prod启动成功");
while (FLAG){
String data = atomicInteger.incrementAndGet()+"";
try{ TimeUnit.MICROSECONDS.sleep( 1 ); } catch(Exception e){}
boolean offer = blockingQueue.offer(data,2L,TimeUnit.SECONDS);
if (offer){
System.out.println(Thread.currentThread().getName()+"\t "+data +"插入成功" );
}else {
System.out.println(Thread.currentThread().getName()+"\t "+data +"插入失败" );
}
}
System.out.println("生产者被叫停");
}
public void consumer() throws InterruptedException {
System.out.println("consumer启动成功");
try{ TimeUnit.SECONDS.sleep( 3 ); } catch(Exception e){}
while (FLAG){
String poll = blockingQueue.poll(2L, TimeUnit.SECONDS);
if(poll==null||poll.equalsIgnoreCase("")){
System.out.println("超过两秒获取失败");
continue;
}
System.out.println(poll+"消费成功");
}
System.out.println("消费者被叫停");
}
public void stop(){
FLAG = false;
}
public static void main(String[] args) {
BlockQueueDome blockQueueDome = new BlockQueueDome(new ArrayBlockingQueue(10));
new Thread(() -> {
try {
blockQueueDome.prod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "prod1").start();
new Thread(() -> {
try {
blockQueueDome.consumer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "consumer1").start();
new Thread(() -> {
try {
blockQueueDome.prod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "prod2").start();
new Thread(() -> {
try {
blockQueueDome.consumer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "consumer2").start();
try{ TimeUnit.SECONDS.sleep( 5 ); } catch(Exception e){}
blockQueueDome.stop();
}
}