Java Reference
In-Depth Information
/** How long to sleep between tries */
public
public static
static final
final
int
int
MINUTES
=
5
;
private
private static
static final
final
int
int
SECONDS
=
MINUTES
*
60
;
public
public
AutoSave
(
FileSaver m
) {
super
super
(
"AutoSave Thread"
);
setDaemon
(
true
true
);
// so we don't keep the main app alive
model
=
m
;
}
public
public
void
void
run
() {
while
while
(
true
true
) {
// entire run method runs forever.
try
try
{
sleep
(
SECONDS
*
1000
);
}
catch
catch
(
InterruptedException e
) {
// do nothing with it
}
iif
(
model
.
wantAutoSave
() &&
model
.
hasUnsavedChanges
())
model
.
saveFile
(
null
null
);
}
}
// Not shown:
// 1) saveFile() must now be synchronized.
// 2) method that shuts down main program be synchronized on *SAME* object
}
/** Local copy of FileSaver interface, for compiling AutoSave demo. */
interface
interface
FileSaver
FileSaver
{
/** Load new model from fn; if null, prompt for new fname */
public
public
void
void
loadFile
(
String fn
);
/** Ask the model if it wants AutoSave done for it */
public
public
boolean
boolean
wantAutoSave
();
/** Ask the model if it has any unsaved changes, don't save otherwise */
public
public
boolean
boolean
hasUnsavedChanges
();
/** Save the current model's data in fn.
* If fn == null, use current fname or prompt for a filename if null.
*/
public
public
void
void
saveFile
(
String fn
);
}