Producer thread of communication - consumer queue blocking mode
Manufacturers face questions:
1, you talk about the understanding of the volatile?
2, CAS Did you know?
3, ABA Atomic AtomicInteger class to talk about? Atomic update references know?
4, we all know that ArrayList is not thread-safe, please write an insecure coding case and gives the solution?
5, lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.
6, CountDownLatch, CyclicBarrier, Semaphore used it?
7 , blocking queue know?
8, the thread pool used it? ThreadPoolExecutor talk about your understanding?
9, the thread pool used it? Production How do you set reasonable parameters?
10, location analysis and coding deadlock?
Producer thread of communication - consumer queue blocking mode code verification
package com.wwl.juc;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Thread operations resource category
*/
class MyResource {
// control producer - consumer flag
private volatile boolean FLAG = true;
// atomic class, concurrent processes to ensure the security thread
private AtomicInteger atomicInteger = new AtomicInteger(1);
// blocking queue, the message queue as
private BlockingQueue<String> blockingQueue;
public MyResource(BlockingQueue<String> blockingQueue) {
this.blockingQueue = blockingQueue;
System.out.println ( "Use blocking queue:". + BlockingQueue.getClass () getName ());
}
// producer method
public void produce() throws InterruptedException {
String data;
boolean produceFlag;
while (FLAG) {
data = atomicInteger.getAndIncrement() + "";
produceFlag = blockingQueue.offer(data, 2, TimeUnit.SECONDS);
if (produceFlag) {
System.out.println(Thread.currentThread().getName() + "\t 生产" + data + "成功");
} else {
System.out.println(Thread.currentThread().getName() + "\t 生产" + data + "失败");
}
TimeUnit.SECONDS.sleep(1);
}
System.out.println (. Thread.currentThread () getName () + "\ t thread halt, produce pause");
}
// Consumers methods
public void consume() throws InterruptedException {
String consumeFlag;
while (FLAG) {
consumeFlag = blockingQueue.poll(2, TimeUnit.SECONDS);
if (consumeFlag == null || "".equals(consumeFlag)) {
System.out.println (. Thread.currentThread () getName () + "\ t thread halt, consume pause");
} else {
System.out.println(Thread.currentThread().getName() + "\t 消费" + consumeFlag + "成功");
}
}
}
// Pause thread method
public void stop() {
this.FLAG = false;
}
}
/ **
* producer thread of communication - consumer queue blocking mode
* Volatile / CAS / AtomicInteger / BlockingQueue integrated use of knowledge
* Volatile ensure visibility between threads;
* CAS compare exchange algorithm, reducing thread context switching, improve concurrent efficiency;
* atom of AtomicInteger class, guaranteed in the case of concurrent and thread-safe;
* BlockingQueue as message queues, no manual preparation of blocked wakeup codes, with automatic blocking queue blocking, wake-up function.
* /
public class ProducerConsumerBlockQueueDemo {
public static void main(String[] args) throws InterruptedException {
MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
new Thread(() -> {
System.out.println ( "producer Producer thread started");
try {
myResource.produce();
} catch (InterruptedException e) {
e.printStackTrace ();
}
}, "producer").start();
new Thread(() -> {
System.out.println ( "Consumer Consumer thread started");
System.out.println();
System.out.println();
try {
myResource.consume();
} catch (InterruptedException e) {
e.printStackTrace ();
}
}, "consumer").start();
// simulate business execution: Let producer - consumer thread executes five seconds
TimeUnit.SECONDS.sleep(5);
// Pause producer - consumer Thread
System.out.println (Thread.currentThread () getName () + "\ t pause producer - consumer thread.");
myResource.stop();
}
}
Program execution results are as follows: