KickJava   Java API By Example, From Geeks To Geeks.

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


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.LogEnabled;
21 import org.apache.avalon.framework.logger.Logger;
22
23 /**
24  * A demonstration runnable object that simply logs a countdown sequence.
25  *
26  * @author Stephen McConnell
27  * @avalon.component name="counter"
28  */

29 public class Counter extends Thread JavaDoc implements LogEnabled
30 {
31    /**
32     * The supplied logging channel.
33     */

34     private Logger m_logger;
35
36     private int m_count = 10;
37
38     public void enableLogging( Logger logger )
39     {
40         m_logger = logger;
41     }
42
43     protected Logger getLogger()
44     {
45         return m_logger;
46     }
47
48     public void run()
49     {
50         while( m_count > 0 )
51         {
52             getLogger().info( "count: " + m_count );
53             m_count--;
54             try
55             {
56                 sleep( 1000 );
57             }
58             catch( Throwable JavaDoc e )
59             {
60                 getLogger().info( "I've been interrupted." );
61                 m_count = -1;
62             }
63         }
64         getLogger().info( "Time to die." );
65         m_logger = null;
66     }
67 }
68
69
Popular Tags