Tag:

在Java中向线程池中添加新任务之前等待任务完成的最佳方法?

我想使用类似ThreadPoolExecutor的东西来管理在可用线程上运行一堆任务。 这些任务都是相同的类型,但处理不同的帐户。 这些帐户的新任务可以定期添加,我希望它能够检查并且不允许新任务开始,直到同一帐户的旧任务已经完成。 最好的方法是什么? 例 帐户“234”的任务已启动(通过ThreadPoolExecutor.execute()) 帐户“238”的任务已启动(通过ThreadPoolExecutor.execute()) 帐户“234”的新任务已创建但未添加执行,因为第一个“234”任务未完成(检查此问题的最佳方法是什么?) 帐户“238”的任务完成 帐户“238”的新任务启动(通过ThreadPoolExecutor.execute()),因为当前没有为该帐户运行 最好的方法是什么? 只需要在等待/睡眠()中检查Runnable中的某些检查变量,以便完成“234”的第一个任务吗? 或者有更好的解决方案吗?

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[], […]

管理客户端套接字池

我需要从Java应用程序管理与外部服务器的长时间运行的TCP套接字连接。 我正在寻找一个好的套接字池,所以我将能够重新使用套接字。 还有另一个解决方案而不是org.apache.commons.pool2 ?

Java连接池实现

如果可以实现它,你能看看我的连接池吗? public class ConnectionPool { private static List pool = null; private static int available = 0; private ConnectionPool() {} public static DBConnection getConnection() { if (pool == null) { pool = new ArrayList(); for (int i = 0; i 0) { available–; return pool.remove(available); } else { return null; } } public static void […]

哪个更快? 更多可运行的工作更少,或者更少的可运行工作? (ExecutorService的)

我试图弄清楚如何从multithreading应用程序中获得最大性能。 我有一个我创建的线程池,如下所示: ExecutorService executor = Executors.newFixedThreadPool(8); // I have 8 CPU cores. 我的问题是,我应该将工作分成只有8个runnables / callables,这与线程池中的线程数相同,还是应该将它分成1000000 runnables / callables呢? for (int i = 0; i < 1000000; i++) { Callable worker = new MyCallable(); // Each worker does little work. Future submit = executor.submit(worker); } long sum = 0; for (Future future : list) sum += […]

Android线程池管理多个蓝牙handeling线程?

所以我有我的Android蓝牙应用程序,它有主机和客户端。 问题是,因为我正在进行多个连接,所以我需要一个线程来处理每个连接。 这都是milk’n’cookies,所以我想我会把所有线程都放在一个数组中。 一个小小的研究表明,更好的方法是使用线程池,但我似乎无法理解其工作原理。 此外,它实际上甚至可以在一个数组中保存线程吗?

Java:当线程池中的所有线程都完成时,通知主类/不同线程中对象的相同实例

当ThreadPoolExecutor所有线程都完成后,如何通知我的主类实例化ThreadPoolExecutor ? ThreadPoolExecutor threadPool = null; ThreadClass threadclass1; ThreadClass threadclass2; final ArrayBlockingQueue queue = new ArrayBlockingQueue(maxPoolSize); puclic MyClass(){ threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue); threadClass1 = new ThreadClass; threadClass2 = new ThreadClass; threadPool.execute(threadClass1); threadPool.execute(threadClass2); //Now I would like to do something until the threadPool is done working //The threads fill a ConcurrentLinkedQueueand I […]

ScheduledThreadPoolExecutor和corePoolSize 0?

我想要一个ScheduledThreadPoolExecutor ,如果没有工作要做,它也会停止最后一个线程,如果有新任务,则创建(并保持线程活动一段时间)。 但是,一旦没有更多工作要做,它应该再次丢弃所有线程。 我天真地将它创建为new ScheduledThreadPoolExecutor(0)但结果是,没有创建任何线程,也没有执行任何计划任务。 任何人都可以告诉我,如果我没有在ScheduledThreadpoolExecutor周围编写自己的包装器就可以实现我的目标吗? 提前致谢!

如何在scheduleWithFixedDelay抛出exception时重新启动计划?

我使用ScheduledExecutorService来安排一些需要定期运行的任务。 我想知道这个代码是否可以在发生exception时恢复计划。 ScheduledExecutorService service = Executors.newScheduledThreadPool(1); this.startMemoryUpdateSchedule(service);//See below method //Recursive method to handle exception when run schedule task private void startMemoryUpdateSchedule(ScheduledExecutorService service) { ScheduledFuture future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.MINUTES); try { future.get(); } catch (ExecutionException e) { e.printStackTrace(); logger.error(“Exception thrown for thread”,e); future.cancel(true); this.startMemoryUpdateSchedule(service); } catch(Exception e) { logger.error(“Other exception “,e); } }

是否有两个ExecutorServices可以共享一个线程池?

我有一组要处理的记录,并且处理可以并行化,所以我创建了一个ExecutorService (通过Executors#newCachedThreadPool() )。 单个记录的处理本身由可并行化的步骤组成,因此我想使用另一个ExecutorService 。 是否有一种简单的方法可以使这个新的使用相同的底层线程池? 它甚至可取吗? 谢谢。