threadpoolimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 488 行 · 第 1/2 页
JAVA
488 行
// (see bug 6268145). // 2. We want to make sure that the permissions associated // with this thread do NOT include the permissions of // the current thread that is calling this method. // This leads to problems in the app server where // some threads in the ThreadPool randomly get // bad permissions, leading to unpredictable // permission errors. WorkerThread thread = new WorkerThread(threadGroup, name); // The thread must be set to a daemon thread so the // VM can exit if the only threads left are PooledThreads // or other daemons. We don't want to rely on the // calling thread always being a daemon. // Note that no exception is possible here since we // are inside the doPrivileged block. thread.setDaemon(true); thread.start(); return null ; } } ) ; } } /** * This method will return the minimum number of threads maintained * by the threadpool. */ public int minimumNumberOfThreads() { return minWorkerThreads; } /** * This method will return the maximum number of threads in the * threadpool at any point in time, for the life of the threadpool */ public int maximumNumberOfThreads() { return maxWorkerThreads; } /** * This method will return the time in milliseconds when idle * threads in the threadpool are removed. */ public long idleTimeoutForThreads() { return inactivityTimeout; } /** * This method will return the total number of threads currently in the * threadpool. This method returns a value which is not synchronized. */ public int currentNumberOfThreads() { synchronized (lock) { return currentThreadCount; } } /** * This method will return the number of available threads in the * threadpool which are waiting for work. This method returns a * value which is not synchronized. */ public int numberOfAvailableThreads() { synchronized (lock) { return availableWorkerThreads; } } /** * This method will return the number of busy threads in the threadpool * This method returns a value which is not synchronized. */ public int numberOfBusyThreads() { synchronized (lock) { return (currentThreadCount - availableWorkerThreads); } } /** * This method returns the average elapsed time taken to complete a Work * item in milliseconds. */ public long averageWorkCompletionTime() { synchronized (lock) { return (totalTimeTaken / processedCount); } } /** * This method returns the number of Work items processed by the threadpool */ public long currentProcessedCount() { synchronized (lock) { return processedCount; } } public String getName() { return name; } /** * This method will return the number of WorkQueues serviced by the threadpool. */ public int numberOfWorkQueues() { return 1; } private static synchronized int getUniqueThreadId() { return ThreadPoolImpl.threadCounter++; } private class WorkerThread extends Thread { private Work currentWork; private int threadId = 0; // unique id for the thread // thread pool this WorkerThread belongs too private String threadPoolName; // name seen by Thread.getName() private StringBuffer workerThreadName = new StringBuffer(); WorkerThread(ThreadGroup tg, String threadPoolName) { super(tg, "Idle"); this.threadId = ThreadPoolImpl.getUniqueThreadId(); this.threadPoolName = threadPoolName; setName(composeWorkerThreadName(threadPoolName, "Idle")); } public void run() { while (true) { try { synchronized (lock) { availableWorkerThreads++; } // Get some work to do currentWork = ((WorkQueueImpl)workQueue).requestWork(inactivityTimeout); synchronized (lock) { availableWorkerThreads--; // It is possible in notifyForAvailableWork that the // check for availableWorkerThreads = 0 may return // false, because the availableWorkerThreads has not been // decremented to zero before the producer thread added // work to the queue. This may create a deadlock, if the // executing thread needs information which is in the work // item queued in the workqueue, but has no thread to work // on it since none was created because availableWorkerThreads = 0 // returned false. // The following code will ensure that a thread is always available // in those situations if ((availableWorkerThreads == 0) && (workQueue.workItemsInQueue() > 0)) { createWorkerThread(); } } // Set the thread name for debugging. setName(composeWorkerThreadName(threadPoolName, Integer.toString(this.threadId))); long start = System.currentTimeMillis(); try { // Do the work currentWork.doWork(); } catch (Throwable t) { // Ignore all errors. ; } long end = System.currentTimeMillis(); synchronized (lock) { totalTimeTaken += (end - start); processedCount++; } // set currentWork to null so that the work item can be // garbage collected currentWork = null; setName(composeWorkerThreadName(threadPoolName, "Idle")); } catch (TimeoutException e) { // This thread timed out waiting for something to do. synchronized (lock) { availableWorkerThreads--; // This should for both bounded and unbounded case if (currentThreadCount > minWorkerThreads) { currentThreadCount--; // This thread can exit. return; } else { // Go back to waiting on workQueue continue; } } } catch (InterruptedException ie) { // InterruptedExceptions are // caught here. Thus, threads can be forced out of // requestWork and so they have to reacquire the lock. // Other options include ignoring or // letting this thread die. // Ignoring for now. REVISIT synchronized (lock) { availableWorkerThreads--; } } catch (Throwable e) { // Ignore any exceptions that currentWork.process // accidently lets through, but let Errors pass. // Add debugging output? REVISIT synchronized (lock) { availableWorkerThreads--; } } } } private String composeWorkerThreadName(String poolName, String workerName) { workerThreadName.setLength(0); workerThreadName.append("p: ").append(poolName); workerThreadName.append("; w: ").append(workerName); return workerThreadName.toString(); } } // End of WorkerThread class}// End of file.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?