java_定时关机

java定时关机(Copy Code)

import java.util.Scanner;

class Timeing implements Runnable
{
    int i = 0;

    public Timeing (int i)
    {
        this.i = i;
    }

    @Override
    public void run ()
    {
        try
        {
            Thread.sleep(i * 60 * 1000);
            System.out.print("将要关机!");
            Program.exec("S");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

public class Program
{
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main (String [] args) throws InterruptedException
    {
        int i = 0;
        while (true)
        {
            System.out.println("1.定时关机 2.立即关机 3.重启 4.退出");
            Scanner scanner = new Scanner(System.in);
            try
            {
                i = scanner.nextInt();
                switch (i)
                {
                    case 1:
                        timing();
                        break;
                    case 2:
                        close();
                        break;
                    case 3:
                        restart();
                        break;
                    case 4:
                        System.exit(0);
                        break;
                    default:
                        System.out.println("输入有误,有效数字:1-2-3-4");
                        continue;
                }
                break;
            }
            catch (Exception e)
            {
                System.out.println("输入有误!请重新输入!");
                continue;
            }
        }


    }

    private static void timing () throws InterruptedException
    {
        System.out.println("倒计时   几分钟?");
        Scanner scanner= new Scanner(System.in);
        int i;
        while(true)
        {
            try
            {
                i = scanner.nextInt();
                break;
            }
            catch (Exception e)
            {
                System.out.println("输入合法数字");
                continue;
            }
        }
        
        Thread thread = new Thread(new Timeing(i));
        thread.start();
        time(i);
    }

    private static void time (int i) throws InterruptedException
    {

        System.out.println("开始计时:");
        i = i * 60;
        while (true)
        {
            Thread.sleep(1000);
            i -= 1;
            if (i <= 0)
                break;
            System.out.println(i + ",");
        }

    }

    private static void close ()
    {
        exec("S");
    }

    private static void restart ()
    {
        exec("R");
    }

    public static void exec (String kind)
    {
        try
        {
            Runtime.getRuntime().exec("cmd /c start call shutdown -" + kind + " -f -t 0");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

转载于:https://www.cnblogs.com/lijialong/archive/2010/10/18/javasort.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值