• 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时打断线程要注意特殊处理

  • 相关阅读:
    Python学习笔记(三)
    自己出的一套前端笔试题
    Vue 数组封装和组件data定义为函数一些猜测
    前端Mvvm QC 上传了测试版
    为什么我们的web前端变的越来越复杂
    grootJsAPI文档
    grootjs 简明教程
    深入grootJs(进阶教程)
    也议 js闭包和ie内存泄露原理
    此utf8 非彼utf8 ——谈http协议里的编码问题
  • 原文地址:https://www.cnblogs.com/zheaven/p/12143074.html
Copyright © 2020-2023  润新知