简体   繁体   English

非阻塞Java方法

[英]Non-blocking Java methods

Suppose I want to make a method non-blocking, and make the app continue as it is and still surely get the return value: 假设我想使方法成为非阻塞方法,并使应用程序按原样继续运行,并且仍然肯定会获得返回值:

Key key = datastore.put(complexInstance);
String name = key.getName();
doSomethingWithTheName(name);

Or simply, for some Java environment that can't run thread for more than 30 seconds. 或者简单地说,对于某些不能运行线程超过30秒的Java环境。

Where in the put method: 在put方法中的位置:

public Key put(Object instance){
  Key result = null;
  // In here process could take up time, say 30 seconds or more, IDK :-/
  return result; 
}

What is the strategy to achieve this? 实现这一目标的策略是什么?

You could use an implementation of an ExecutorService in combination with a Future object ( http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html ). 您可以将ExecutorService的实现与Future对象( http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html )结合使用。 You would simply start a new thread (or use an existing one) and could fetch the result later. 您只需启动一个新线程(或使用现有线程),然后就可以获取结果。

Java 8 made the process a lot simpler: Java 8使该过程更加简单:

//field in a manager class
ScheduledExecutorService es = Executors.newScheduledThreadPool(10);

//Schedule a task
es.schedule(() -> { /* contents of a runnable */ }, 0, TimeUnit.SECONDS);

Otherwise, you can still just use an anonymous runnable with the Scheduler: 否则,您仍然可以在Scheduler中使用匿名可运行对象:

es.schedule(new Runnable() {

    public void run() {
        /* do what you need */
    }

}, 0, TimeUnit.SECONDS);

However, as you specified, you will still need to do something for a returned value. 但是,按照您的指定,您仍然需要为返回的值做些事情。 There isn't really much that you can do, aside from use something from either a state manager, or to execute relevant methods within your runnable. 除了使用状态管理器中的某些功能或在可运行对象中执行相关方法外,您实际上无能为力。

Your class needs to take a thread pool, probably via the interface ExecutorService , that the methods will run on. 您的类可能需要一个线程池(可能通过接口ExecutorService ,方法将在该线程池上运行。 You could make it a private static variable, but more likely it's better for it to be passed in or at least configured by the client code, who will set size, etc. 您可以将其设为private static变量,但是最好将其传入或至少由客户端代码(由谁来设置大小)进行配置,等等。

Note that if IO is the asynchronous part, it's better to use something built on Java's nio framework than to use lots of threads. 请注意,如果IO是异步部分,那么最好使用基于Java的nio框架构建的内容而不是使用大量线程。

You will need to return a Future of some sort. 您将需要返回某种Future Through Java 7 at least (I'm not sure about 8), Java's future library is very weak and omits some obviously needed functionality. 至少通过Java 7(我不确定8),Java的未来库非常薄弱,并且省略了一些显然需要的功能。 Look at either Functional Java or Google's library. 查看Functional Java或Google的库。 But you will notice that many libraries (Apache's MINA, Amazon Web Service's Java SDK, etc.) implement their own promise libraries to get over these weaknesses. 但是您会注意到,许多库(Apache的MINA,Amazon Web Service的Java SDK等)实现了自己的promise库来克服这些弱点。 (I did the same in my company's code base. Whoops.) (我在公司的代码库中也做过同样的事情。哎呀。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM