Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
2  
Frankly, you're using the wrong language for this. It's trivial to do from a shell script, or even from the command line, neither of which requires wasting a thread. Hint: at /?. And shutdown -f -s will force running programs to quit before shutting down, but -t 30 already implies -f. –  cHao May 10 at 20:06
    
@cHao ok thanks, I will learn how to make shell scripts. 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? –  Abdul May 10 at 22:01
1  
It should work, but it's more complicated than it needs to be. You could run an at command the same way you're running shutdown, 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. –  cHao May 10 at 22:17
    
You are making reference to powershell. Do you want this to run only on WIndows machines? –  Alexandre Santos May 11 at 8:45
1  
@h.j.k. I didn't need it to be platform independent. I initially wrote it in Java because that's the only language that I'm relatively proficient with. I asked about the platform independence because Alexandre made a reference to the platform dependence, so I was curious how I would make it independent. I basically just needed a personal timed shutdown for my PC. –  Abdul May 12 at 14:27

1 Answer 1

up vote 4 down vote accepted

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).

share|improve this answer
    
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? –  Abdul May 10 at 22:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.