摘要:基本原理采用的和方法在另外一個線程中結(jié)果回來,一下,返回否則就等待超時返回超時采用一線程輪詢的的雙重保險實例參考
基本原理
采用LockSupport的parkNanos和unpack方法
在另外一個線程中結(jié)果回來,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
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://hztianpu.com/yun/65792.html
摘要:傳播安全上下文或使用,通過增加的屬性,來增加相關(guān)的配置來達到執(zhí)行隔離策略,控制線程數(shù)或者控制并發(fā)請求數(shù)來達到熔斷降級的作用。 SpringCloud(第 015 篇)電影Ribbon微服務(wù)集成Hystrix增加隔離策略控制線程數(shù)或請求數(shù)來達到熔斷降級的作用 - 一、大致介紹 1、本章節(jié)介紹關(guān)于Hystrix的2種隔離方式(Thread Pool 和 Semaphores); 2、Thr...
原理 利用Executors的Future的限時get方法,Google的SimpleTimeLimiter本質(zhì)上是對Executors的Future的包裝。 實例 package com.facebook.presto.raptor.backup; import com.facebook.presto.spi.PrestoException; import com.google.common....
摘要:運行可運行狀態(tài)的線程獲得了時間片,執(zhí)行程序代碼。阻塞的情況分三種一等待阻塞運行的線程執(zhí)行方法,會把該線程放入等待隊列中。死亡線程方法執(zhí)行結(jié)束,或者因異常退出了方法,則該線程結(jié)束生命周期。死亡的線程不可再次復生。 系列文章傳送門: Java多線程學習(一)Java多線程入門 Java多線程學習(二)synchronized關(guān)鍵字(1) java多線程學習(二)synchronized關(guān)鍵...
閱讀 3755·2023-04-26 02:32
閱讀 4251·2021-11-23 10:05
閱讀 2381·2021-10-08 10:04
閱讀 2881·2021-09-22 16:06
閱讀 3699·2021-09-22 15:27
閱讀 830·2019-08-30 15:54
閱讀 1865·2019-08-30 13:50
閱讀 2779·2019-08-29 13:56