Guarded Suspension设计模式

场景:
A:正在厨房炒菜
B:您的快递到了,请接收
A:等我把这盘菜炒好再接收,请稍等

请求类

package com.dwz.concurrency2.chapter9;

public class Request {
    final private String value;
    
    public Request(String value) {
        this.value = value;
    }
    
    public String getValue() {
        return this.value;
    }
}

请求队列

package com.dwz.concurrency2.chapter9;

import java.util.LinkedList;

public class RequestQueue {
    private final LinkedList<Request> queue = new LinkedList<>();
    
    public Request getRequest() {
        synchronized (queue) {
            while (queue.isEmpty()) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
            return queue.removeFirst();
        }
    }
    
    public void putRequest(Request request) {
        synchronized (queue) {
            queue.addLast(request);
            queue.notifyAll();
        }
    }
}

客户端

package com.dwz.concurrency2.chapter9;

import java.util.Random;

public class ClientThread extends Thread {
    private final RequestQueue queue;
    //随机因子
    private final Random random;
    
    private final String sendValue;
    
    public ClientThread(RequestQueue queue, String sendValue) {
        this.queue = queue;
        this.sendValue = sendValue;
        random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        for(int i = 0; i < 10; i++) {
            System.out.println("Client -> request " + sendValue);
            queue.putRequest(new Request(sendValue));
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

服务端

package com.dwz.concurrency2.chapter9;

import java.util.Random;

public class ServerThread extends Thread {
    private final RequestQueue queue;
    
    private final Random random;
    
    private volatile boolean flag = true;
    
    public ServerThread(RequestQueue queue) {
        this.queue = queue;
        random = new Random(System.currentTimeMillis());
    }
    
    @Override
    public void run() {
        while (flag) {
            Request request = queue.getRequest();
            if(null == request) {
                System.out.println("Received the empty request.");
                continue;
            }
            System.out.println("server ->" + request.getValue());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                break;
            }
        }
    }
    
    public void close() {
        this.flag = false;
        this.interrupt();
    }
}

测试类

package com.dwz.concurrency2.chapter9;

public class SuspensionClient {
    public static void main(String[] args) throws InterruptedException {
        RequestQueue queue = new RequestQueue();
        new ClientThread(queue, "Alex").start();
        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();
        
        Thread.sleep(10_000L);
        
        serverThread.close();
    }
}

注意:在有wait(),sleep(),while时打断线程要注意特殊处理

原文地址:https://www.cnblogs.com/zheaven/p/12143074.html