KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > daemon > impl > util > ThreadGroupJoin


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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.daemon.impl.util;
18
19 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
20 import org.apache.jetspeed.services.logging.JetspeedLogger;
21
22 /**
23 Util class for joining a ThreadGroup and joining all of its Threads and waiting
24 for completion.
25
26 @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 */

29 public class ThreadGroupJoin
30 {
31     /**
32      * Static initialization of the logger for this class
33      */

34     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(ThreadGroupJoin.class.getName());
35     
36     /**
37     Join a ThreadGroup and wait until it finishes
38     */

39     public static void join( ThreadGroup JavaDoc tg )
40     {
41         //join the threadgroup but don't have a source.
42
join( tg, null, 0 );
43     }
44
45     /**
46     Join a ThreadGroup bug specify a source and use a default interval.
47     */

48     public static void join( ThreadGroup JavaDoc tg,
49                              String JavaDoc source )
50     {
51         join( tg, source, 100 );
52     }
53     
54     /**
55     Join a ThreadGroup except also log when interval number of Threads have
56     finished.
57     */

58     public static void join( ThreadGroup JavaDoc tg,
59                              String JavaDoc source,
60                              int interval )
61     {
62
63         Thread JavaDoc[] threads = new Thread JavaDoc[ tg.activeCount() ];
64         
65         tg.enumerate( threads );
66
67         //keep waiting until all the DownloadThreads have stopped.
68

69         long begin = System.currentTimeMillis();
70         
71         for ( int i = 0; i < threads.length; ++i ) {
72
73             if ( !threads[i].interrupted() )
74             {
75                 try
76                 {
77                     if ( threads[i] != null )
78                     {
79                         threads[i].join();
80                         
81                         //if this is an even MOD of this interval and a source
82
//is defined then log it
83
if ( i != 0 &&
84                             i % interval == 0 &&
85                             source != null )
86                         {
87                             long seconds = ( System.currentTimeMillis() - begin ) / 1000;
88                             begin = System.currentTimeMillis();
89                             
90                             if ( logger.isInfoEnabled() )
91                             {
92                                 logger.info( source + ": has completed " + i + " threads in " + seconds + " second(s)" );
93                             }
94                             
95                         }
96                         
97                     }
98
99                 }
100                 catch (InterruptedException JavaDoc e)
101                 {
102                     logger.info( "Thread: " + threads[i].getName() + " -> DONE");
103                     //noop. this is standard.
104
}
105            }
106
107         }
108         
109         
110     }
111     
112 }
113     
114   
115
Popular Tags