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

  • 相关阅读:
    RecyclerView 数据刷新的几种方式 局部刷新 notify MD
    【图片】批量获取几万张图片
    RV BaseRecyclerViewAdapterHelper 总结 MD
    RecyclerView.ItemDecoration 间隔线
    Kotlin【简介】Android开发 配置 扩展
    Kotlin 特性 语法糖 优势 扩展 高阶 MD
    一个十分简洁实用的MD风格的UI主框架
    折叠伸缩工具栏 CollapsingToolbarLayout
    FloatingActionButton FAB 悬浮按钮
    Glide Picasso Fresco UIL 图片框架 缓存 MD
  • 原文地址:https://www.cnblogs.com/zheaven/p/12143074.html
Copyright © 2011-2022 走看看