天天看點

多線程之等待逾時模式

package com.ilucky.test.jdk.util.concurrent;

/**
 * 等待/通知機制.
 * 等待逾時模式.
 * @author IluckySi
 *
 */
public class WaitNotifyTest2 {

    private static Object lock = new Object();

    public static void main(String[] args) {
        System.out.println(getResouce(L));
    }

    public static int getResouce(long timeout) {
        synchronized(lock) {
            long future = System.currentTimeMillis() + timeout;
            long remaining = timeout;
            while(remaining > ) {
                System.out.println("----------> remaining="+remaining);
                try {
                    lock.wait(remaining);
                    remaining = future - System.currentTimeMillis();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return -;
                }
            }
            return ;
        }
    }
}
/**
----------> remaining=2000
1
*/
           

繼續閱讀