FutureTask.get方法永远阻塞,虽然我从另一个线程设置它的值(FutureTask.get method is blocking forever, although I am setting it's value from another thread)
我试图使用下面的FutureResult类在两个线程之间发出信号,该类扩展了FutureTask类。 运行脚本时,它会打印以下结果。
发送:0
SENT:0 POLL:FutureResult @ 513431
SIGNALED:FutureResult @ 513431然后程序永远挂断。 我希望FutureResult实例应该从它的阻塞get方法返回值。 然后在控制台中打印结果。 但FutureResult.get永远阻止。
import java.util.concurrent.*; /** * Created by someone on 20/08/2015. */ final public class FutureResult<T> extends FutureTask<T> { private static final Object SS = "SS"; public FutureResult() { super(() -> null); } public void signal(final T value) { set(value); } public void signalError(final Throwable throwable) { setException(throwable); } public static void main(String... args) throws Exception { final ArrayBlockingQueue<FutureResult> queue = new ArrayBlockingQueue<>(1000000); new Thread(() -> { while (true) { try { final FutureResult poll = queue.take(); System.out.println("POLL: " + poll); if (poll != null) { poll.signal(SS); System.out.println("SIGNALLED: " + poll); } } catch (Exception e) { e.printStackTrace(); } } }).start(); new Thread(() -> { for (int i = 0; i < 1; i++) { final FutureResult<Object> result = new FutureResult<>(); System.out.println("SENDING: " + i); queue.offer(new FutureResult()); try { System.out.println("SENT: " + i); result.get(); System.out.println("GOT : " + i); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); } }
I am trying to signal between two threads using the below FutureResult class which extends FutureTask class. When run the script, it prints the following result.
SENDING: 0
SENT: 0 POLL: FutureResult@513431
SIGNALLED: FutureResult@513431Then the program hang up forever. I expect FutureResult instance should return the value from it's blocking get method. Then print the result in the console. But FutureResult.get is blocking forever.
import java.util.concurrent.*; /** * Created by someone on 20/08/2015. */ final public class FutureResult<T> extends FutureTask<T> { private static final Object SS = "SS"; public FutureResult() { super(() -> null); } public void signal(final T value) { set(value); } public void signalError(final Throwable throwable) { setException(throwable); } public static void main(String... args) throws Exception { final ArrayBlockingQueue<FutureResult> queue = new ArrayBlockingQueue<>(1000000); new Thread(() -> { while (true) { try { final FutureResult poll = queue.take(); System.out.println("POLL: " + poll); if (poll != null) { poll.signal(SS); System.out.println("SIGNALLED: " + poll); } } catch (Exception e) { e.printStackTrace(); } } }).start(); new Thread(() -> { for (int i = 0; i < 1; i++) { final FutureResult<Object> result = new FutureResult<>(); System.out.println("SENDING: " + i); queue.offer(new FutureResult()); try { System.out.println("SENT: " + i); result.get(); System.out.println("GOT : " + i); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); } }
原文:https://stackoverflow.com/questions/32420624
最满意答案
如果您的数据协定是接口,则WCF无法知道要为传入请求实例化的对象。 在所有Add Service Reference读取WSDL并根据WSDL中的类型信息生成新类之后,不需要该类与服务中的相同。
If your data contracts are interfaces WCF can't know what object to instantiate for an incoming request. There is no need for the class to be the same as in the service, after all the Add Service Reference reads the WSDL and generates new classes based on the type info in the WSDL.
相关问答
更多-
首先你需要清楚一项服务是什么。 您是指单个端点,还是同一个主机中的多个端点? 假设你的意思是一个单一的端点,那么是的,但有一点工作。 端点只能实现单个接口; 所以你需要做的是将你想要实现的所有接口合并到一个接口中 public interface IMyInterface : IInterface1, IInterface2 然后在你的实现类中实现它们。 你不能做的是将多个接口和多个实现神奇地合并成一个端点。 First you need to be clear what a service is. Do ...
-
远程处理有一些用例,例如单个进程中的应用程序域之间的通信。 话虽如此,是的,WCF是要走的路。 然而,从你所说的一些事情来看,我不确定你是否明白这应该如何工作。 在WCF中做事的通常方法是创建一个完全由数据传输对象(所有属性,无代码)和接口组成的共享程序集。 双方都引用此程序集,但客户端使用服务器的服务引用。 这有帮助吗? Remoting has some use cases, such as communication among appdomains within a single process. ...
-
可以实现多个接口。 人们只能继承一个抽象类。 One can implement more than one interface. One can only inherit a single abstract class.
-
不是。当你浏览网页时,你并没有像在C#中那样处理引用,所以你将无法返回一个不可序列化的对象。 即使那样,只有标记为DataMember的数据才会出现。 是。 您将不得不调整已知类型,但同样,这将成为DataContract的接口而不是OperationContract No. When you're going over the web you're not dealing with references like you might in C# so you won't be able to return ...
-
WCF中的DataContractAttribute不适用于接口: 请参阅此处的文档 这个问题的答案可能会让你更好地了解原因。 The DataContractAttribute in WCF is not intended for use on an interface: See the documentation here The answer in this question may give you a better idea why.
-
ServiceContract和OperationContract可以用于抽象类,并且代码将成功编译,因为根据C#编译器,没有任何问题。 然而,在托管服务时,ServiceHost 会抛出一个异常,因为根据WCF规则 - 如果一个类已经标记了ServiceContract属性,则另一个类不能从它继承。 底线 - 它不受支持。 你需要使用接口。 ServiceContract and OperationContract can be used on abstract classes and the code ...
-
IBar需要具体和DataContract。 WCF不是关于分布式对象,而是一种传输数据并使服务在这些数据上运行的方式。 您无法返回WCF中具有行为的对象。 IBar needs to be concrete and a DataContract. WCF isn't about distributed objects, but rather a way to transfer data and have services work on that data. You can't return an obj ...
-
使用与WCF的接口(Using Interfaces With WCF)[2022-08-19]
如果您的数据协定是接口,则WCF无法知道要为传入请求实例化的对象。 在所有Add Service Reference读取WSDL并根据WSDL中的类型信息生成新类之后,不需要该类与服务中的相同。 If your data contracts are interfaces WCF can't know what object to instantiate for an incoming request. There is no need for the class to be the same as in t ... -
尝试也在基本接口上设置ServiceContract属性。 Ok, this is the exact code, I condensed it as much as I can. Just start a new console application and copy/paste it. Start it and add a service reference to it. CommonlyUsedMethod() is not present in the reference, while the o ...