Javaマルチスレッドの生産者と消費者モデル,スレッド間の通信
JAvaマルチスレッドの生産者と消費者モデル:まずブロックキューがあり、生産者は生産したものをキューに入れ、消費者はキューから取り出す.キュー内のものの数がその容量に達するとブロックされます.
synchronizedキーワード修飾:オブジェクトにロックをかけ、スレッドの安全を保証し、CPUが任意のスケジューリングを発生した場合、スレッドが安全ではないことはありません.
スレッド間の通信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クラスには2つの属性の2つの方法final Person p=new Person();//新Personクラスオブジェクトpnew 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();//匿名スレッドを開始する}}
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 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クラスには2つの属性の2つの方法final Person p=new Person();//新Personクラスオブジェクトpnew 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();//匿名スレッドを開始する}}