Tag: executorservice

超出范围时,ExecutorService是否会收集垃圾?

我问这个问题是因为我正在创建大量的执行程序服务,虽然我可能已经在需要调查的地方发生内存泄漏,但我认为最近对以下代码的更改实际上使其恶化,因此我试图确认到底是怎么回事: @FunctionalInterface public interface BaseConsumer extends Consumer { @Override default void accept(final Path path) { String name = path.getFileName().toString(); ExecutorService service = Executors.newSingleThreadExecutor(runnable -> { Thread thread = new Thread(runnable, “documentId=” + name); thread.setDaemon(true); return thread; }); Future future = service.submit(() -> { baseAccept(path); return null; }); try { future.get(); } catch (InterruptedException ex) { […]

如何停止ScheduledExecutorService?

该程序在九次打印后完成: class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beep() { final Runnable beeper = new Runnable() { public void run() { System.out.println(“beep”); } }; final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate( beeper, 1, 1, SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 1 * 9, SECONDS); } public static void […]

执行器中的Thread.join()等价物

我有一个新手问题。 我有这个代码: public class Main { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub IntHolder aHolder=new IntHolder(); aHolder.Number=0; IncrementorThread A= new IncrementorThread(1, aHolder); IncrementorThread B= new IncrementorThread(2, aHolder); IncrementorThread C= new IncrementorThread(3, aHolder); A.start(); B.start(); C.start(); A.join(); B.join(); C.join(); System.out.println(“All threads completed…”); } } 这将等待所有线程完成。 如果我像这样使用Executors : public class Main […]

如何中断ExecutorService的线程

使用Executors.newSingleThreadExecutor()返回的Executors.newSingleThreadExecutor() ,如何中断它?

转换为ScheduledThreadPoolExecutor

我仍然是Java的初学者,所以我没有学到很多关于线程和并发的知识。 但是,我希望能够使用ScheduledThreadPoolExecutor作为计时器,因为我遇到了java.util.Timer和TimerTask的问题。 我对线程的创建非常感兴趣,并知道我将在几周内了解它们。 但是,如果有可能,有人可以给我一个基本的例子,说明如何使用util.timer将当前的迷你测试程序转换为使用ScheduledThreadPoolExecutor? 我想尽快完成这个例子,所以我没有太多时间去学习线程 – 不管我想要多少。 说完之后,请包含您认为Java初学者应该了解的有关ScheduledThreadPoolExecutor的重要信息。 示例程序 我已经做了一个快速的小例子来表示我在一个更大的程序中遇到的问题。 该程序应该做的是允许用户按下按钮来启动计数器。 然后,用户必须能够在他/她想要时停止并重新启动计数器。 在较大的程序中,此计数器保持相等至关重要,因此我使用了scheduleAtFixRate()方法。 初始延迟始终相同(在这种情况下为0)也很重要。 问题(我相信你会看到)是一旦取消定时器就无法重启 – 我希望ScheduledThreadPoolExecutor能解决这个问题。 码: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.TimerTask; import java.util.Timer; public class Tester extends JFrame { JButton push = new JButton(“Push”); static JTextArea textOut = new JTextArea(); Timer timer = new Timer(); boolean pushed = false; static […]

java代码执行在没有断点和正常运行的调试中产生不同的结果。 ExecutorService坏了吗?

TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); 在debug中运行并发,但在正常运行时它启动并发,但后来在单线程中运行。 我有一些代码,我在ExecutorService启动了4个不同的任务。 其中两个任务应该几乎立即完成,另外两个应该运行一段时间。 这些任务在Future以秒为单位返回执行时间。 此代码负责任务执行和测量: public Future measure(int[] arr, ProcessIntArray processIntArray, ExecutorService es) { Callable task = () -> { long start = System.nanoTime(); processIntArray.process(arr); long end = System.nanoTime(); return (end – start) / 1000000000.0; }; return es.submit(task); } 稍后,在启动这些任务后,我将按照先前运行的执行时间顺序打印相同的输入大小。 Future bubbleSortTime = measure(bubbleSortArray, Solution::bubbleSort, executorService); Future insertionSortTime = measure(insertionSortArray, […]

shutdown和awaitTermination哪个第一次调用有什么区别?

有什么区别 ExecutorService eService = Executors.newFixedThreadPool(2); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.awaitTermination(1, TimeUnit.NANOSECONDS); eService.shutdown(); 和 eService.shutdown(); eService.awaitTermination(1, TimeUnit.NANOSECONDS); 我真的不了解shutdown() 。 此方法不会等待先前提交的任务完成执行。 这是否意味着shutdown()可以终止已提交但未完成的任务? 我尝试了一些例子,他们没有certificate,请举个例子。

如何终止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 […]

如何在java中设置Socket写入时间?

我在java中处理套接字时遇到问题。 我正在运行具有多个客户端连接的TCP服务器。 出于性能原因,我使用了一个简单的线程池来处理数据包。 请参阅下面的代码 public enum LazyWorkCenter { instance; LazyWorkCenter() { lazyWorker = new NamedThreadPoolExecutor(3,3, 0L,TimeUnit.MILLISECONDS, “LazyWorker”); } private ExecutorService lazyWorker ; public void executeLazy(Runnable lazyTask) { lazyWorker.execute(lazyTask); } } public class TcpServerForClient { DataOutputStream out = null; DataInputStream in = null; public void onConnect(ServerSocket socket) throws IOException { Socket client = server.accept(); client.setSoTimeout(1000 * […]

在关闭执行程序之前等待所有线程完成

这是我的代码片段。 ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize); while(conditionTrue) { ClassImplementingRunnable c = new ClassImplementingRunnable(); executor.submit(c); } 现在这样做了 executor.shutdown(); 我想在这里实现的是,我想等待线程池中的所有线程完成执行,然后我想关闭执行程序。 但我想这不是这里发生的事情。 主线程似乎正在执行关闭,它只是关闭所有东西。 在我的线程池大小为2之前,我做了以下操作,它似乎工作。 ClassImplementingRunnable c1 = new ClassImplementingRunnable(); executor.submit(c1); ClassImplementingRunnable c2 = new ClassImplementingRunnable(); executor.submit(c2); Future f1 = executor.submit(c1); Future f2 = executor.submit(c2); while(!f1.done || !f2.done) {} executor.submit(); 我如何为线程池中的更multithreading执行此操作? 谢谢。