摘要:基本原理采用的和方法在另外一個線程中結果回來,一下,返回否則就等待超時返回超時采用一線程輪詢的的雙重保險實例參考
基本原理
采用LockSupport的parkNanos和unpack方法
在另外一個線程中結果回來,unpack一下,返回;否則就等待超時返回(超時采用一線程輪詢 + lock的condition的await 雙重保險)
實例import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * * Created by codecraft on 2015/8/26. */ public class DefaultFuture { private static final Map參考FUTURES = new ConcurrentHashMap (); private final long id; private final Lock lock = new ReentrantLock(); private final Condition done = lock.newCondition(); private volatile Response response; private final long start = System.currentTimeMillis(); private final int timeout; public DefaultFuture(long id,int timeout) { this.id = id; this.timeout = timeout; } private long getStartTimestamp() { return start; } public int getTimeout() { return timeout; } public boolean isDone() { return response != null; } public long getId() { return id; } public Object get(int timeout){ if (timeout <= 0) { timeout = 1000; } if (! isDone()) { long start = System.currentTimeMillis(); lock.lock(); try { while (! isDone()) { done.await(timeout, TimeUnit.MILLISECONDS); if (isDone() || System.currentTimeMillis() - start > timeout) { break; } } } catch (InterruptedException e) { throw new RuntimeException(e); } finally { lock.unlock(); } if (! isDone()) { // throw new RuntimeException("timeout"); System.out.println("timeout"); } } return response; } private void doReceived(Response res) { lock.lock(); try { response = res; if (done != null) { done.signal(); } } finally { lock.unlock(); } } public static void received(Response response) { try { DefaultFuture future = FUTURES.remove(response.getId()); if (future != null) { future.doReceived(response); } else { System.out.println("The timeout response finally returned at " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())) + ", response "); } } finally { // CHANNELS.remove(response.getId()); } } private static class RemotingInvocationTimeoutScan implements Runnable { public void run() { while (true) { try { for (DefaultFuture future : FUTURES.values()) { if (future == null || future.isDone()) { continue; } if (System.currentTimeMillis() - future.getStartTimestamp() > future.getTimeout()) { // create exception response. Response timeoutResponse = new Response(future.getId()); // handle response. DefaultFuture.received(timeoutResponse); } } Thread.sleep(30); } catch (Throwable e) { e.printStackTrace(); } } } } static { Thread th = new Thread(new RemotingInvocationTimeoutScan(), "ResponseTimeoutScanTimer"); th.setDaemon(true); th.start(); } public static void main(String[] args){ int timeout = 1000; System.out.println("start"); final long start = System.currentTimeMillis(); final DefaultFuture future = new DefaultFuture(1,timeout); new Thread(new Runnable() { @Override public void run() { while (System.currentTimeMillis() - start < 2000) { //sleep } Response response = new Response(); response.setResult("hello"); future.doReceived(response); } }).start(); Object response = future.get(timeout); System.out.println(System.currentTimeMillis() - start); System.out.println("res "+response); } }
dubbo-DefaultFuture
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65792.html
摘要:傳播安全上下文或使用,通過增加的屬性,來增加相關的配置來達到執行隔離策略,控制線程數或者控制并發請求數來達到熔斷降級的作用。 SpringCloud(第 015 篇)電影Ribbon微服務集成Hystrix增加隔離策略控制線程數或請求數來達到熔斷降級的作用 - 一、大致介紹 1、本章節介紹關于Hystrix的2種隔離方式(Thread Pool 和 Semaphores); 2、Thr...
原理 利用Executors的Future的限時get方法,Google的SimpleTimeLimiter本質上是對Executors的Future的包裝。 實例 package com.facebook.presto.raptor.backup; import com.facebook.presto.spi.PrestoException; import com.google.common....
摘要:運行可運行狀態的線程獲得了時間片,執行程序代碼。阻塞的情況分三種一等待阻塞運行的線程執行方法,會把該線程放入等待隊列中。死亡線程方法執行結束,或者因異常退出了方法,則該線程結束生命周期。死亡的線程不可再次復生。 系列文章傳送門: Java多線程學習(一)Java多線程入門 Java多線程學習(二)synchronized關鍵字(1) java多線程學習(二)synchronized關鍵...
閱讀 3627·2023-04-26 02:32
閱讀 3905·2021-11-23 10:05
閱讀 2291·2021-10-08 10:04
閱讀 2711·2021-09-22 16:06
閱讀 3612·2021-09-22 15:27
閱讀 764·2019-08-30 15:54
閱讀 1698·2019-08-30 13:50
閱讀 2704·2019-08-29 13:56