01: /*
02: * Created on 20/01/2005
03: *
04: * Swing Components - visit http://sf.net/projects/gfd
05: *
06: * Copyright (C) 2004 Igor Regis da Silva Simões
07: *
08: * This program is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU General Public License
10: * as published by the Free Software Foundation; either version 2
11: * of the License, or (at your option) any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: * You should have received a copy of the GNU General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21: *
22: */
23: package br.com.gfp.util;
24:
25: import br.com.gfp.dao.GFPController;
26:
27: /**
28: * @author f4353008
29: * @since 20/01/2005 13:13:47
30: *
31: * To change the template for this generated type comment go to
32: * Window - Preferences - Java - Code Generation - Code and Comments
33: */
34: public class ThreadUtil {
35:
36: /**
37: * Faz com que a thread corrente espera pelo fim da thread com o nome passado como parametro
38: * @param threadName
39: */
40: public static final void waitFor(String threadName) {
41: Thread[] t = new Thread[Thread.activeCount()];
42: Thread.currentThread().getThreadGroup().enumerate(t);
43: for (int i = 0; i < t.length; i++) {
44: if (t[i] != null && t[i].getName().equals(threadName)) {
45: try {
46: t[i].join();
47: } catch (InterruptedException e) {
48: ((SimpleLog) GFPController.getGFPController()
49: .getContexto().get(GFPController.LOG))
50: .log("Erro ao aguardar Thread "
51: + threadName);
52: ((SimpleLog) GFPController.getGFPController()
53: .getContexto().get(GFPController.LOG))
54: .log(e.getLocalizedMessage());
55: ((SimpleLog) GFPController.getGFPController()
56: .getContexto().get(GFPController.LOG))
57: .log(e);
58: }
59: break;
60: }
61: }
62: }
63:
64: /**
65: * Envia um sinal de interrupt para a thread com o nome especificado
66: * @param threadName
67: */
68: public static final void interrupt(String threadName) {
69: Thread[] t = new Thread[Thread.activeCount()];
70: Thread.currentThread().getThreadGroup().enumerate(t);
71: for (int i = 0; i < t.length; i++) {
72: if (t[i] != null && t[i].getName().equals(threadName)) {
73: System.out.println("Matando " + threadName);
74: t[i].interrupt();
75: break;
76: }
77: }
78: }
79:
80: /**
81: * Indica se uma determinada Thread está rodando com base em seu nome
82: * @param threadName
83: * @return
84: */
85: public static boolean isRunning(String threadName) {
86: Thread[] t = new Thread[Thread.activeCount()];
87: Thread.currentThread().getThreadGroup().enumerate(t);
88: for (int i = 0; i < t.length; i++) {
89: if (t[i] != null && t[i].getName().equals(threadName)) {
90: return true;
91: }
92: }
93: return false;
94: }
95: }
|