简体   繁体   English

java:在最长的时间段内执行一个方法

[英]java : execute a method over a maximum period of time

I am using the JavaMail API , and there is a method in the Folder class called "search" that sometimes take too long to execute. 我正在使用JavaMail API ,在Folder类中有一个名为“search”的方法,有时执行时间太长。 What i want is to execute this method over a maximum period of time( say for example 15 seconds in maximum) , that way i am sure that this method will not run up more than 15 seconds. 我想要的是在最长的时间段内执行此方法(例如最多15秒),这样我确信此方法不会超过15秒。

Pseudo Code 伪代码

messages = maximumMethod(Folder.search(),15);

Do I have to create a thread just to execute this method and in the main thread use the wait method ? 我是否必须创建一个线程来执行此方法,并在主线程中使用wait方法?

The best way to do this is create a single threaded executor which you can submit callables with. 执行此操作的最佳方法是创建一个单线程执行程序 ,您可以使用它来提交callables。 The return value is a Future<?> which you can get the results from. 返回值是Future<?> ,您可以从中获取结果。 You can also say wait this long to get the results. 你也可以说等这么久才能得到结果。 Here is sample code: 这是示例代码:

    ExecutorService service = Executors.newSingleThreadExecutor();
    Future<Message[]> future = service.submit(new Callable<Message[]>() {
        @Override
        public Message[] call() throws Exception {
            return Folder.search(/*...*/);
        }
    });

    try {
        Message[] messages = future.get(15, TimeUnit.SECONDS);
    }
    catch(TimeoutException e) {
        // timeout
    }

You could 你可以

  1. mark current time 标记当前时间
  2. launch a thread that will search in the folder 启动将在该文件夹中搜索的线程
  3. while you get the result (still in thread) don't do anything if current time exceeds time obtained in 1 plus 15 seconds. 当你得到结果(仍然在线程中)时,如果当前时间超过1加15秒内获得的时间,则不要做任何事情。 You won't be able to stop the connection if it is pending but you could just disgard a late result. 如果连接处于待处理状态,您将无法停止连接,但您可能只是迟到了一个迟到的结果。

Also, if you have access to the socket used to search the folder, you could set its timeout but I fear it's gonna be fully encapsulated by javamail. 此外,如果您有权访问用于搜索文件夹的套接字,您可以设置其超时但我担心它将被javamail完全封装。

Regards, Stéphane 此致,Stéphane

This SO question shows how to send a timeout exception to the client code: How do I call some blocking method with a timeout in Java? 这个SO问题显示了如何向客户端代码发送超时异常: 如何在Java中使用超时调用某些阻塞方法?

You might be able to interrupt the actual search using Thread.interrupt() , but that depends on the method's implementation. 您可以使用Thread.interrupt()中断实际搜索,但这取决于方法的实现。 You may end up completing the action only to discard the results. 您可能最终只完成操作以丢弃结果。

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

相关问题 在Java中,是否可以将方法执行一段时间,达到时间限制后停止? - In Java, is it possible to execute a method for a period of time and stop after it reaches the time limit? 在每个时间段后执行代码,JAVA - Execute code after each period of time, JAVA Java 安排一个作业在某个时间运行最长时间 - Java schedule a job to run at certain time for maximum period of time Java-为什么一段时间内响应时间会增加? - Java - why is there an increase in response time over period of time? Java 8时间:以周期为单位遍历一天的LocalTime - Java 8 time: Iterate over a day's LocalTime in period steps 长时间监控Java中的代码指标 - Monitoring code metrics in Java over longer time period Java计算一段时间内两点之间的过渡 - Java calculate the transition between two points over a period of time 如何在Java中执行返回最大时间值的阻塞函数 - How to execute a blocking function that returns a value for a maximum amount of time in Java 在Java中执行方法以检查时间并在特定时间执行方法 - Execute a method in java to check the time and execute a method on certain times 定期调用Java Web Service方法 - Calling Java Web Service Method In A Regular Time Period
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM