home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
Analyzer.java
< prev
next >
Wrap
Text File
|
1998-11-05
|
3KB
|
104 lines
// Copyright (c) 1997, 1998 Symantec, Inc. All Rights Reserved.
/*
This is thread analyzes the log file while the AnalyzeDialog is up.
It also provides the mechanism for safe thread cancelling.
Note: It is usually not safe to call the Thread.stop() method, as
randomly stopping execution in a thread may leave "damaged" objects
(objects in an inconsistent state).
*/
class Analyzer extends Thread {
TrackProgress trackProgress;
boolean bCancelled = false;
// Constructs an Analyzer.
public Analyzer(TrackProgress trackProgress) {
this.trackProgress = trackProgress;
//{{INIT_CONTROLS
//}}
}
// This is Thread's standard run method.
public void run() {
Report report = Data.getDataInstance().getCurrentReport();
report.run(trackProgress);
}
/* Use this method to cancel an Analyzer thread.
It flags the thread as cancelled, then waits
awhile for it to die.
Returns true if the thread was/is stopped.
*/
public static boolean cancel(Analyzer analyzer) {
// return if nothing to do
if(analyzer == null || !analyzer.isAlive()) {
return true;
}
// cancel the thread
analyzer.setCancelled();
// put out message
analyzer.trackProgress.step("CANCELLING", 0);
// wait up to 10 seconds for cancel success
int count = 100;
while(analyzer.isAlive() && --count >= 0) {
try {
analyzer.join(100);
} catch(InterruptedException x) {
}
}
return !analyzer.isAlive();
}
/* This method is used in the run() code to see whether the
thread should stop running. If so, an exception is thrown.
*/
public static void throwExceptionIfCurrentThreadCancelled() throws ParseLogException {
boolean threadIsCancelled;
Thread t = currentThread();
if(t instanceof Analyzer) {
threadIsCancelled = ((Analyzer)t).isCancelled();
} else {
// fake it. this bit of code not actually used in this app
threadIsCancelled = t.isInterrupted();
}
// throw exception if cancelled
if(threadIsCancelled) {
throw new ParseLogException("CANCELLED", null);
}
}
/* Flag this thread as cancelled and interrupt it in case it's blocked
(waiting on IO, etc).
*/
private void setCancelled() {
// if already cancelled, do nothing
if(bCancelled) {
return;
}
// set cancelled flag
synchronized(this) {
bCancelled = true;
}
// interrupt this thread
interrupt();
}
/* Determine if "setCancel" has been called for this thread.
*/
private boolean isCancelled() {
// once set never cleared, so no need to sync here
if(bCancelled) {
return true;
}
// sync here, though
synchronized(this) {
return bCancelled;
}
}
//{{DECLARE_CONTROLS
//}}
}