【问题标题】:Calling Runnable out of Runnable从 Runnable 中调用 Runnable
【发布时间】:2014-11-05 11:59:15
【问题描述】:
public class MyClass implements Runnable {

    public void run(){
        // first work
        ThreadPool.getInstance().runNew(new MyClass());
        // second work
    }

}

如果我在 MyClass 中创建一个新的 MyClass 实例并将其发送到 ThreadPoolExecutor 会发生什么?我希望它是无限循环,一次只有一个实例工作。第一个线程会立即到达“第二个工作”还是等到下一个线程完成?

【问题讨论】:

  • 添加System.out.println("echo");语句,然后运行看看...
  • 这取决于执行器的实现。由于标准 API 中没有 ThreadPool 类,我们无法知道。
  • 只是一个疯狂的猜测 - StackOverflow ?
  • @vikingsteve 正如 Holger 所说,答案完全取决于 ThreadPool.getInstance().runNew() 对新对象的作用。如果它真的一个线程池,那么就不会有堆栈溢出。
  • public void runNew (Runnable pkt){ threadPool.execute(pkt); } , and _threadPool = new ThreadPoolExecutor(10, 10, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue());

标签: java multithreading threadpoolexecutor


【解决方案1】:

我猜它会一直运行到无法创建新的Thread

然后 JVM 会抛出:

java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:592)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多