一个对象取决于gc根对象,例如一个线程字段,该对象的gc是什么?

时间:2019-03-11 08:19:13

标签: java garbage-collection

请参阅ThreadPoolExecutor的Worker类

      private final class Worker
        extends AbstractQueuedSynchronizer
        implements Runnable
    {

        final Thread thread;

        Runnable firstTask;

        volatile long completedTasks;


        Worker(Runnable firstTask) {
            setState(-1); // inhibit interrupts until runWorker
            this.firstTask = firstTask;
            this.thread = getThreadFactory().newThread(this);
        }

        /** Delegates main run loop to outer runWorker  */
        public void run() {
            runWorker(this);
        }
    }

这是ThreadPoolExecutor类中的一些代码段

    private boolean addWorker(Runnable firstTask, boolean core) {
        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
            w = new Worker(firstTask);
            final Thread t = w.thread;
            ...
        }
    }

我通过Google得知Thread是gc根,worker依赖于线程对象,线程也依赖于worker。我知道,当线程运行时,如果将回收工作线程,并且仅在相对于gc root无法访问对象时才进行标记,则工作线程如何确定应将工作线程标记为可回收。否则,线程运行时不会回收工作者?

0 个答案:

没有答案
相关问题