001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.lang.reflect.Field;
021    import java.lang.reflect.Method;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class ClearTimerThreadUtil {
027    
028            public static void clearTimerThread() throws Exception {
029                    if (!_INITIALIZED) {
030                            return;
031                    }
032    
033                    Thread[] threads = ThreadUtil.getThreads();
034    
035                    for (Thread thread : threads) {
036                            if (thread == null) {
037                                    continue;
038                            }
039    
040                            Class<?> threadClass = thread.getClass();
041    
042                            String threadClassName = threadClass.getName();
043    
044                            if (!threadClassName.equals("java.util.TimerThread")) {
045                                    continue;
046                            }
047    
048                            Object queue = _QUEUE_FIELD.get(thread);
049    
050                            synchronized (queue) {
051                                    _NEW_TASKS_MAY_BE_SCHEDULED_FIELD.setBoolean(thread, false);
052    
053                                    _CLEAR_METHOD.invoke(queue);
054    
055                                    queue.notify();
056                            }
057                    }
058            }
059    
060            private static final Method _CLEAR_METHOD;
061    
062            private static final boolean _INITIALIZED;
063    
064            private static final Field _NEW_TASKS_MAY_BE_SCHEDULED_FIELD;
065    
066            private static final Field _QUEUE_FIELD;
067    
068            private static final Log _log = LogFactoryUtil.getLog(
069                    ClearTimerThreadUtil.class);
070    
071            static {
072                    Method clearMethod = null;
073                    Field newTasksMayBeScheduledField = null;
074                    Field queueField = null;
075    
076                    boolean initialized = false;
077    
078                    try {
079                            Class<?> taskQueueClass = Class.forName("java.util.TaskQueue");
080    
081                            clearMethod = ReflectionUtil.getDeclaredMethod(
082                                    taskQueueClass, "clear");
083    
084                            Class<?> timeThreadClass = Class.forName("java.util.TimerThread");
085    
086                            newTasksMayBeScheduledField = ReflectionUtil.getDeclaredField(
087                                    timeThreadClass, "newTasksMayBeScheduled");
088                            queueField = ReflectionUtil.getDeclaredField(
089                                    timeThreadClass, "queue");
090    
091                            initialized = true;
092                    }
093                    catch (Throwable t) {
094                            if (_log.isWarnEnabled()) {
095                                    _log.warn("Failed to initialize ClearTimerThreadUtil");
096                            }
097                    }
098    
099                    _CLEAR_METHOD = clearMethod;
100    
101                    _NEW_TASKS_MAY_BE_SCHEDULED_FIELD = newTasksMayBeScheduledField;
102    
103                    _QUEUE_FIELD = queueField;
104    
105                    _INITIALIZED = initialized;
106            }
107    
108    }