@Override
	public synchronized void run()
	{
		List<ActiveObject> inactiveList = new ArrayList<>();
		try
		{
			log.debug("Starting activity watchdog (wakeUpInterval="+WAKE_UP_INTERVAL+"ms)");
			while (!stop)
			{
				wait(WAKE_UP_INTERVAL);
				if (stop)
					return;
				
				// Check watchlist
				long now = System.currentTimeMillis();
				synchronized (watchList)
				{
					for (int i = 0; i < watchList.size(); i++)
					{
						WeakReference<ActiveObject> weakRef = watchList.get(i);
						ActiveObject obj = weakRef.get();
						if (obj == null)
						{
							// Object was garbage collected, remove and continue
							watchList.remove(i--);
							continue;
						}
					
						// Check object activity
						if ((now - obj.getLastActivity()) > obj.getTimeoutDelay())
						{
							log.trace("Marking object as inactive : "+obj);
							inactiveList.add(obj);
						}
					}
				}
				
				// Process inactive objects
				for (int n = 0; n < inactiveList.size(); n++)
				{
					ActiveObject obj = inactiveList.get(n);
					try
					{
						if (!obj.onActivityTimeout())
							inactiveList.remove(n--);
					}
					catch (Exception e)
					{
						log.error("Error notifying an inactive object",e);
					}
				}
				
				// Remove expired objects
				synchronized (watchList)
				{
					for(int n=0;n<inactiveList.size();n++)
					{
						ActiveObject targetObj = inactiveList.get(n);
						for (int i = 0; i < watchList.size(); i++)
						{
							WeakReference<ActiveObject> weakRef = watchList.get(i);
							ActiveObject obj = weakRef.get();
							if (obj == targetObj)
							{
								log.trace("Removing inactive object from watch list : "+targetObj);
								watchList.remove(i--);
								break;
							}
						}
					}
				}
		
				// Clear inactive object list
				inactiveList.clear();
			}
			log.debug("Activity watchdog stopped");
		}
		catch (Throwable e)
		{
			log.error("Activity watchdog failed",e);
		}
	}
@ Override public synchronized void run ( ) { List < ActiveObject > inactiveList = new ArrayList < > ( ) ; try { log . debug ( "Starting activity watchdog (wakeUpInterval=" + WAKE_UP_INTERVAL + "ms)" ) ; while ( ! stop ) { wait ( WAKE_UP_INTERVAL ) ; if ( stop ) return ; long now = System . currentTimeMillis ( ) ; synchronized ( watchList ) { for ( int i = 0 ; i < watchList . size ( ) ; i ++ ) { WeakReference < ActiveObject > weakRef = watchList . get ( i ) ; ActiveObject obj = weakRef . get ( ) ; if ( obj == null ) { watchList . remove ( i -- ) ; continue ; } if ( ( now - obj . getLastActivity ( ) ) > obj . getTimeoutDelay ( ) ) { log . trace ( "Marking object as inactive : " + obj ) ; inactiveList . add ( obj ) ; } } } for ( int n = 0 ; n < inactiveList . size ( ) ; n ++ ) { ActiveObject obj = inactiveList . get ( n ) ; try { if ( ! obj . onActivityTimeout ( ) ) inactiveList . remove ( n -- ) ; } catch ( Exception e ) { log . error ( "Error notifying an inactive object" , e ) ; } } synchronized ( watchList ) { for ( int n = 0 ; n < inactiveList . size ( ) ; n ++ ) { ActiveObject targetObj = inactiveList . get ( n ) ; for ( int i = 0 ; i < watchList . size ( ) ; i ++ ) { WeakReference < ActiveObject > weakRef = watchList . get ( i ) ; ActiveObject obj = weakRef . get ( ) ; if ( obj == targetObj ) { log . trace ( "Removing inactive object from watch list : " + targetObj ) ; watchList . remove ( i -- ) ; break ; } } } } inactiveList . clear ( ) ; } log . debug ( "Activity watchdog stopped" ) ; } catch ( Throwable e ) { log . error ( "Activity watchdog failed" , e ) ; } }
/* (non-Javadoc)
@see java.lang.Thread#run()