码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程的生产者与消费者模型,线程间的通信

时间:2020-01-30 09:16:27      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:obj   方法   produce   生产者与消费者   value   tar   static   main   vat   

java多线程中的生产者与消费者模式:
首先有一个阻塞队列,生产者将生产的东西放到队列里,消费者再从队列中取。当队列中的东西数量达到其容量就发生阻塞。

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

public class UseBlockingQueue {
    private static BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);//1是队列容量,超过就会阻塞。
            // new PriorityBlockingQueue<>();
            // new LinkedBlockingQueue<>();
            // new ArrayBlockingQueue<>(10);

    private static class Producer extends Thread {
        @Override
        public void run() {
            Random random = new Random(20191116);
            while (true) {
                try {
                    int message = random.nextInt(100);
                    queue.put(String.valueOf(message));//将消息放入队列中
                    System.out.println("放入消息: " + message);
                    Thread.sleep(random.nextInt(3) * 100);//睡眠
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class Customer extends Thread {
        @Override
        public void run() {
            Random random = new Random(20191116);
            while (true) {
                try {
                    String message = queue.take();//从队列中取走消息
                    System.out.println("收到消息: " + message);
                    Thread.sleep(random.nextInt(3) * 100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread producer = new Producer();
        Thread customer = new Customer();
        producer.start();
        customer.start();
    }
}

synchronized关键字修饰:给对象加锁,保证线程安全,如果CPU发生任意调度,也不会线程不安全。

public class MyQueue2 {
    private int[] array = new int[2];
    private volatile int size;
    private int front;
    private int rear;

    private Object full = new Object();
    private Object empty = new Object();

    public void put(int message) throws InterruptedException {
        while (size == array.length) {
            synchronized (full) {
                full.wait();
            }
        }

        synchronized (this) {
            array[rear] = message;
            rear = (rear + 1) % array.length;
            size++;
        }

        synchronized (empty) {
            empty.notify();
        }
    }

    public synchronized int take() throws InterruptedException {
        while (size == 0) {
            synchronized (empty) {
                empty.wait();
            }
        }

        int message;
        synchronized (this) {
            message = array[front];
            front = (front + 1) % array.length;
            size--;
        }

        synchronized (full) {
            full.notify();
        }

        return message;
    }
}

线程间的通信
public class ThreadDemo {
public static void main(String[] args){
class Person{
public String name;
private String gender;
public void set(String name,String gender){
this.name =name;
this.gender =gender;
}
public void get(){
System.out.println(this.name+"...."+this.gender);
}
}//Person类 有两个属性 两个方法
final Person p =new Person();//new一个Person类对象p
new Thread(new Runnable(){//匿名线程
public void run(){//覆写run方法
int x=0;
while(true){
if(x==0){
p.set("张三", "男");
}else{
p.set("lili", "nv");
}
x=(x+1)%2;
}
}
}).start();
new Thread(new Runnable(){
public void run(){
while(true){
p.get();
}
}
}).start();//启动一个匿名线程
}
}

Java多线程的生产者与消费者模型,线程间的通信

标签:obj   方法   produce   生产者与消费者   value   tar   static   main   vat   

原文地址:https://blog.51cto.com/14232658/2468584

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!