【详解】Java并发之确保挂起设计模式

分析

在Tomcat中的例子

Request -> Tomcat httpServer  -> doing
.................
Request -> Tomcat httpServer  -> Queue wait -> doing

需求
​ 一个线程正在做一个非常关键的任务,这时,有一个其他的线程让当前线程做其他的事情,当前线程只有完成当前的任务才能做其他的任务。

解决方法

使用队列将其他需要工作的线程保存,然后在未来需要时,继续取队列中获取任务,然后执行。

public class ServerThread extends Thread {
    private final RequestQueue queue;
    private final Random random;

    public ServerThread(RequestQueue queue) {
        this.queue = queue;
        random = new Random(System.currentTimeMillis());
    }

    @Override
    public void run() {
        while (!Thread.interrupted()){
            Request request = queue.getRequest();
            if (request == null){
                break;
            }
            System.out.println("server ->"+request.getValue());
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}
public class RequestQueue {

    private final LinkedList<Request> queue = new LinkedList<>();


    /**
     * 放任务
     *
     * @param request 请求任务
     */
    public void putRequest(Request request) {
        synchronized (queue) {
            queue.addLast(request);
            queue.notifyAll();
        }
    }


    /**
     * 取任务
     *
     * @return
     */
    public Request getRequest() {
        synchronized (queue) {
            while (!(queue.size() > 0)) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
            if (Thread.interrupted()){
                return null;
            }
            return queue.removeFirst();
        }
    }
}
public class Request {

    final String value;

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

    public String getValue() {
        return value;
    }
}
public class Test {

    public static void main(String[] args) throws InterruptedException {
        final RequestQueue queue = new RequestQueue();

        new ClientThread(queue,"Alex").start();

        ServerThread serverThread = new ServerThread(queue);
        serverThread.start();


        Thread.sleep(1000);
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        threadGroup.interrupt();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值