public CoreScheduler( ScheduledExecutorService executorService, SyncTaskManager syncTaskManager ) { this.executorService = executorService; this.syncTaskManager = syncTaskManager; // Check for long execution timings ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); this.executorService.scheduleWithFixedDelay( () -> { long current = System.currentTimeMillis(); synchronized ( threads ) { Object2LongMap.FastEntrySet<Thread> threadSet = (Object2LongMap.FastEntrySet<Thread>) threads.object2LongEntrySet(); ObjectIterator<Object2LongMap.Entry<Thread>> threadIterator = threadSet.fastIterator(); while ( threadIterator.hasNext() ) { Object2LongMap.Entry<Thread> entry = threadIterator.next(); long diff = current - entry.getLongValue(); if ( diff > TimeUnit.SECONDS.toMillis( 10 ) ) { ThreadInfo threadInfo = mxBean.getThreadInfo( entry.getKey().getId() ); Thread.State state = threadInfo.getThreadState(); if ( !alreadyAlerted.contains( entry.getKey() ) && ( state == Thread.State.WAITING || state == Thread.State.TIMED_WAITING || state == Thread.State.BLOCKED ) ) { LOGGER.warn( "Following runnable is blocking the scheduler loops: {}", threadRunnables.get( entry.getKey() ).getClass().getName() ); threadInfo = mxBean.getThreadInfo( entry.getKey().getId(), Integer.MAX_VALUE ); for ( StackTraceElement element : threadInfo.getStackTrace() ) { LOGGER.warn( " {}", element ); } alreadyAlerted.add( entry.getKey() ); } } } } }, 10, 10, TimeUnit.MILLISECONDS ); }
/** * Returns an iterator that will be {@linkplain FastEntrySet fast}, if possible, * on the {@linkplain Map#entrySet() entry set} of the provided {@code map}. * * @param map * a map from which we will try to extract a (fast) iterator on the * entry set. * @return an iterator on the entry set of the given map that will be fast, if * possible. * @since 8.0.0 */ @SuppressWarnings("unchecked") public static <K> ObjectIterator<Object2LongMap.Entry<K>> fastIterator(Object2LongMap<K> map) { final ObjectSet<Object2LongMap.Entry<K>> entries = map.object2LongEntrySet(); return entries instanceof Object2LongMap.FastEntrySet ? ((Object2LongMap.FastEntrySet<K>) entries).fastIterator() : entries.iterator(); } /**