多线程之Guarded Suspension 挂起监视

多线程发起请求,服务端按顺序处理,需挂起监视。

模拟request对象

//请求的模拟对象,request.
public class Request {
    //请求者的名字
    private final String value;

    public Request(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

模拟request容器


//线程安全控制的核心
//存放请求request容器
public class RequestQueue {
    //存放request
    private final LinkedList<Request> queue = new LinkedList<>();
    //容器进口,存放request,并通知其他线程。
    public void putRequest(Request request) {
        synchronized (queue) {
            //按顺序存放request
            queue.addLast(request);
            //通知其他线程
            queue.notifyAll();
        }
    }
    //容器出口,处理request,队列里没有 request 就让当前线程 wait。
    public Request getRequest() {
        synchronized (queue) {
            while (queue.size() <= 0) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
            //按顺序拿出request
            Request request = queue.removeFirst();
            return request;
        }
    }
}

请求端线程


//发请求的线程,请求端。
public class ClientThread extends Thread {
    //request容器
    private final RequestQueue queue;

    //发请求者名字
    private final String sendValue;

    public ClientThread(RequestQueue queue, String sendValue) {
        this.queue = queue;
        this.sendValue = sendValue;
    }

    @Override
    public void run() {
        //
        for (int i = 0; i < 8; i++) {
            System.out.println("请求者的名字: " + sendValue);
            //将请求放入 RequestQueue 的集合中
            queue.putRequest(new Request(sendValue));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

服务端线程 


//请求的处理者,服务端
public class ServerThread extends Thread {
    //request容器
    private final RequestQueue queue;
    //关闭服务端标识
    private volatile boolean closed = false;

    public ServerThread(RequestQueue queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        while (!closed) {
            Request request = queue.getRequest();
            if (null == request) {
                System.out.println("收到空请求.");
                //返回到while中判断
                continue;
            }
            System.out.println("开始处理请求------> " + request.getValue());
        }
    }
    //中断方法
    public void close() {
        this.closed = true;
        this.interrupt();
    }
}

Test_Main

public static void main(String[] args) throws InterruptedException {
        final RequestQueue queue = new RequestQueue();
        //开启两个请求线程
        new ClientThread(queue, "Jun").start();
        new ClientThread(queue, "South").start();
        //开启服务线程
        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();
        //serverThread.join();
        System.out.println("给予10秒处理时间,10秒后将关闭服务端。");
        Thread.sleep(10000);
        //关闭服务端
        System.out.println("10时间到了,关闭服务端。");
        serverThread.close();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值