KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > timer > WorkerThread


1 /**
2 * The XMOJO Project 5
3 * Copyright © 2003 XMOJO.org. All rights reserved.
4
5 * NO WARRANTY
6
7 * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
8 * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
9 * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
10 * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
11 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
13 * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
14 * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
15 * REPAIR OR CORRECTION.
16
17 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
18 * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
19 * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
20 * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
21 * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
22 * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
23 * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
24 * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGES.
26 **/

27
28 package javax.management.timer;
29
30 import java.util.*;
31
32 /** A WorkerThread for running tasks in a Java VM */
33
34 class WorkerThread extends Thread JavaDoc
35 {
36
37     Scheduler JavaDoc scheduler = null;
38
39     // for logging
40
public int m_current_indent_count = 0;
41     public StringBuffer JavaDoc m_current_indent_buffer = null;
42
43     /** The constructor may not be necessary **/
44     public WorkerThread(Scheduler JavaDoc s,String JavaDoc nam)
45     {
46         super(nam);
47         scheduler = s;
48         m_current_indent_buffer = new StringBuffer JavaDoc();
49     }
50
51     /** Wake this one up, if waiting **/
52     synchronized void wakeUp()
53     {
54         notifyAll();
55     }
56
57     /** Wait here until asked to to another task **/
58     synchronized void waitUntilAsked()
59     {
60         while ((scheduler.ready_tasks.size() == 0) && (!scheduler.STOP_ALL))
61         {
62             try
63             {
64                 wait(100);
65             }
66             catch (InterruptedException JavaDoc iex) {}
67         }
68     }
69
70     /** The run methos does the tasks as they come in **/
71     public void run()
72     {
73         while ( (!scheduler.STOP_ALL) && (!scheduler.STOP_THIS) )
74             try
75             {
76                 waitUntilAsked();
77                 Runnable JavaDoc task = scheduler.getNextTask();
78                 if (task != null) task.run();
79             }
80             catch (Exception JavaDoc ex)
81             {
82                 System.err.println("Exception running task: "+ ex);
83 // ex.printStackTrace();
84
}
85         scheduler.NUM_THREADS_STOPPED += 1;
86         scheduler.TOTAL_THREADS -= 1;
87     }
88
89 }
Popular Tags