G
Guest
I'm an occasional java programmer for my own utilities. Some applets and
servlets, and few standalone applications.
*) standalone with threads is OK
I've written a test standalone application which happily uses concurrent
threads (on one hand it pings hosts on the local LAN, on the other it
retrieves info on which machine is on which slot in a switch, via an
http connection to the switch itself). This application runs once, does
most of its task in the main() method and outputs the result to
terminal.
Now the next step would be to integrate it with a swing GUI.
*) standalone with swing is also OK
The only other swing standalone application I had (and which I routinely
use), was based on a standard tutorial. Let's call it myClass ...
- myClass extends JPanel
- its main method calls javax.swing.SwingUtilities.invokeLater
to run() a createAndShowGUI() wrapper.
- the wrapper creates a frame, instantiates a new myGui(), sets
it as content, and maps the frame
- myGui generates all the gui with buttons and text areas necessary
to establish my actions on a mysql database. One of the components
of the GUI is a JComponent bottom, which ...
... is initialized as bottom = new myClass(table) (where table
is a database table name obtained as args to the main)
... it is regularly removed and replaced with a new instance of
myClass as I continue to issue different mysql queries
- the constructor myClass manages internally a swing JTable
*) putting swing and threads together
In the next step I designed the layout of my new GUI and used
the same paradigm as above
- the "monitor" class extends JPanel
- its main() calls invokeLater to run() a createAndShowGUI() wrapper.
- the wrapper creates a frame with a new myGui()
- myGui generates all the gui with buttons etc.
Now, where should I run my (time-consuming) threads ? So far I
started with a single thread (the most time-consuming). I was unsure
whether to put it into a monitor() METHOD or in a new monitor()
CONSTRUCTOR.
In the latter case I use a static JComponent dummy; and do
dummy=new monitor();
monitor() at some point does a tsw4.start();
where tsw4 is a static Thread tsw4 = new Thread(sw4);
and sw4 is an instance of a class which handles the http
connection. All this defined once forever in the prologue (global
to my topmost class)
Anyhow, I can do something of what I want in both cases (method or
constructor) provided that the invocation of monitor() occurs in the
main() after a suitable delay (say Thread.sleep(1000) from the
deferred creation of the GUI.
monitor() can run once, and can display its status messages to the
GUI. I can also use a button to break the thread tsw4 and update
the status.
In the future monitor() should become an infinite loop, which
should run all his threads to completion, display results, wait
for a programmable interval, and re-run the threads etc.
The idea is that I could break the threads (which I can already do)
and then use a button to start a new monitor
So far my "break" button does :
tsw4.interrupt();
dummy=null;
and my "restart" button does :
dummy=new monitor();
Actually I tried to put the dummy=null; soon after the interrupt
or soon before the new monitor();
In both cases I get a java.lang.IllegalThreadStateException on the
tsw4.start(); statement in the monitor() constructor
but what is the state of the thread ?
(and why is it illegal ?)
Note that monitor() does no graphics of its own. I tried also with a
monitor() method, and to call it in various places, but with obviously
worse results (e.g. monitor() giving null pointer if GUI start is not
completed, the GUI waiting to show up until monitor() threads are
completed, monitor() giving null pointer when trying to write to a
message area in the GUI, or simply monitor can run once, but a new run
is ineffective)
servlets, and few standalone applications.
*) standalone with threads is OK
I've written a test standalone application which happily uses concurrent
threads (on one hand it pings hosts on the local LAN, on the other it
retrieves info on which machine is on which slot in a switch, via an
http connection to the switch itself). This application runs once, does
most of its task in the main() method and outputs the result to
terminal.
Now the next step would be to integrate it with a swing GUI.
*) standalone with swing is also OK
The only other swing standalone application I had (and which I routinely
use), was based on a standard tutorial. Let's call it myClass ...
- myClass extends JPanel
- its main method calls javax.swing.SwingUtilities.invokeLater
to run() a createAndShowGUI() wrapper.
- the wrapper creates a frame, instantiates a new myGui(), sets
it as content, and maps the frame
- myGui generates all the gui with buttons and text areas necessary
to establish my actions on a mysql database. One of the components
of the GUI is a JComponent bottom, which ...
... is initialized as bottom = new myClass(table) (where table
is a database table name obtained as args to the main)
... it is regularly removed and replaced with a new instance of
myClass as I continue to issue different mysql queries
- the constructor myClass manages internally a swing JTable
*) putting swing and threads together
In the next step I designed the layout of my new GUI and used
the same paradigm as above
- the "monitor" class extends JPanel
- its main() calls invokeLater to run() a createAndShowGUI() wrapper.
- the wrapper creates a frame with a new myGui()
- myGui generates all the gui with buttons etc.
Now, where should I run my (time-consuming) threads ? So far I
started with a single thread (the most time-consuming). I was unsure
whether to put it into a monitor() METHOD or in a new monitor()
CONSTRUCTOR.
In the latter case I use a static JComponent dummy; and do
dummy=new monitor();
monitor() at some point does a tsw4.start();
where tsw4 is a static Thread tsw4 = new Thread(sw4);
and sw4 is an instance of a class which handles the http
connection. All this defined once forever in the prologue (global
to my topmost class)
Anyhow, I can do something of what I want in both cases (method or
constructor) provided that the invocation of monitor() occurs in the
main() after a suitable delay (say Thread.sleep(1000) from the
deferred creation of the GUI.
monitor() can run once, and can display its status messages to the
GUI. I can also use a button to break the thread tsw4 and update
the status.
In the future monitor() should become an infinite loop, which
should run all his threads to completion, display results, wait
for a programmable interval, and re-run the threads etc.
The idea is that I could break the threads (which I can already do)
and then use a button to start a new monitor
So far my "break" button does :
tsw4.interrupt();
dummy=null;
and my "restart" button does :
dummy=new monitor();
Actually I tried to put the dummy=null; soon after the interrupt
or soon before the new monitor();
In both cases I get a java.lang.IllegalThreadStateException on the
tsw4.start(); statement in the monitor() constructor
but what is the state of the thread ?
(and why is it illegal ?)
Note that monitor() does no graphics of its own. I tried also with a
monitor() method, and to call it in various places, but with obviously
worse results (e.g. monitor() giving null pointer if GUI start is not
completed, the GUI waiting to show up until monitor() threads are
completed, monitor() giving null pointer when trying to write to a
message area in the GUI, or simply monitor can run once, but a new run
is ineffective)