1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.daemon.impl.util;
1819import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
20import org.apache.jetspeed.services.logging.JetspeedLogger;
2122/***23Util class for joining a ThreadGroup and joining all of its Threads and waiting24for completion.2526@author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>27@version $Id: ThreadGroupJoin.java,v 1.10 2004/02/23 02:47:43 jford Exp $28*/29publicclassThreadGroupJoin30 {
31/***32 * Static initialization of the logger for this class33 */34privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(ThreadGroupJoin.class.getName());
3536/***37 Join a ThreadGroup and wait until it finishes38 */39publicstaticvoid join( ThreadGroup tg )
40 {
41//join the threadgroup but don't have a source.42 join( tg, null, 0 );
43 }
4445/***46 Join a ThreadGroup bug specify a source and use a default interval.47 */48publicstaticvoid join( ThreadGroup tg,
49 String source )
50 {
51 join( tg, source, 100 );
52 }
5354/***55 Join a ThreadGroup except also log when interval number of Threads have 56 finished.57 */58publicstaticvoid join( ThreadGroup tg,
59 String source,
60intinterval )
61 {
6263 Thread[] threads = new Thread[ tg.activeCount() ];
6465 tg.enumerate( threads );
6667//keep waiting until all the DownloadThreads have stopped.6869long begin = System.currentTimeMillis();
7071for ( int i = 0; i < threads.length; ++i ) {
7273if ( !threads[i].interrupted() )
74 {
75try76 {
77if ( threads[i] != null )
78 {
79 threads[i].join();
8081//if this is an even MOD of this interval and a source82//is defined then log it83if ( i != 0 &&
84 i % interval == 0 &&
85 source != null )
86 {
87long seconds = ( System.currentTimeMillis() - begin ) / 1000;
88 begin = System.currentTimeMillis();
8990if ( logger.isInfoEnabled() )
91 {
92 logger.info( source + ": has completed " + i + " threads in " + seconds + " second(s)" );
93 }
9495 }
9697 }
9899 }
100catch (InterruptedException e)
101 {
102 logger.info( "Thread: " + threads[i].getName() + " -> DONE");
103//noop. this is standard.104 }
105 }
106107 }
108109110 }
111112 }
113114