Tag: futuretask

Future.get(超时)的基础线程行为

我们正在使用Future以超时来完成任务。 当时间限制超过时,我们会收到TimeOutException 。 从thread dump的行为,我意识到底层线程继续。 是这样的吗? 如何处理漫游的multithreading? 如果从池中删除的线程没有IOException ,该怎么办? 如果这是真的,那么kill底层线程的方法是什么。 在我的情况下,它一直在等待外部IO 。 线程转储的一部分: Thread 29587: (state = IN_NATIVE) – java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) @bci=0 (Compiled frame; information may be imprecise) – java.net.SocketInputStream.read(byte[], int, int) @bci=84, line=129 (Compiled frame) – java.io.BufferedInputStream.fill() @bci=175, line=218 (Compiled frame) – java.io.BufferedInputStream.read1(byte[], int, int) @bci=44, line=258 (Compiled frame) – java.io.BufferedInputStream.read(byte[], […]

TimerTask和Executors.newScheduledThreadPool之间的区别(1)

我需要安排一些将来要完成的工作。 我可以用两种方式做到: 创建TimerTask并执行timer.schedule(…); 使用Executors.newScheduledThreadPool(1) : ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ScheduledFuture scheduleHandle = scheduler.schedule(pushExternalRunnable, runScheduleDate.getTime() – now.getTime(), TimeUnit.MILLISECONDS); 这两种方法在未来安排工作有什么区别?

如何进入FutureTask执行状态?

我有一个singleThreadExecutor,以执行我按顺序提交给它的任务,即一个接一个的任务,没有并行执行。 我有runnable这样的东西 MyRunnable implements Runnable { @Override public void run() { try { Thread.sleep(30000); } catch (InterruptedException e1) { e1.printStackTrace(); } } 例如,当我向上述单线程执行程序提交三个MyRunnable实例时,我希望第一个任务执行,因为Thread.sleep在TIMED_WAITING中有执行线程(我可能错误的具体州)。 其他两个任务不应该分配线程来执行它们,至少在第一个任务完成之前不会。 所以我的问题是如何通过FutureTask API获取此状态或以某种方式到达正在执行任务的线程(如果没有这样的线程然后任务等待执行或挂起)并获得其状态或者可能由某些其他方式? FutureTask只定义了isCanceled()和isDone()方法,但这些方法还不足以描述Task的所有可能的执行状态。

Android蓝牙套装 – 计时

我写了一个用于连接外部附件的蓝牙API。 设计API的方式是有一堆阻塞调用,如getTime , setTime , getVolume , setVolume等。这些工作方式是他们创建一个有效负载来发送和调用一个名为sendAndReceive()的方法,一些准备工作,最终做到以下几点: byte[] retVal = null; BluetoothSocket socket = getSocket(); // write socket.getOutputStream().write(payload); // read response if(responseExpected){ byte[] buffer = new byte[1024]; // buffer store for the stream int readbytes = socket.getInputStream().read(buffer); retVal = new byte[readbytes]; System.arraycopy(buffer, 0, retVal, 0, readbytes); } return retVal; 问题是,有时这个设备会变慢或没有响应,所以我想对这个调用设置超时。 我已经尝试了几种将此代码放在线程\ future任务中并使用超时运行它的方法,例如: FutureTask theTask […]

java Callable FutureTask Excecuter:如何监听已完成的任务

我对执行者服务很陌生。 喜欢自己做所有事情,但我认为是时候相信这些服务了。 我想通过Executer将Runnable交给我。 执行者将它包装在FutureTask并将其交还给我。 现在我调用poll done()方法。 但是我希望在done()方法返回true时得到通知。 有一个get()方法阻塞直到Runnable完成,但是我需要为每个作业添加一个额外的线程,只是为了看看它什么时候完成。 我可以给我的执行者一些额外的Callable以获得关于任务完成的通知吗? 怎么去这里? 我可以在run方法的末尾添加一些代码,但是done()可能仍然是false …

如何终止multithreading中超时的任务?

我需要创建一个库,在其中我将有同步和异步方法。 executeSynchronous() – 等到我有结果,返回结果。 executeAsynchronous() – 立即返回一个Future,如果需要,可以在其他事情完成后处理。 我的图书馆的核心逻辑 客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它。 然后,我们将使用该DataKey对象构造一个URL,并通过执行它来对该URL进行HTTP客户端调用,然后在我们将响应作为JSON字符串返回之后,我们将通过创建将该JSON字符串发送回我们的客户DataResponse对象。 有些客户会调用executeSynchronous() ,有些可能会调用executeAsynchronous() ,这就是为什么我需要在我的库中单独提供两个方法。 接口: public interface Client { // for synchronous public DataResponse executeSynchronous(DataKey key); // for asynchronous public Future executeAsynchronous(DataKey key); } 然后我有我的DataClient实现上面的Client接口: public class DataClient implements Client { private RestTemplate restTemplate = new RestTemplate(); private ExecutorService executor = Executors.newFixedThreadPool(10); // for synchronous call […]

在TimeoutException之后如何让FutureTask返回?

在下面的代码中,我按照预期在100秒后捕获TimeoutException。 在这一点上,我希望代码退出main和程序终止,但它继续打印到控制台。 如何让任务在超时后停止执行? private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool(); private static T timedCall(Callable c, long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, TimeoutException { FutureTask task = new FutureTask(c); THREAD_POOL.execute(task); return task.get(timeout, timeUnit); } public static void main(String[] args) { try { int returnCode = timedCall(new Callable() { public Integer call() throws Exception { for […]

如何在FutureTask中捕获exception

在发现在Java 1.6(和Eclipse Executors.newCachedThreadPool()上的Executors.newCachedThreadPool()运行的FutureTask吞下了Runnable.run()方法中的exception后,我试图找到一种方法来捕获这些exception而不添加throw / catch我所有的Runnable实现。 API建议覆盖FutureTask.setException()应该有助于: 导致此未来报告ExecutionException,并将给定的throwable作为其原因,除非已设置或已取消此Future。 在计算失败时,run方法在内部调用此方法。 但是,似乎没有调用此方法(使用调试器运行显示FutureTask捕获exception,但未setException )。 我写了以下程序来重现我的问题: public class RunTest { public static void main(String[] args) { MyFutureTask t = new MyFutureTask(new Runnable() { @Override public void run() { throw new RuntimeException(“Unchecked exception”); } }); ExecutorService service = Executors.newCachedThreadPool(); service.submit(t); } } public class MyFutureTask extends FutureTask { public MyFutureTask(Runnable r) […]

如何使用ThreadPoolExecutor和自定义任务实现PriorityBlockingQueue

我经常搜索,但找不到解决问题的方法。 我有自己的类BaseTask ,它使用ThreadPoolExecutor来处理任务。 如果我不想要优先级(即使用LinkedBlockingQueue ),这可以正常工作,但是当我尝试使用PriorityBlockingQueue我得到ClassCastException因为ThreadPoolExecutor将我的Tasks包装到FutureTask对象中。 这显然是可以的,因为FutureTask没有实现Comparable ,但我将如何继续解决优先级问题? 我已经读过你可以在ThreadPoolExecutor覆盖newTaskFor ,但我似乎根本找不到这个方法……? 我们欢迎所有的建议! 一些代码可以帮助: 在我的BaseTask课程中,我有 private static final BlockingQueue sWorkQueue = new PriorityBlockingQueue(); private static final ThreadFactory sThreadFactory = new ThreadFactory() { private final AtomicInteger mCount = new AtomicInteger(1); public Thread newThread(Runnable r) { return new Thread(r, “AsyncTask #” + mCount.getAndIncrement()); } }; private static final BaseThreadPoolExecutor sExecutor […]