Konsole beendet - Programm kontrolliert runter fahren

bliet

Mitglied
Hallo alle zusammen,

ich habe ein Konsolenprogramm geschrieben.
Das Programm läuft unter Windows. Nun wollte ich es verhindern, dass das Programm mitten drin abgebrochen wird, wenn die Konsole mittels Mausklick auf X geschlossen wird.
Per Google kam ich auf ShutdownHooks.

Grobe Skizze:
Java:
public class MainProg {
   static long run = true;

   public static void main(String[] args) {
      addDaemonShutdownHook();
      while(run){
         doSomething();
      }
   }

   static protected void addDaemonShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {            
            @Override
            public void run() {
            	
                try {
                	run = false;
            }
        });
    }
}

Nun müsste ich ja noch irgendwie sagen, dass gewartet werden soll, bis die Methoe doSomethind() fertig ist.
Wie bekomme ich das hin?
Bin ich überhaupt auf den richtigen weg?

Danke für eure Hilfe.
MfG
bliet
 
Du bist auf jeden Fall auf dem richigen Weg, wenn es geht, dann mit einem ShutdownHook. Wie immer hilft bei Unklarheiten ein Blick in die API:
Runtime#addShutdownHook(java.lang.Thread) hat gesagt.:
A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.
Soll heisen, dass dein main-Thread schon beendet wurde, wenn die VM aus einem anderen Grund als System.exit beendet wurde. Wenn das ganze aber in einem Deamon-Thread starten würde, würde der Deamon-Thread auf jeden Fall weiterlaufen, ich denke also das folgendes funktionieren sollte:

Java:
public class MainProg {
   static long run = true;
   static Thread t;
   
   public static void main(String[] args) {
      addDaemonShutdownHook();
      t=new Thread(){
      while(run){
         doSomething();
      }};
      t.setDeamon(true);
      t.start();
      t.join();
   }
 
   static protected void addDaemonShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {            
            @Override
            public void run() {
                   run = false;
                   t.join;
        });
    }
}
 
Hier jetzt mal ein KSKB

Java:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class main {	
	static boolean run = true;
	static Thread t;
	
	public static void main(String[] args) {
		addDaemonShutdownHook();
		t = new Thread(){
			@Override
			public void run() {
				File file = new File("test.txt");
				try {
					FileWriter writer = new FileWriter(file, false);
					
					while(run){
						for(int i = 0; i < 10000; i++){
							System.out.println(i);
							writer.write(""+i);
							writer.write(System.getProperty("line.separator"));
						}
					}
					
					writer.flush();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
				
				
			}
		};
		
		t.setDaemon(true);
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static protected void addDaemonShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {            
            @Override
            public void run() {
                   run = false;
                   try {
					t.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
	}
}

Scheint zu funktionieren. Danke.
Werde es dann mal in mein richtiges Programm einbauen, und schauen, ob es dort auch läuft.

MfG
bliet
 

Neue Themen


Zurück
Oben