您好,登錄后才能下訂單哦!
這篇文章給大家分享的Java多線程實(shí)現(xiàn)生產(chǎn)者與消費(fèi)者模型的代碼,相信大部分人都還沒學(xué)會這個(gè)技能,為了讓大家學(xué)會,給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。
首先有一個(gè)阻塞隊(duì)列,生產(chǎn)者將生產(chǎn)的東西放到隊(duì)列里,消費(fèi)者再從隊(duì)列中取。當(dāng)隊(duì)列中的東西數(shù)量達(dá)到其容量就發(fā)生阻塞。
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是隊(duì)列容量,超過就會阻塞。
// 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));//將消息放入隊(duì)列中
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();//從隊(duì)列中取走消息
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關(guān)鍵字修飾:給對象加鎖,保證線程安全,如果CPU發(fā)生任意調(diào)度,也不會線程不安全。
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類 有兩個(gè)屬性 兩個(gè)方法
final Person p =new Person();//new一個(gè)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();//啟動(dòng)一個(gè)匿名線程
}
}
看完這篇文章,你們學(xué)會Java多線程實(shí)現(xiàn)生產(chǎn)者與消費(fèi)者模型的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。