我有一个对象需要定期做一些工作,而对象本身是活着的,所以我设计了类似下面的东西.基本上是一个Main类,它包含对ScheduledExecutorService实例的引用.在此示例中,所有定期工作都是将字符串打印到std.
我希望代码的行为如下:
但是,如果我运行这个程序,会发生什么,它会永远发生.基本上gc从不调用o1的终结器,因此,调度程序永远不会关闭,因此,即使主线程结束,程序仍然不会退出.
现在,如果我在test2()中注释掉o1.register,程序的行为就像它应该的那样,例如gc调用等.同样在调试器中,似乎只有在调用ScheduledExecutorService.schedule后才会创建一个实际的线程.
有什么解释发生了什么?
public class Main {
public static void main(String[] args) throws Exception {
test2();
System.gc();
System.out.println("Waiting for finalize to be called..");
Thread.sleep(5000);
}
private static void test2() throws Exception {
Main o1 = new Main();
o1.register();
Thread.sleep(5000);
}
private final ScheduledExecutorService _scheduler = Executors.newSingleThreadScheduledExecutor();
private void register() {
_scheduler.scheduleWithFixedDelay(new Runnable() {
@Override public void run() {
System.out.println("!doing stuff...");
}
}, 1, 1, TimeUnit.SECONDS);
}
@Override
protected void finalize() throws Throwable { …Run Code Online (Sandbox Code Playgroud) 我在Windows XP上,我遇到了新的Python 3.2期货模块的问题.好像我无法让ProcessPoolExecutor工作.会话示例:
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> from concurrent import futures
>>> executor = futures.ProcessPoolExecutor()
>>> def pio(x):
... print("I AM HERE")
... return True
...
>>> fut = executor.submit(pio, 5)
>>> Process Process-1:
Traceback (most recent call last):
File "C:\Python32\lib\multiprocessing\process.py", line 259, in _bootstrap
self.run()
File "C:\Python32\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\Python32\lib\concurrent\futures\process.py", line 133, in _process_worker …Run Code Online (Sandbox Code Playgroud) 在没有Java Executors的生活中,必须为每个Runnable任务创建新线程.创建新线程需要线程开销(创建和拆除),这增加了复杂性并浪费了非Executor程序的时间.
参考代码:
没有Java执行器 -
new Thread (aRunnableObject).start ();
Run Code Online (Sandbox Code Playgroud)
使用Java Executor -
Executor executor = some Executor factory method;
exector.execute (aRunnable);
Run Code Online (Sandbox Code Playgroud)
底线是Executors抽象出如何管理线程的低级细节.
真的吗?
谢谢.
我确信我可以将一些东西放在一起,这可以让我弄清楚这一点,但我希望有一个我只是缺少的开箱即用的解决方案.我阅读了文档,但我没有看到任何内容.
我的特定应用程序使用的是a ThreadPoolExecutor支持DelayQueue,虽然我不确定这是否重要.
谢谢!
我有以下Foo使用FooProcessor类的类.所以我想要做的是,在运行cp1实例进程方法的同时,我想要运行cp2.process().
public class Foo {
public static void main(String [] args){
FooProcessor cp1 = new FooProcessor();
FooProcessor cp2 = new FooProcessor();
cp1.process();
// in parallel process cp2.process();
}
}
public class FooProcessor {
public void process(){
System.out.println("Processing..");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想顺序cp1,所以我希望它运行并完成,如果cp2没有完成或失败它是好的.如果它失败了我想加入结果.它没有返回此示例中的任何内容,但我想返回结果.
为此,应该使用TaskExecutor?还是线程?
我只希望cp2与cp1并行运行.或者,如果我添加更多让我们说cp3,我希望它与cp1并行运行.
我有一个问题,似乎接近Executors和线程池所做的,但我似乎无法让它完全适合.基本上我有工作人员需要花一些时间来初始化,我想要集中,一旦他们准备好了,我就用它们来做工作.我需要在一个线程中执行此操作:
worker = buildExpensiveWorker();
worker.doWork(work1);
worker.doWork(work2);
worker.doWork(work3);
...
Run Code Online (Sandbox Code Playgroud)
虽然Executor只允许我这样做:
doWork(work1);
doWork(work2);
doWork(work3);
...
Run Code Online (Sandbox Code Playgroud)
我需要编写自己的线程池吗?重写已经做得好的事情感觉真是太遗憾了.或者我是否需要用来ThreadLocal保存我的工作者,并从Runnablerun()方法中管理它们?
直接来自这个java教程:
为了支持立即关闭,任务应该正确处理中断
有人能说清楚这一点吗?任务和中断与关机方法之间的关系是什么?这只是意味着是否有任务等待它必须抛出的东西InterruptedException?
提前致谢.
在 ExecutorCompletionService 中,我们有 take() 和 poll()。一个阻塞,直到队列有一个未来,如果队列中没有未来,另一个返回 null。但是什么时候使用 take() 和 poll() 。是否有任何特殊条件来决定这一点,或者我们可以选择任何一个??
我正在为 OCP 考试而学习,我注意到以下代码片段,其中在 DoubleStream 上调用的 forEach 方法的参数必须与 DoubleConsumer 函数接口的参数匹配,但是 lambda 与所需的类型不匹配,为什么它仍然编译?
DoubleStream.of(3.14159, 2.71828)
.forEach(c -> service.submit(
() -> System.out.println(10*c)
));
Run Code Online (Sandbox Code Playgroud)
DoubleConsumer(接受 Double 类型的参数并返回 void 类型),但是这个 forEach 的返回类型为Future<?>where ?表示 Runnable lambda 的返回类型,它是 void,Future - 这不是 void。我这么说是因为 service.submit(...) 的返回类型Future<?>不是 void,为什么这段代码会编译?
我在帖子中读到 Coroutine 不是在启动屏幕上使用的好习惯,而 Executors 是最好的,因为 Coroutine 比 Executors 需要更多时间来启动,但我没有找到实现它的例子,是 Executors是用于管理线程池的普通java Executor 类吗?
executor ×10
java ×8
concurrency ×3
android ×1
daemon ×1
executors ×1
future ×1
lambda ×1
python ×1
return-type ×1
threadpool ×1