1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 tg )
40 {
41
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 tg,
49 String 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 tg,
59 String source,
60 int interval )
61 {
62
63 Thread[] threads = new Thread[ tg.activeCount() ];
64
65 tg.enumerate( threads );
66
67
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
82
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 e)
101 {
102 logger.info( "Thread: " + threads[i].getName() + " -> DONE");
103
104 }
105 }
106
107 }
108
109
110 }
111
112 }
113
114