public class SingleInstance { /**
 * @param args
 */
public static void main(String[] args) {
try{
java.net.ServerSocket server=new java.net.ServerSocket(12345);
}catch(Exception e){
System.out.println("single");
System.exit(0);
}
try{
Thread.sleep(10000000L);
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run(){
System.out.println("bye!");//手动关闭程序,这个bye!总是打不出来,那里有错?
}
});
}catch(Exception e){}
}
}

解决方案 »

  1.   

    手机关闭!!出现不了,那是因为你在睡觉,没有执行下面的,就是还没有挂监听处理.
    把Thread.sleep(10000000L);改成
    new Thread(){public void run(){try{Thread.sleep(100000L);}catch(Exception e){}}}.start();
    就可以了!
      

  2.   

    public class SingleInstance { /**
     * @param args
     */
    public static void main(String[] args) {
    try{
    java.net.ServerSocket server=new java.net.ServerSocket(12345);
    }catch(Exception e){
    System.out.println("single");
    System.exit(0);
    }
    try{
    //Thread.sleep(10000000L);

    Runtime.getRuntime().addShutdownHook(new Thread(){
    public void run(){
    while(true){
    System.out.println("bye!");
    }

    }
    });
    }catch(Exception e){
    e.printStackTrace();
    }
    System.exit(0);//手动关不可以,只有这样才有效.
    }
    }