From a44b803e07f3675f83cb51e3dea443db67658c4a Mon Sep 17 00:00:00 2001 From: wentch <22250530@qq.com> Date: Fri, 11 Dec 2015 16:53:46 +0800 Subject: [PATCH] --- src/org/redkale/net/sncp/SncpClient.java | 80 ++++++++++++++++++++ src/org/redkale/net/sncp/SncpFuture.java | 94 ------------------------ 2 files changed, 80 insertions(+), 94 deletions(-) delete mode 100644 src/org/redkale/net/sncp/SncpFuture.java diff --git a/src/org/redkale/net/sncp/SncpClient.java b/src/org/redkale/net/sncp/SncpClient.java index d55a43e75..5ce6ea535 100644 --- a/src/org/redkale/net/sncp/SncpClient.java +++ b/src/org/redkale/net/sncp/SncpClient.java @@ -403,4 +403,84 @@ public final class SncpClient { buffer.putInt(0); //结果码, 请求方固定传0 buffer.position(currentpos); } + + protected static final class SncpFuture implements Future { + + private volatile boolean done; + + private T result; + + private RuntimeException ex; + + public SncpFuture() { + } + + public SncpFuture(T result) { + this.result = result; + this.done = true; + } + + public void set(T result) { + this.result = result; + this.done = true; + synchronized (this) { + notifyAll(); + } + } + + public void set(RuntimeException ex) { + this.ex = ex; + this.done = true; + synchronized (this) { + notifyAll(); + } + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public boolean isDone() { + return done; + } + + @Override + public T get() throws InterruptedException, ExecutionException { + if (done) { + if (ex != null) throw ex; + return result; + } + synchronized (this) { + if (!done) wait(10_000); + } + if (done) { + if (ex != null) throw ex; + return result; + } + throw new InterruptedException(); + } + + @Override + public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { + if (done) { + if (ex != null) throw ex; + return result; + } + synchronized (this) { + if (!done) wait(unit.toMillis(timeout)); + } + if (done) { + if (ex != null) throw ex; + return result; + } + throw new TimeoutException(); + } + } } diff --git a/src/org/redkale/net/sncp/SncpFuture.java b/src/org/redkale/net/sncp/SncpFuture.java deleted file mode 100644 index 389adf080..000000000 --- a/src/org/redkale/net/sncp/SncpFuture.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkale.net.sncp; - -import java.util.concurrent.*; - -/** - * 简单的Future实现, set、get方法均只能一个线程调用 - * - * @author zhangjx - * @param - */ -public class SncpFuture implements Future { - - private volatile boolean done; - - private T result; - - private RuntimeException ex; - - public SncpFuture() { - } - - public SncpFuture(T result) { - this.result = result; - this.done = true; - } - - public void set(T result) { - this.result = result; - this.done = true; - synchronized (this) { - notifyAll(); - } - } - - public void set(RuntimeException ex) { - this.ex = ex; - this.done = true; - synchronized (this) { - notifyAll(); - } - } - - @Override - public boolean cancel(boolean mayInterruptIfRunning) { - return false; - } - - @Override - public boolean isCancelled() { - return false; - } - - @Override - public boolean isDone() { - return done; - } - - @Override - public T get() throws InterruptedException, ExecutionException { - if (done) { - if (ex != null) throw ex; - return result; - } - synchronized (this) { - if (!done) wait(10_000); - } - if (done) { - if (ex != null) throw ex; - return result; - } - throw new InterruptedException(); - } - - @Override - public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - if (done) { - if (ex != null) throw ex; - return result; - } - synchronized (this) { - if (!done) wait(unit.toMillis(timeout)); - } - if (done) { - if (ex != null) throw ex; - return result; - } - throw new TimeoutException(); - } -}