Ecco le parti più importanti..
codice:
public class ImagesExtractorTask implements Runnable{
public synchronized void runTask() throws Throwable {
//operazioni varie
}
public static void main(final String[] args) throws InterruptedException {
long patience = 1000 * 60 * 60;
threadMessage("Starting Extractor thread...");
long startTime = System.currentTimeMillis();
Thread t = new Thread(new ImagesExtractorTask());
t.start();
threadMessage("Waiting for Extractor thread to finish");
while (t.isAlive()) {
threadMessage("Still waiting...");
//Wait maximum of 1 second for Extractor thread to
//finish.
t.join(1000);
if (((System.currentTimeMillis() - startTime) > patience) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
t.join();
}
}
}
public void run(){
try {
runTask();
} catch (Throwable ex) {
Logger.getLogger(ImagesExtractorTask.class.getName()).log(Level.SEVERE, null, ex);
}
}
static void threadMessage(String message) {
String threadName = Thread.currentThread().getName();
System.out.format("%s: %s%n", threadName, message);
}
}