KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > requestplayer > MonitoringThread


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.requestplayer;
26
27 import org.objectweb.cjdbc.common.util.Stats;
28
29 /**
30  * Displays the number of requests processed at a given time period.
31  *
32  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
33  * @version 1.0
34  */

35 public class MonitoringThread extends Thread JavaDoc
36 {
37   /** Number of read requests. */
38   private Stats selectStats = null;
39
40   /** Number of unknown requests. */
41   private Stats unknownStats = null;
42
43   /** Number of update requests. */
44   private Stats updateStats = null;
45
46   /** Number of insert requests. */
47   private Stats insertStats = null;
48
49   /** Number of delete requests. */
50   private Stats deleteStats = null;
51
52   /** Time to wait between 2 outputs in milleseconds. */
53   private long timeInMs; //
54

55   /** <code>true</code> if this thread has been killed. */
56   private boolean killed = false;
57
58   /**
59    * Creates a new <code>MonitoringThread</code>.
60    *
61    * @param father the client emulator.
62    * @param timeInMs the time to wait between 2 outputs in milleseconds.
63    */

64   public MonitoringThread(ClientEmulator father, long timeInMs)
65   {
66     super("MonitoringThread");
67
68     // Init the pointers to the stats
69
selectStats = father.getSelectStats();
70     unknownStats = father.getUnknownStats();
71     updateStats = father.getUpdateStats();
72     insertStats = father.getInsertStats();
73     deleteStats = father.getDeleteStats();
74
75     this.timeInMs = timeInMs;
76   }
77
78   /**
79    * @see java.lang.Runnable#run()
80    */

81   public void run()
82   {
83     int oldStats = 0;
84     int currentStats;
85     for (int i = 0; !killed; i++)
86     {
87       try
88       {
89         Thread.sleep(timeInMs);
90       }
91       catch (InterruptedException JavaDoc e)
92       {
93         System.out.println("Monitoring thread interrupted");
94         break;
95       }
96       currentStats = selectStats.getCount() + updateStats.getCount()
97           + insertStats.getCount() + deleteStats.getCount()
98           + unknownStats.getCount();
99       System.out.println("Monitor:" + i + ": " + (currentStats - oldStats));
100       oldStats = currentStats;
101     }
102   }
103
104   /**
105    * Returns <code>true</code> if this thread has been killed.
106    *
107    * @return a <code>boolean</code>
108    */

109   public boolean isKilled()
110   {
111     return killed;
112   }
113
114   /**
115    * Used to stop the execution of this thread.
116    *
117    * @param killed <code>boolean</code>.
118    */

119   public void setKilled(boolean killed)
120   {
121     this.killed = killed;
122   }
123 }
Popular Tags