Java applet: how to make sure that destruction is completed before page refresh

In Java utilizing logging, I run the handler in init () and close the handler in destroy (), and it works fine: the log file was created, etc. If the user refreshes the page as usual, it still just creates a single log file.

However, if the user refreshes the page several times using the applet, it seems that destroy () has not been called or may not have completed the task, and since init () is called again, it assumes the previous file is still locked and creates a new log file.

I tried using both destroy () and finalize () to close the handler, but it does not work. Does anyone know how to solve this problem?

Another minor question: what actually happened if init () did not finish and the page was refreshed. Will it continue the process and, in the end, will not be able to call destroy (), or will it just stop right there?

+5
source share
3 answers

Quote from Java Tutorials :

The Java Plug-in software creates a workflow for each Java applet.

In a multi-threaded environment, you must be very careful with shared resources. The best and easiest approach is to not share anything (it scales best without deadlocks).

, "init" -. , ( ). , - . Oracle .


- " ". :

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

private String generateFileName() {
    return String.format("applets-log/%s-%s.log", dateFormat.format(new Date()), UUID.randomUUID());
}

, .


():

java- . "" "", , .

, , - .

6u10, , (1000 200 ) .

+4

.

, destroy() finalize() . , . , 2 , init() ( ) init() , , destroy() . finalize() , , , . , - .

Javadoc:

- , , .

/ , . -, , . , -, , .

If you absolutely need to have log files for applets inside your web browser, the easiest solution for each call init()is to create a new file specific to that call to the applet. If you want to ambitiously, you can use lock files to indicate which files were used, and in destroy()time combines the unlocked log files into one larger one, but again there is the problem of coordinating stream concatenation processes.

0
source

All Articles