代码之家  ›  专栏  ›  技术社区  ›  Ky -

我如何制作一个按钮,当按下时,停止运行一个内部程序?

  •  1
  • Ky -  · 技术社区  · 14 年前

    (个人问题来自 IDE-Style program running )

    2 回复  |  直到 14 年前
        1
  •  3
  •   aioobe    14 年前

    ProcessBuilder

    stop

    public class Main {
    
        public static void main(String args[]) throws InterruptedException {
    
            ThreadGroup internalTG = new ThreadGroup("internal");
            Thread otherProcess = new Thread(internalTG, "Internal Program") {
                public void run() {
                    OtherProgram.main(new String[0]);
                }
            };
    
            System.out.println("Starting internal program...");
            otherProcess.start();
    
            Thread.sleep(1000);
            System.out.println("Killing internal program...");
    
            internalTG.stop();
        }
    }
    

    class OtherProgram {
    
        public static void main(String[] arg) {
            for (int i = 0; i < 5; i++)
                new Thread() {
                    public void run() {
                        System.out.println("Starting...");
                        try {
                            sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Stopping...");
                    }
                }.start();
        }
    }
    

    Starting internal program...
    Starting...
    Starting...
    Starting...
    Starting...
    Starting...
    Killing internal program...
    
        2
  •  0
  •   Darron    14 年前