以给定的执行时间启动线程 [英] Start Thread with a given execution time

查看:54
本文介绍了以给定的执行时间启动线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主进程调用了一个外部库方法.该方法有时会挂起.我无法修复外部库,因为其他公司对此负责.

My main process calls an external library method. This method sometimes hangs. I can not fix the external library because another company is responsible for it.

我想对定义的执行计时器的库调用使用线程.当方法调用耗时很长时,放置该方法调用的带有Runnable的线程应该停止并且主进程应该前进.

I want to use a Thread for the library calls with a defined execution timer. When the method call takes to long, the Thread with the Runnable in which the method call is placed should stop and the main process should go forward.

  1. 主线程等待
  2. 执行线程
    • 启动启动计时器线程
    • 计时器线程完成后,杀死执行线程
  1. Main Thread wait
  2. Execute Thread
    • start Start timer Thread
    • When timer thread is finished kill Execute Thread

有人可以使用此逻辑的代码吗,可以使用的设计模式还是可以阅读某些信息的网页?

Does anybody have some code this logic is for, a design pattern i can use or a internet page i can read some informations?

谢谢

推荐答案

看看Java 5和更高版本中的java.lang.concurrent包,尤其是CompletionService接口和实现它的类.

Take a look at the java.lang.concurrent package in Java 5 and later, in particular the CompletionService interface and the classes that implement it.

此界面包括一些调用,这些调用使您可以提交任务并等待其完成,或者在超时后继续执行.

This interface includes calls that allow you to submit a task and either wait for it to complete, or else continue after a timeout.

这篇关于以给定的执行时间启动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆