一、使用非阻塞队列实现消费者-生产者模式
//使用非阻塞队列模式实现消费者-生产者模式:
public class ProAndCou {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
int maxTask = 3;
for(int i=0;i<3;i++){
new Thread(new Productor(queue, maxTask)).start();
}
for(int i=0;i<3;i++){
new Thread(new Customer(queue, maxTask)).start();
}
}
}
class Customer implements Runnable {
private Queue<String> queue; // 生产队列
@SuppressWarnings("unused")
private Integer maxTask; // 最大产量
public Customer(Queue<String> queue, Integer maxTask) {
super();
this.queue = queue;
this.maxTask = maxTask;
}
@Override
public void run() {
while (true) {
synchronized (queue) {
while (queue.size() == 0) {
try {
queue.wait(); // 线程进入阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000); // 模拟消费用时
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者线程:" + queue.remove());
queue.notify(); // 唤醒阻塞线程
}
}
}
}
class Productor implements Runnable {
private Queue<String> queue; // 生产队列
private Integer maxTask; // 最大产量
int i = 1; // 生产计数
public Productor(Queue<String> queue, Integer maxTask) {
super();
this.queue = queue;
this.maxTask = maxTask;
}
@Override
public void run() {
while (true) {
synchronized (queue) {
while (queue.size() >= maxTask) {
try {
queue.wait(); // 线程进入阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000); // 模拟生产用时
} catch (InterruptedException e) {
e.printStackTrace();
}
String msg = "产品序列-" + (i++);
queue.add(msg);
System.out.println("生产者线程:" + msg);
queue.notify(); // 唤醒阻塞线程
}
}
}
}
二、使用阻塞队列实现消费者-生产者模式
public class ProAndCouBlocked {
public static void main(String[] args) {
int queueSize = 10;
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(queueSize);
new Thread(new Consumerr(queue,queueSize)).start();
new Thread(new Producerr(queue,queueSize)).start();
}
}
class Consumerr implements Runnable{
private BlockingQueue queue;
public Consumerr(BlockingQueue queue,int queueSize){
this.queue=queue;
}
@Override
public void run() {
while(true){
try {
queue.take();
System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producerr implements Runnable{
private BlockingQueue queue;
private int queueSize;
public Producerr(BlockingQueue queue,int queueSize){
this.queue = queue;
this.queueSize =queueSize;
}
@Override
public void run() {
while(true){
try {
queue.put(1);
System.out.println("向队列插入一个元素,队列剩余空间:"+(queueSize-queue.size()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
三、循环打印ABC
通过private static volatile int value
;来控制
public class circleABC {
private static Thread a;
private static Thread b;
private static Thread c;
public static void main(String[] args) {
ABCthread abc = new ABCthread();
a = new Thread(()->{
for(int i=0;i<5;i++){
try {
abc.printA();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"线程A");
b = new Thread(()->{
for(int i=0;i<5;i++){
try {
abc.printB();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"线程B");
c = new Thread(()->{
for(int i=0;i<5;i++){
try {
abc.printC();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"线程C");
a.start();
b.start();
c.start();
}
}
class ABCthread {
private static volatile int value = 1;
public void printA() throws InterruptedException {
synchronized (this){
while(value!=1){
wait();
}
System.out.println(Thread.currentThread().getName() + ": A");
value = 2;
notifyAll();
}
}
public void printB() throws InterruptedException {
synchronized (this){
while(value!=2){
wait();
}
System.out.println(Thread.currentThread().getName() + ": B");
value = 3;
notifyAll();
}
}
public void printC() throws InterruptedException {
synchronized (this){
while(value!=3){
wait();
}
System.out.println(Thread.currentThread().getName() + ": C");
value = 1;
notifyAll();
}
}
}
四、 哲学家吃饭避免死锁
package thread;
public class philosophere_1 {
// chopsticks
private boolean[] used = new boolean[] { false, false, false, false, false };
private static String LOCK = "lock";
public static void main(String[] args) {
//表示5双筷子
ChopStick cs0 = new ChopStick();
ChopStick cs1 = new ChopStick();
ChopStick cs2 = new ChopStick();
ChopStick cs3 = new ChopStick();
ChopStick cs4 = new ChopStick();
philosophere_1 philosophere1 = new philosophere_1();
philosophere1.new Philosopher(0,cs0,cs1).start();
philosophere1.new Philosopher(1,cs1,cs2).start();
philosophere1.new Philosopher(2,cs2,cs3).start();
philosophere1.new Philosopher(3,cs3,cs4).start();
philosophere1.new Philosopher(4,cs4,cs0).start();
}
class Philosopher extends Thread {
private int num;
private ChopStick left;
private ChopStick right;
public Philosopher(int num,ChopStick left,ChopStick right) {
this.num = num;
this.left= left;
this.right = right;
}
public void eating() {
System.out.println("my num is " + num + " , I am eating...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void thinking() {
System.out.println("my num is " + num + ", I am thinking...");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//死锁
public void takeChopsticks() throws InterruptedException {
synchronized(left) {
Thread.sleep(1000);
synchronized (right){
Thread.sleep(1000);
System.out.println(num+" 拿到了筷子");
}
}
}
//左撇子算法
public void takeChopsticks1() throws InterruptedException {
if(num%2==0){
synchronized(left) {
Thread.sleep(1000);
synchronized (right){
Thread.sleep(1000);
System.out.println(num+" 拿到了筷子");
}
}
}else{
synchronized(right) {
Thread.sleep(1000);
synchronized (left){
Thread.sleep(1000);
System.out.println(num+" 拿到了筷子");
}
}
}
}
//线程粗话,同时拿到左右筷子
public void takeChopsticks2() throws InterruptedException {
synchronized (LOCK) {
if(used[num]||used[(num+1)%5]){
LOCK.wait();
}
System.out.println(num + " :获取筷:" + left + ":" + right);
used[num] = true;
used[(num+1)%5] = true;
}
}
//放下筷子时,也要申请锁,然后通知所有等待的线程
public void putDownChopsticks() {
synchronized(LOCK) {
used[num] = false;
used[(num + 1) % 5] = false;
System.out.println("my num is " + num + " , I have finished...");
LOCK.notifyAll();
}
}
@Override
public void run() {
while(true) {
thinking();
try {
takeChopsticks1();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
eating();
//putDownChopsticks();
}
}
}
}
class ChopStick{
}