1 /*
2 * Fsgrep is a simple Java application which allows a user to
3 * search all files in a directory structure for lines matching
4 * a given pattern. Its functionality is a combination of the
5 * Unix 'find' and 'grep' utilities.
6 * Visit [http://fsgrep.sourceforge.net/] for more information.
7 *
8 * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9 *
10 * Fsgrep is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * Fsgrep is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Fsgrep (see the file named LICENSE.txt); if not, write
21 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22 * Boston, MA 02111-1307 USA
23 */
24
25 package mk.fsgrep.util.thread;
26
27
28
29
30 /***
31 * Execute a runnable in a new thread and then trigger a callback when
32 * finished.
33 *
34 * @author Murali Krishnan
35 *
36 */
37 public class ThreadRunner implements Runnable {
38
39 //------------------------------------------------------------
40 //- Class Variables
41
42
43
44 //------------------------------------------------------------
45 //- Class Functions
46
47
48
49 //------------------------------------------------------------
50 //- Instance Variables
51
52 private String _name = null;
53 private RunCallback _callback = null;
54 private Thread _thread = null;
55 private Runnable _action = null;
56
57
58
59 //------------------------------------------------------------
60 //- Constructors
61
62 public ThreadRunner(String pName, RunCallback pCallback) {
63 _name = pName;
64 _callback = pCallback;
65 }
66
67
68 //------------------------------------------------------------
69 //- Accessors
70
71 public String getName() {return _name;}
72 public RunCallback getCallback() {return _callback;}
73 public Thread getThread() {return _thread;}
74 public Runnable getAction() {return _action;}
75
76
77
78 //------------------------------------------------------------
79 //- Settors
80
81 public void setThread(Thread val) {_thread=val;}
82 public void setAction(Runnable val) {_action=val;}
83
84
85 //------------------------------------------------------------
86 //- Private/Protected Utility Functions
87
88
89
90 //------------------------------------------------------------
91 //- Public Interface Functions
92
93 public String toString() {
94 String result = "ThreadRunner[" + getName() + "]";
95
96 return result;
97 }
98
99
100 public void execute(Runnable runnable) {
101 setAction(runnable);
102 setThread(new Thread(this, getName()));
103 // getThread().setDaemon(true);
104
105 getThread().start();
106 }
107
108
109 //------------------------------------------------------------
110 //- Class Interface Functions
111
112 public void run() {
113 try {
114 getAction().run();
115 } catch (Throwable thr) {
116 System.err.println("Fatal error in " + this);
117 thr.printStackTrace();
118 }
119
120 getCallback().runFinished(this);
121 }
122
123
124 //------------------------------------------------------------
125 //- Inner Classes
126
127
128
129 //------------------------------------------------------------
130 //- Main
131
132
133
134 }