View Javadoc

1   package cz.cuni.amis.tests;
2   
3   import java.util.concurrent.CountDownLatch;
4   import java.util.logging.Logger;
5   
6   public abstract class ConcurrentTask implements Runnable {
7   
8   	private Exception e;
9   	private CountDownLatch latch;
10  	private Logger log;
11  	
12  	public void setLatch(CountDownLatch latch) {
13  		this.latch = latch;
14  	}
15  	
16  	public void setLogger(Logger log) {
17  		this.log = log;
18  	}
19  	
20  	public final void run() {
21  		try {
22  			runImpl();
23  		} catch (Exception e) {
24  			this.e = e;
25  		} finally {
26  			if (log != null) {
27  				synchronized(latch) {
28  					latch.countDown();
29  					log.info("Jobs remaining: " + latch.getCount());
30  				}
31  			} else {
32  				latch.countDown();
33  			}
34  		}
35  	}
36  	
37  	public boolean isException() {
38  		return e != null;
39  	}
40  	
41  	public Exception getException() {
42  		return e;
43  	}
44  
45  	protected abstract void runImpl();
46  	
47  }