線程協作
生産者消費者模式(非GOF23)
- 應用場景:生産者和消費者問題
- 假設倉庫中隻能存放一件産品,生産者将生産出來的産品放入倉庫,消費者将倉庫中産品取走消費
- 如果倉庫中沒有産品,則生産者将産品放入倉庫,否則停止生産并等待,知道倉庫中的産品被消費者取走為止
- 如果倉庫中放有産品,則消費者可以将産品取走消費,否則停止消費并等待,直到倉庫中再次放入産品為止
JDK提供了幾個方法解決線程之間的通信問題

解決方式1–>管程法
package com.kuang.gaoji;
//測試生産者消費者問題1:管程法-->利用緩沖區解決
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Producer(container).start();
new Consumer(container).start();
}
}
//生産者
class Producer extends Thread{
SynContainer container;
public Producer(SynContainer container) {
this.container = container;
}
//生産
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.push(new Chicken(i));
System.out.println("生産了"+i+"隻雞");
}
}
}
//消費者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
//消費
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消費了-->"+container.pop().id+"隻雞");
}
}
}
//産品
class Chicken{
int id;//産品編号
public Chicken(int id) {
this.id = id;
}
}
//緩沖區
class SynContainer{
//需要一個容器大小
Chicken[] chickens = new Chicken[10];
//容器計數器
int count = 0;
//生産者放入産品
public synchronized void push(Chicken chicken){
//如果容器滿了就需要等待消費者消費
if(count==chickens.length){
//通知消費者消費,生産者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果沒滿,我們就需要丢入産品
chickens[count] = chicken;
count++;
//可以通知消費者消費了
this.notify();
}
//消費者消費産品
public synchronized Chicken pop(){
//判斷能否消費
if(count==0){
//等待生産者生産,消費者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果可以消費
count--;
Chicken chicken = chickens[count];
//通知生産者生産
this.notifyAll();
return chicken;
}
}
解決方式2–>信号燈法
package com.kuang.gaoji;
//測試生産者消費者問題1:管程法-->利用緩沖區解決
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生産者-->演員
class Player extends Thread{
TV tv;
public Player(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if(i%2==0){
this.tv.play("快樂大學營播放中");
}else{
this.tv.play("抖音:記錄美好生活");
}
}
}
}
//消費者-->觀衆
class Watcher extends Thread{
TV tv;
public Watcher(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.tv.watch();
}
}
}
//産品-->節目
class TV{
//演員表演,觀衆等待
//觀衆觀看,演員等待
String voice;//表演的節目
boolean flag = true;
//表演
public synchronized void play(String voice){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演員表演了:"+voice);
//通知觀衆觀看
this.notifyAll();//通知觀衆觀看
this.voice=voice;
this.flag = !this.flag;
}
//觀看
public synchronized void watch(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("觀看了:"+voice);
//通知演員表演
this.notifyAll();
this.flag = ! this.flag;
}
}
線程池
使用線程池
- JDK5.0起提供了線程池相關API:ExecutorSevice 和 Executors
- ExecutorService:真正的線程池接口。常見子類ThreadPoolExecutor
- void execute(Runnable command):執行任務/指令,沒有傳回值,一般用來執行Runnable
- Future submit (Callable task):執行任務,有傳回值,一般用來執行Callable
- void shutdown()
- Executors:工具類、線程池的工廠類,使用者建立并傳回不同類型的線程池
package com.kuang.gaoji;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//測試線程池
public class TestPool {
public static void main(String[] args) {
//1.建立服務,建立線程池
//newFixedThreadPool 參數為:線程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
//執行runnable接口的實作類
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//2.關閉連接配接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}