diff -r 8ff882030755 src/share/vm/runtime/thread.cpp --- a/src/share/vm/runtime/thread.cpp Wed Jan 28 16:45:35 2015 -0800 +++ b/src/share/vm/runtime/thread.cpp Tue Feb 03 13:52:32 2015 -0500 @@ -1318,24 +1318,20 @@ } void WatcherThread::stop() { - // Get the PeriodicTask_lock if we can. If we cannot, then the - // WatcherThread is using it and we don't want to block on that lock - // here because that might cause a safepoint deadlock depending on - // what the current WatcherThread tasks are doing. - bool have_lock = PeriodicTask_lock->try_lock(); - _should_terminate = true; - OrderAccess::fence(); // ensure WatcherThread sees update in main loop - - if (have_lock) { + { + // Let safepoints happen to avoid the deadlock: + // The VM thread has Threads_lock and waits for Java threads to report back for a safepoint. + // The watcher thread has PeriodicTask_lock and tries to acquire Threads_lock. + // A Java thread executes WatcherThread::stop, tries to acquire PeriodicTask_lock, + // and holds up the VM thread by not reaching a safepoint. + MutexLocker mu(PeriodicTask_lock); + WatcherThread* watcher = watcher_thread(); if (watcher != NULL) { - // If we managed to get the lock, then we should unpark the - // WatcherThread so that it can see we want it to stop. + // Unpark the WatcherThread so that it can see that it should terminate. watcher->unpark(); } - - PeriodicTask_lock->unlock(); } // it is ok to take late safepoints here, if needed