1824 * completed execution. Because the states of tasks and threads
1825 * may change dynamically during computation, the returned value
1826 * is only an approximation, but one that does not ever decrease
1827 * across successive calls.
1828 *
1829 * @return the number of tasks
1830 */
1831 public long getCompletedTaskCount() {
1832 final ReentrantLock mainLock = this.mainLock;
1833 mainLock.lock();
1834 try {
1835 long n = completedTaskCount;
1836 for (Worker w : workers)
1837 n += w.completedTasks;
1838 return n;
1839 } finally {
1840 mainLock.unlock();
1841 }
1842 }
1843
1844 /* Extension hooks */
1845
1846 /**
1847 * Method invoked prior to executing the given Runnable in the
1848 * given thread. This method is invoked by thread {@code t} that
1849 * will execute task {@code r}, and may be used to re-initialize
1850 * ThreadLocals, or to perform logging.
1851 *
1852 * <p>This implementation does nothing, but may be customized in
1853 * subclasses. Note: To properly nest multiple overridings, subclasses
1854 * should generally invoke {@code super.beforeExecute} at the end of
1855 * this method.
1856 *
1857 * @param t the thread that will run task {@code r}
1858 * @param r the task that will be executed
1859 */
1860 protected void beforeExecute(Thread t, Runnable r) { }
1861
1862 /**
1863 * Method invoked upon completion of execution of the given Runnable.
1944 }
1945
1946 /**
1947 * A handler for rejected tasks that throws a
1948 * {@code RejectedExecutionException}.
1949 */
1950 public static class AbortPolicy implements RejectedExecutionHandler {
1951 /**
1952 * Creates an {@code AbortPolicy}.
1953 */
1954 public AbortPolicy() { }
1955
1956 /**
1957 * Always throws RejectedExecutionException.
1958 *
1959 * @param r the runnable task requested to be executed
1960 * @param e the executor attempting to execute this task
1961 * @throws RejectedExecutionException always.
1962 */
1963 public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
1964 throw new RejectedExecutionException();
1965 }
1966 }
1967
1968 /**
1969 * A handler for rejected tasks that silently discards the
1970 * rejected task.
1971 */
1972 public static class DiscardPolicy implements RejectedExecutionHandler {
1973 /**
1974 * Creates a {@code DiscardPolicy}.
1975 */
1976 public DiscardPolicy() { }
1977
1978 /**
1979 * Does nothing, which has the effect of discarding task r.
1980 *
1981 * @param r the runnable task requested to be executed
1982 * @param e the executor attempting to execute this task
1983 */
1984 public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
|
1824 * completed execution. Because the states of tasks and threads
1825 * may change dynamically during computation, the returned value
1826 * is only an approximation, but one that does not ever decrease
1827 * across successive calls.
1828 *
1829 * @return the number of tasks
1830 */
1831 public long getCompletedTaskCount() {
1832 final ReentrantLock mainLock = this.mainLock;
1833 mainLock.lock();
1834 try {
1835 long n = completedTaskCount;
1836 for (Worker w : workers)
1837 n += w.completedTasks;
1838 return n;
1839 } finally {
1840 mainLock.unlock();
1841 }
1842 }
1843
1844 /**
1845 * Returns a string identifying this pool, as well as its state,
1846 * including indications of run state and estimated worker and
1847 * task counts.
1848 *
1849 * @return a string identifying this pool, as well as its state
1850 */
1851 public String toString() {
1852 long ncompleted;
1853 int nworkers, nactive;
1854 final ReentrantLock mainLock = this.mainLock;
1855 mainLock.lock();
1856 try {
1857 ncompleted = completedTaskCount;
1858 nactive = 0;
1859 nworkers = workers.size();
1860 for (Worker w : workers) {
1861 ncompleted += w.completedTasks;
1862 if (w.isLocked())
1863 ++nactive;
1864 }
1865 } finally {
1866 mainLock.unlock();
1867 }
1868 int c = ctl.get();
1869 String rs = (runStateLessThan(c, SHUTDOWN) ? "Running" :
1870 (runStateAtLeast(c, TERMINATED) ? "Terminated" :
1871 "Shutting down"));
1872 return super.toString() +
1873 "[" + rs +
1874 ", pool size = " + nworkers +
1875 ", active threads = " + nactive +
1876 ", queued tasks = " + workQueue.size() +
1877 ", completed tasks = " + ncompleted +
1878 "]";
1879 }
1880
1881 /* Extension hooks */
1882
1883 /**
1884 * Method invoked prior to executing the given Runnable in the
1885 * given thread. This method is invoked by thread {@code t} that
1886 * will execute task {@code r}, and may be used to re-initialize
1887 * ThreadLocals, or to perform logging.
1888 *
1889 * <p>This implementation does nothing, but may be customized in
1890 * subclasses. Note: To properly nest multiple overridings, subclasses
1891 * should generally invoke {@code super.beforeExecute} at the end of
1892 * this method.
1893 *
1894 * @param t the thread that will run task {@code r}
1895 * @param r the task that will be executed
1896 */
1897 protected void beforeExecute(Thread t, Runnable r) { }
1898
1899 /**
1900 * Method invoked upon completion of execution of the given Runnable.
1981 }
1982
1983 /**
1984 * A handler for rejected tasks that throws a
1985 * {@code RejectedExecutionException}.
1986 */
1987 public static class AbortPolicy implements RejectedExecutionHandler {
1988 /**
1989 * Creates an {@code AbortPolicy}.
1990 */
1991 public AbortPolicy() { }
1992
1993 /**
1994 * Always throws RejectedExecutionException.
1995 *
1996 * @param r the runnable task requested to be executed
1997 * @param e the executor attempting to execute this task
1998 * @throws RejectedExecutionException always.
1999 */
2000 public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
2001 throw new RejectedExecutionException("Task " + r.toString() +
2002 " rejected from " +
2003 e.toString());
2004 }
2005 }
2006
2007 /**
2008 * A handler for rejected tasks that silently discards the
2009 * rejected task.
2010 */
2011 public static class DiscardPolicy implements RejectedExecutionHandler {
2012 /**
2013 * Creates a {@code DiscardPolicy}.
2014 */
2015 public DiscardPolicy() { }
2016
2017 /**
2018 * Does nothing, which has the effect of discarding task r.
2019 *
2020 * @param r the runnable task requested to be executed
2021 * @param e the executor attempting to execute this task
2022 */
2023 public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
|