requestqueue.java

来自「多线程与并发处理是程序设计好坏优劣的重要课题」· Java 代码 · 共 26 行

JAVA
26
字号
import java.util.LinkedList;

public class RequestQueue {
    private static final long TIMEOUT = 30000;
    private final LinkedList queue = new LinkedList();
    public synchronized Request getRequest() {      
        long start = System.currentTimeMillis(); // 开始时间
        while (queue.size() <= 0) {                 
            long now = System.currentTimeMillis(); //现在时间
            long rest = TIMEOUT - (now - start); //剩下时间
            if (rest <= 0) {
                throw new LivenessException("thrown by " + Thread.currentThread().getName());
            }
            try {                                   
                wait(rest);
            } catch (InterruptedException e) {      
            }                                       
        }                                           
        return (Request)queue.removeFirst();
    }
    public synchronized void putRequest(Request request) {  
        queue.addLast(request);
        notifyAll();                                        
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?