I'm trying to make a program that will shutdown my computer after a certain time that I will input via GUI. I'm wondering if I did it the correct way.
Also, will this slow down my computer while it waits to shut off? Do you see anything wrong with the code?
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class PCShutdown {
private int minutes;
private int hours;
private void setHours(int hoursArg) {
this.hours = hoursArg;
}
private void setMinutes(int minutesArg) {
this.minutes = minutesArg;
}
private void shutDown() {
Thread shutDownThread = new Thread(new ShutDownThread());
shutDownThread.start();
}
private class ShutDownThread implements Runnable {
@Override
public void run() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec("shutdown -s -t 30");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} finally { process.destroy();}
}
}, (hours * 60 * 60 * 1000) + (minutes * 60 * 1000));
}
}
}
Also, I still have to figure out how to kill all open programs except for this one before it shuts down. I'm going to work on that now.
Also, this might be a very dumb question, but will any of these operations harm my computer in any way? Will it be better if I just learn to do this via powershell?
1 Answer 1
Your questions at the end are the most important points here, so I answer those first:
but will any of these operations harm my computer in any way?
No, and how could it anyway?
Will it be better if I just learn to do this via powershell?
Yes! Writing a Java program for this is overkill!
In terms of a Java implementation, I think you're really overcomplicating it:
- Why use a thread to do this? As you're not going to do anything else in parallel, it's pointless.
- Why use a timer? Essentially the same argument applies here too as with threads: you're not doing anything else in parallel, so what's the point? And putting a timer inside a thread seems doubly pointless.
Maybe you assumed that by running this in a thread, the application will just schedule the task and exit. That is not the case. The JVM will continue running until the task is completed, and the program will stay in the foreground. You would have to background the process in the calling shell.
As such, you could replace this with a much simpler implementation:
public static void main(String[] args) throws InterruptedException, IOException {
if (args.length < 2) {
System.out.println("usage: java PCShutdown HOURS MINUTES");
}
int hours = Integer.parseInt(args[0]);
int minutes = Integer.parseInt(args[1]);
Thread.sleep(hours * 60 * 60 * 1000 + minutes * 60 * 1000);
Runtime.getRuntime().exec("shutdown -s -t 30");
}
But then, I'd return to my earlier statement: Java is overkill for this, use the shell and native scheduling features instead (for example at
in UNIX).
-
\$\begingroup\$ Honestly, I was thinking about Thread.sleep since that's what I used earlier, but I learned of timer class today, so I thought it'd be more appropriate. I will learn how to do this via powershell instead. But let's say if I want to make it for a friend who isn't comfortable with command line; will my current idea be appropriate for them? \$\endgroup\$Honinbo Shusaku– Honinbo Shusaku2014年05月10日 22:00:10 +00:00Commented May 10, 2014 at 22:00
Explore related questions
See similar questions with these tags.
at /?
. Andshutdown -f -s
will force running programs to quit before shutting down, but-t 30
already implies-f
. \$\endgroup\$at
command the same way you're runningshutdown
, and you wouldn't even need the program to keep running. For something Windows-specific, though, a PowerShell or batch script would still be far simpler and more straightforward. Your friend should be able to run either type of script by double-clicking it. \$\endgroup\$