java认证原版英文辅导
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Previous Article | Absolute Java Home | Next Article
There is no Java-provided mechanism to ensure only one instance of a Java application is running at any one time. From the operating system's perspective, a Java application is simply another process -the fact that this process happens to be a Java VM which in turn runs your Java application is entirely irrelevant to the operating system. If you're running two instances of the same Java application, you're simply running two instances of the Java VM. To ensure only one instance of your application is running at a time, you must establish some communication mechanism whereby a running instance of the application can inform other instances that they should not run. When an instance of the application starts, it first checks some condition that indicates whether or not another instance is already running. A seemingly simple way to do this is via the existence of a common file. The existence of the file serves as a "flag" to indicate an instance of the application is already running. When the file exists it is a signal to other instances of the application that they should not run; if the file does not exist, another instance may run. The code here is pretty simple:
1: import java.io.*; 2: 3: public class OneInstance1 { 4: public static void main(String[] args) { 5: // Set up a "flag file" whose existence indicates the applicat 6: // is already running. 7: File flagFile = new File("c:\\temp\\FlagFile"); 8: 9: try { 10: if(false == flagFile.createNewFile()) { 11: System.out.println("This program is already running..." 12: System.exit(1); 13: } 14: } catch(final IOException e) { 15: System.err.println("IOException: " + e); 16: System.exit(1); 17: } 18: 19: System.out.println("This is the first instance of this program 20: 21: // Mark the "flag file" for automatic deletion when the VM end
Unfortunately, even though this program looks like it should work (and it usually will), there are a couple problems with it: 1. If the VM terminates abnormally after the "flag file" has been created, the file will not be deleted. When the application is restarted it will find the "flag file" already exists and will shut itself down. 2. If the "flag file" is deleted (either accidentally or intentionally) while the first instance of the application is executing, a second instance of the application will begin executing. Because the second instance of the application will successfully create its own "flag file", it will assume it is the only instance in existence. If at First You Don't Succeed... We can modify the above program to bring us closer to our goal. Unlike the OneInstance1 program above, the version below (OneInstance2) actually opens the file for output. While the file is open, it cannot be deleted. Since the flag file cannot be deleted while it is open there is no possibility of a subsequent instance of the application running while the first instance is running. Essentially, OneInstance2 eliminates the problem of the "flag file" being accidentally or intentially deleted while the first instance of the application is executing. We do, however, still have the problem that if the first instance ends abnormally, the "flag file" will not be automatically deleted. We'll take caree of that problem in a moment.
1: import java.io.*; 2: 3: public class OneInstance2 { 4: public static void main(String[] args) { 5: // Set up a "flag file" whose existence indicates the applicat 6: // is already running. 7: File flagFile = new File("c:\\temp\\FlagFile"); 8: FileOutputStream fos = null; 8: 9: try { 10: if(false == flagFile.createNewFile()) {
As an aside, you should note that due to a JDK bug (JDK v1.3.0) the above code had to explicitly close the file in order for it to be automatically deleted. Yes, good programmers should always explicitly close files anyway, but failure to do so should not prevent File.deleteOnExit() from working properly. Anyway, if you are using deleteOnExit(), make sure you always close the file first or the deleteOnExit() method will have no effect. Third Time's the Charm Now for a version that does work. The program below (OneInstance3) uses a .ServerSocket rather than files. When the program starts, it tries to create a .ServerSocket on a predefined port (port 1942, in this example). Because only one ServerSocket can listen on a given port at any one time, any attempt by another application to listen on the same port will generate a .BindException.
Preventing Multiple Instances of a Java Program from Running Simultaneously
1/4
Preventing Multiple Instances of a Java Program from Running Simultaneously
Preventing Multiple Instances of a Java Prபைடு நூலகம்gram from Running Simultaneously
2/4
22: // but this only applies if the VM terminates normally. 23: flagFile.deleteOnExit(); 24: 25: // Do some useful work here... 26: } // main() 27: } // OneInstance1
Preventing Multiple Instances of a Java Program from Running Simultaneously
3/4
11: System.out.println("This program is already running..." 12: System.exit(1); 13: } 14: 15: // Open the file to prevent its deletion. 16: fos = new FileOutputStream(flagFile); 17: } catch(final IOException e) { 18: System.err.println("IOException: " + e); 19: System.exit(1); 20: } 21: 22: System.out.println("This is the first instance of this program 23: 24: // Mark the "flag file" for automatic deletion when the VM end 25: // but this only applies if the VM terminates normally. 26: flagFile.deleteOnExit(); 27: 28: // Do some useful work here... 29: 30: // Due to a JDK bug, we *must* close() the file in order for 31: // it to be automatically deleted. 32: try { 33: fos.close(); 34 } catch(final IOException e) { 35: System.err.println("IOException: " + e); 36: System.exit(1); 37: } 38: } // main() 39: } // OneInstance2
There is no Java-provided mechanism to ensure only one instance of a Java application is running at any one time. From the operating system's perspective, a Java application is simply another process -the fact that this process happens to be a Java VM which in turn runs your Java application is entirely irrelevant to the operating system. If you're running two instances of the same Java application, you're simply running two instances of the Java VM. To ensure only one instance of your application is running at a time, you must establish some communication mechanism whereby a running instance of the application can inform other instances that they should not run. When an instance of the application starts, it first checks some condition that indicates whether or not another instance is already running. A seemingly simple way to do this is via the existence of a common file. The existence of the file serves as a "flag" to indicate an instance of the application is already running. When the file exists it is a signal to other instances of the application that they should not run; if the file does not exist, another instance may run. The code here is pretty simple:
1: import java.io.*; 2: 3: public class OneInstance1 { 4: public static void main(String[] args) { 5: // Set up a "flag file" whose existence indicates the applicat 6: // is already running. 7: File flagFile = new File("c:\\temp\\FlagFile"); 8: 9: try { 10: if(false == flagFile.createNewFile()) { 11: System.out.println("This program is already running..." 12: System.exit(1); 13: } 14: } catch(final IOException e) { 15: System.err.println("IOException: " + e); 16: System.exit(1); 17: } 18: 19: System.out.println("This is the first instance of this program 20: 21: // Mark the "flag file" for automatic deletion when the VM end
Unfortunately, even though this program looks like it should work (and it usually will), there are a couple problems with it: 1. If the VM terminates abnormally after the "flag file" has been created, the file will not be deleted. When the application is restarted it will find the "flag file" already exists and will shut itself down. 2. If the "flag file" is deleted (either accidentally or intentionally) while the first instance of the application is executing, a second instance of the application will begin executing. Because the second instance of the application will successfully create its own "flag file", it will assume it is the only instance in existence. If at First You Don't Succeed... We can modify the above program to bring us closer to our goal. Unlike the OneInstance1 program above, the version below (OneInstance2) actually opens the file for output. While the file is open, it cannot be deleted. Since the flag file cannot be deleted while it is open there is no possibility of a subsequent instance of the application running while the first instance is running. Essentially, OneInstance2 eliminates the problem of the "flag file" being accidentally or intentially deleted while the first instance of the application is executing. We do, however, still have the problem that if the first instance ends abnormally, the "flag file" will not be automatically deleted. We'll take caree of that problem in a moment.
1: import java.io.*; 2: 3: public class OneInstance2 { 4: public static void main(String[] args) { 5: // Set up a "flag file" whose existence indicates the applicat 6: // is already running. 7: File flagFile = new File("c:\\temp\\FlagFile"); 8: FileOutputStream fos = null; 8: 9: try { 10: if(false == flagFile.createNewFile()) {
As an aside, you should note that due to a JDK bug (JDK v1.3.0) the above code had to explicitly close the file in order for it to be automatically deleted. Yes, good programmers should always explicitly close files anyway, but failure to do so should not prevent File.deleteOnExit() from working properly. Anyway, if you are using deleteOnExit(), make sure you always close the file first or the deleteOnExit() method will have no effect. Third Time's the Charm Now for a version that does work. The program below (OneInstance3) uses a .ServerSocket rather than files. When the program starts, it tries to create a .ServerSocket on a predefined port (port 1942, in this example). Because only one ServerSocket can listen on a given port at any one time, any attempt by another application to listen on the same port will generate a .BindException.
Preventing Multiple Instances of a Java Program from Running Simultaneously
1/4
Preventing Multiple Instances of a Java Program from Running Simultaneously
Preventing Multiple Instances of a Java Prபைடு நூலகம்gram from Running Simultaneously
2/4
22: // but this only applies if the VM terminates normally. 23: flagFile.deleteOnExit(); 24: 25: // Do some useful work here... 26: } // main() 27: } // OneInstance1
Preventing Multiple Instances of a Java Program from Running Simultaneously
3/4
11: System.out.println("This program is already running..." 12: System.exit(1); 13: } 14: 15: // Open the file to prevent its deletion. 16: fos = new FileOutputStream(flagFile); 17: } catch(final IOException e) { 18: System.err.println("IOException: " + e); 19: System.exit(1); 20: } 21: 22: System.out.println("This is the first instance of this program 23: 24: // Mark the "flag file" for automatic deletion when the VM end 25: // but this only applies if the VM terminates normally. 26: flagFile.deleteOnExit(); 27: 28: // Do some useful work here... 29: 30: // Due to a JDK bug, we *must* close() the file in order for 31: // it to be automatically deleted. 32: try { 33: fos.close(); 34 } catch(final IOException e) { 35: System.err.println("IOException: " + e); 36: System.exit(1); 37: } 38: } // main() 39: } // OneInstance2