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){}
}
}
* @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){}
}
}
把Thread.sleep(10000000L);改成
new Thread(){public void run(){try{Thread.sleep(100000L);}catch(Exception e){}}}.start();
就可以了!
* @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);//手动关不可以,只有这样才有效.
}
}