Using Runnable Threads
Hi all,
I am fairly new to java but am developing a java application that will
enable the user to shutdown a room of computers down remotely. It is
shutting down computers by using the feature within Windows via the
command prompt. However when doing it through my program what happens
is if a computer has been selected to shutdown but that computer has
no network connection or is turned off the program waits for 40
seconds before sending the shutdown request onto the next computer.
What i want the program to do is for each computer it has to go it has
to be threaded so the program can continue to send the shutdown
requests onto all the computer while at the same time waiting for
other computer to try and get a connection. This is the code that I
have used so far in order to create the Thread.
package remoteshutdown;
import java.io.BufferedInputStream; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream; import java.io.InputStreamReader; import
java.io.RandomAccessFile; import java.text.DateFormatSymbols; import
java.util.Calendar;
public class ShutdownTest {
public void shutdown() { System.out.println("gone into public void
shutdown();"); if
(remoteshutdown.mainScreen.chkShutdownMsg.isSelected()) { System.out
.println("Inside if statement, about to start new thread"); Runnable
runnable = new normalShutdown(); Thread thread = new
Thread(runnable); }
}
class normalShutdown implements Runnable {
public void run() { System.out.println("performing shutdown");
String
choice = (String) mainScreen.lstComputerNames
.getSelectedValue(); System.out.println(choice);
// will be -1 if there aare none or muliple selections. int
which = mainScreen.lstComputerNames.getSelectedIndex();
System.out.println(which);
// detecting multiple selections System.out.println("--
multiples--");
Object[] choices =
mainScreen.lstComputerNames.getSelectedValues(); for (Object
aChoice : choices) { String cmd; cmd = "shutdown -m \\\\" +
aChoice + " -s -f"; try { Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd); InputStream i_stream =
p.getInputStream(); InputStreamReader reader =
new InputStreamReader(i_stream);
InputStream out = new BufferedInputStream(p
.getInputStream());
String s = null; String dayNames[] = new
DateFormatSymbols().getWeekdays(); Calendar
date2 = Calendar.getInstance();
System.out.println("Today is a " +
dayNames[date2.get(Calendar.DAY_OF_WEEK)]); File
f = new File( "C:\\Documents and Settings\\All
Users\\Application Data\\Remote
Shutdown\\ShutdownLog(" +
dayNames[date2.get(Calendar.DAY_OF_WEEK)] +
").html"); RandomAccessFile raf = new
RandomAccessFile(f, "rw"); raf.seek(f.length());
BufferedReader bf_reader = new
BufferedReader(reader); while ((s =
bf_reader.readLine()) != null) {
raf.writeChars("<B><FONT COLOR=RED>" + aChoice +
" failed to shutdown: " + s + "</FONT></B>
<BR>"); raf.close(); byte[] b = new byte[1024];
System.out.print(s); } p.waitFor(); p.destroy();
while ((s = bf_reader.readLine()) == null) {
raf.writeChars("<B>" + aChoice + " Shutdown
successfully</B><BR>"); raf.close(); }
p.waitFor(); p.destroy(); } catch (Exception e)
{ System.out .println("execution error" + cmd +
e.getMessage()); } }
}
} }
Appologise if it is not very clear. This is what the code is doing. It
is going into the public void section fine and is going down to the
bottom of that function in order to create the new thread. However the
program is not creating the Thread and therefore the Runnable section
of the program is not being executed. Thanks for any one who can off
me any help in this matter.
Any help in this matter would be highly appreciated.
Thank you