001    /**
002     * Copyright (c) 2000-2013 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 = _queueField.get(thread);
049    
050                            synchronized (queue) {
051                                    _newTasksMayBeScheduledField.setBoolean(thread, false);
052    
053                                    _clearMethod.invoke(queue);
054    
055                                    queue.notify();
056                            }
057                    }
058            }
059    
060            private static Log _log = LogFactoryUtil.getLog(ClearTimerThreadUtil.class);
061    
062            private static Method _clearMethod;
063            private static boolean _initialized;
064            private static Field _newTasksMayBeScheduledField;
065            private static Field _queueField;
066    
067            static {
068                    try {
069                            Class<?> timeThreadClass = Class.forName("java.util.TimerThread");
070    
071                            _newTasksMayBeScheduledField = ReflectionUtil.getDeclaredField(
072                                    timeThreadClass, "newTasksMayBeScheduled");
073                            _queueField = ReflectionUtil.getDeclaredField(
074                                    timeThreadClass, "queue");
075    
076                            Class<?> taskQueueClass = Class.forName("java.util.TaskQueue");
077    
078                            _clearMethod = ReflectionUtil.getDeclaredMethod(
079                                    taskQueueClass, "clear");
080    
081                            _initialized = true;
082                    }
083                    catch (Throwable t) {
084                            _initialized = false;
085    
086                            if (_log.isWarnEnabled()) {
087                                    _log.warn("Failed to initialize ClearTimerThreadUtil");
088                            }
089                    }
090            }
091    
092    }