Java没有提供这样的机制。从操作系统的观点来看,一个启动的JavaApplication仅仅是一
个JVM的运行实例。运行相同Application的两个实例,仅仅是运行两个无关的JVM。
只有让多个运行实例之间有一个既定的通讯机制就可以保证只有一个实例运行。
方案1:使用Java的加锁文件机制,idea相当简单,让运行实例通过java.nio.channels.FileLock获得一个"well-known"文件的互斥锁。
importjava.io.*;
importjava.nio.channels.*;
publicclassOneInstance_1{
publicstaticvoidmain(String[]args)throwsException{
FileLocklck=newFileOutputStream("C:\\flagFile").getChannel().tryLock();
if(lck==null){
System.out.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}
System.out.println("Thisisthefirstinstanceofthisprogram...");
//Dosomeworkhere.....
}
}
问题:平台相关
方案2:使用java.net.ServerSocket
importjava.io.*;
importjava.net.*;
publicclassOneInstance_2{
privatestaticServerSocketlistenerSocket;
publicstaticvoidmain(String[]args){
try{
listenerSocket=newServerSocket(2004);
//Atthispoint,noothersocketmaylistenonport2004.
}catch(java.net.BindExceptione){
System.err.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}catch(finalIOExceptione){//anunexpectedexceptionoccurred
System.exit(1);
}
//Dosomeworkhere.....
}
}
问题:打开服务端口可能会受到防火墙的影响;可能和别的端口冲突。
方案3:使用File.createNewFile()andFile.deleteOnExit()
importjava.io.*;
publicclassOneInstance_3{
publicstaticvoidmain(String[]args)throwsException{
FileflagFile=newFile("C:\\flagFile");
if(false==flagFile.createNewFile()){
System.out.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}
flagFile.deleteOnExit();
System.out.println("Thisisthefirstinstanceofthisprogram...");
//Dosomeworkhere.....
}
个JVM的运行实例。运行相同Application的两个实例,仅仅是运行两个无关的JVM。
只有让多个运行实例之间有一个既定的通讯机制就可以保证只有一个实例运行。
方案1:使用Java的加锁文件机制,idea相当简单,让运行实例通过java.nio.channels.FileLock获得一个"well-known"文件的互斥锁。
importjava.io.*;
importjava.nio.channels.*;
publicclassOneInstance_1{
publicstaticvoidmain(String[]args)throwsException{
FileLocklck=newFileOutputStream("C:\\flagFile").getChannel().tryLock();
if(lck==null){
System.out.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}
System.out.println("Thisisthefirstinstanceofthisprogram...");
//Dosomeworkhere.....
}
}
问题:平台相关
方案2:使用java.net.ServerSocket
importjava.io.*;
importjava.net.*;
publicclassOneInstance_2{
privatestaticServerSocketlistenerSocket;
publicstaticvoidmain(String[]args){
try{
listenerSocket=newServerSocket(2004);
//Atthispoint,noothersocketmaylistenonport2004.
}catch(java.net.BindExceptione){
System.err.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}catch(finalIOExceptione){//anunexpectedexceptionoccurred
System.exit(1);
}
//Dosomeworkhere.....
}
}
问题:打开服务端口可能会受到防火墙的影响;可能和别的端口冲突。
方案3:使用File.createNewFile()andFile.deleteOnExit()
importjava.io.*;
publicclassOneInstance_3{
publicstaticvoidmain(String[]args)throwsException{
FileflagFile=newFile("C:\\flagFile");
if(false==flagFile.createNewFile()){
System.out.println("Apreviousinstanceisalreadyrunning....");
System.exit(1);
}
flagFile.deleteOnExit();
System.out.println("Thisisthefirstinstanceofthisprogram...");
//Dosomeworkhere.....
}
}
来源:·BBS水木清华站smth.org·[FROM:202.108.130.*]