View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.agent;
22  
23  import java.util.Hashtable;
24  
25  /***
26   * Assign number to each thread.
27   * 
28   * Created on 03.12.2004
29   *
30   * Contributing Author(s):
31   *
32   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
34   *
35   * @author vlads
36   * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/07 09:04:52 $
37   */
38  public class ThreadNumber implements Event {
39  	/***
40  	 * Stable <code>serialVersionUID</code>.
41  	 */
42  	private static final long serialVersionUID = 29132124L;
43  	
44  	static Hashtable threadNumber = new Hashtable(100);
45  	static int count = 0;
46  	
47  	String name;
48  	
49  	String groupName;
50  	
51  	int localNumber;
52  	
53  	int groupActiveCount;
54  	
55  	boolean daemon;
56  	/*
57  	 * what ever extencion I will need.
58  	 */
59  	private String description;
60  	
61  	public ThreadNumber(Thread key, int count) {
62  	    this.localNumber = count;
63  	    this.name = key.getName();
64  	    this.groupName = key.getThreadGroup().getName();
65  	    this.groupActiveCount = key.getThreadGroup().activeCount();
66  	}
67  	
68  	public static ThreadNumber getThreadNumberEvent(Thread key) {
69          ThreadNumber i = (ThreadNumber) threadNumber.get(key);
70          if (i == null) {
71  			synchronized (threadNumber) {
72  			    i = new ThreadNumber(key, ++ count);
73  				threadNumber.put(key, i);
74  			}
75          }
76          return i;	    
77  	}
78  	
79      public static int get(Thread key) {
80          ThreadNumber i = getThreadNumberEvent(key);
81          return i.localNumber;
82      }
83      
84      public static String getUniqueThreadName(ThreadNumber thread, SystemInfoEvent sysinfo) {
85          return sysinfo.getRunID() + " " + thread.getLocalNumber() + " "+ thread.getName();
86      }
87      
88      /***
89       * @return Returns the description.
90       */
91      public String getDescription() {
92          return description;
93      }
94      /***
95       * @return Returns the groupActiveCount.
96       */
97      public int getGroupActiveCount() {
98          return groupActiveCount;
99      }
100     /***
101      * @return Returns the groupName.
102      */
103     public String getGroupName() {
104         return groupName;
105     }
106     /***
107      * @return Returns the localNumber.
108      */
109     public int getLocalNumber() {
110         return localNumber;
111     }
112     /***
113      * @return Returns the name.
114      */
115     public String getName() {
116         return name;
117     }
118 }