KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > threads > tutorial > ThreadConsumer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.threads.tutorial;
19
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.service.Serviceable;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.activity.Initializable;
25 import org.apache.avalon.framework.activity.Disposable;
26
27 import org.apache.excalibur.thread.ThreadControl;
28 import org.apache.excalibur.thread.ThreadPool;
29
30 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
31
32 /**
33  * ThreadConsumer is a example of a component that uses the ThreadManager
34  * service to aquired a thread pool.
35  *
36  * @avalon.component name="consumer" lifestyle="singleton" version="1.0"
37  * @author Stephen McConnell
38  */

39 public class ThreadConsumer extends AbstractLogEnabled implements
40 Serviceable, Initializable, Disposable
41 {
42    /**
43     * The service manager from which serrvices are aquired and released.
44     */

45     private ServiceManager m_manager;
46
47    /**
48     * The cornerstone thread manager.
49     */

50     private ThreadManager m_threads;
51
52    /**
53     * A thread pool aquired from the thread manager.
54     */

55     private ThreadPool m_pool;
56
57    /**
58     * A thread control return from the launching of a new thread.
59     */

60     private ThreadControl m_control;
61
62
63    /**
64     * Servicing of the component by the container during which the
65     * component aquires the ThreadManager service.
66     *
67     * @param manager the thread manager
68     * @exception ServiceException if the thread manager service is
69     * unresolvable
70     * @avalon.dependency key="threads"
71     * type="org.apache.avalon.cornerstone.services.threads.ThreadManager"
72     */

73     public void service( ServiceManager manager ) throws ServiceException
74     {
75         m_manager = manager;
76         getLogger().info( "aquiring cornerstone threads service" );
77         m_threads = (ThreadManager) m_manager.lookup( "threads" );
78     }
79
80    /**
81     * Initialization of the component by the container during which we
82     * establish a child thread by passing a runnable object to the thread pool.
83     * @exception Exception if an initialization stage error occurs
84     */

85     public void initialize() throws Exception JavaDoc
86     {
87         getLogger().info( "initialization" );
88
89         //
90
// get the default thread pool
91
//
92

93         m_pool = m_threads.getDefaultThreadPool();
94
95         //
96
// create a runnable object to run
97
//
98

99         Counter counter = new Counter();
100         counter.enableLogging( getLogger().getChildLogger( "counter" ) );
101
102         //
103
// start a thread and get the thread control reference
104
//
105

106         m_control = m_pool.execute( counter );
107     }
108
109    /**
110     * Disposal of the component during which he handle the closue of the
111     * child thread we have establshed during the initialization stage.
112     */

113     public void dispose()
114     {
115         getLogger().info( "disposal" );
116
117         if( ( m_control != null ) && !m_control.isFinished() )
118         {
119             //
120
// interrupt the child
121
//
122

123             getLogger().info( "disposal invoked while child thread active" );
124             m_control.interrupt();
125
126             //
127
// Using m_control.join() locks things up - why? Using a
128
// wait for finished state instead.
129
//
130

131             while( !m_control.isFinished() )
132             {
133                 getLogger().info( "waiting for child" );
134                 try
135                 {
136                     Thread.sleep( 1000 );
137                 }
138                 catch( InterruptedException JavaDoc ie )
139                 {
140                     // ignore it
141
}
142             }
143         }
144
145         //
146
// check for errors
147
//
148

149         if( ( m_control != null ) && ( m_control.getThrowable() != null ) )
150         {
151             getLogger().warn(
152               "thread terminated with exception condition",
153                m_control.getThrowable() );
154         }
155
156         if( m_pool != null )
157         {
158             if( m_pool instanceof Disposable )
159             {
160                 ((Disposable)m_pool).dispose();
161             }
162             m_pool = null;
163         }
164
165         m_manager.release( m_threads );
166
167         m_control = null;
168         m_threads = null;
169         m_manager = null;
170
171         getLogger().info( "disposed" );
172     }
173 }
174
175
Popular Tags