zoukankan      html  css  js  c++  java
  • 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时打断线程要注意特殊处理

  • 相关阅读:
    线程互斥与同步
    JSP中传递数据出现的乱码问题
    JavaWeb学习——获取类路径下的资源
    Java初始化顺序
    Socket网络编程
    算法练习--LeetCode--17. Letter Combinations of a Phone Number
    算法练习--LeetCode--29. Divide Two Integers
    XCode10 swift4.2 适配遇到的坑
    leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
    iOS 拼音 Swift K3Pinyin
  • 原文地址:https://www.cnblogs.com/zheaven/p/12143074.html
Copyright © 2011-2022 走看看