|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
ScheduledFuture and Binary latchHi all, allow this code to explain:
public class ParseTask implements Runnable { private static final Logger _log = Logger.getLogger(); private static final ThreadFactory factory = new ONExceptionThreadFactory(new ONExceptionHandler()); private static final ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(factory); private static final BinaryLatch binaryLatch = new BinaryLatch(); public void run() { if (binaryLatch.isRunning()) { // skip task if its already running return; } Future future = executorService.submit(new ParseJob()); try { // wait and timeout or return result future.get(300, TimeUnit.SECONDS); } catch (InterruptedException ex) { _log.error(ex.getMessage(), ex); // Re-assert the thread's interrupted status Thread.currentThread().interrupt(); // We don't need the result, so cancel the task too future.cancel(true); } catch (Exception ex) { _log.error(ex.getMessage(), ex); throw ThreadUtils.launderThrowable(ex.getCause()); } } } This ParseTask is started via a Servlet init() as: ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture <?> scheduledParseJob = ses.scheduleWithFixedDelay(new ParseTask(), 0L, 1, TimeUnit.MINUTES); Problem: I want to write a latch, perhaps a semaphore, that skips this task if its already running. So I want to acquire some type of lock when I start this task, skip the task if its already been started, and release the lock when this single task is completed. CountDownLatch doesn't seem right for a repeatable task. I've written a few versions of BinaryLatch but nothing seems right to me. Any ideas? Robert _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
Re: ScheduledFuture and Binary latchOn Thu, Jun 5, 2008 at 3:02 PM, robert lazarski wrote:
> > Problem: I want to write a latch, perhaps a semaphore, that skips this > task if its already running. So I want to acquire some type of lock > when I start this task, skip the task if its already been started, and > release the lock when this single task is completed. CountDownLatch > doesn't seem right for a repeatable task. I've written a few versions > of BinaryLatch but nothing seems right to me. Any ideas? > final AtomicBoolean isRunning = new AtomicBoolean() if (isRunning.compareAndSet(false, true)) try { // run task } finally { isRunning.set(false); } _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
Re: ScheduledFuture and Binary latchI usually use an AtomicBoolean for this type of thing. private AtomicBoolean running = new AtomicBoolean(); ... public void run() { if (running.compareAndSet(false, true)) { try { // ...do work... } finally { running.set(false); } } } > -----Original Message----- > From: concurrency-interest-bounces@... [mailto:concurrency- > interest-bounces@...] On Behalf Of robert lazarski > Sent: Thursday, June 05, 2008 12:03 PM > To: concurrency-interest@... > Subject: [concurrency-interest] ScheduledFuture and Binary latch > > Hi all, allow this code to explain: > > public class ParseTask implements Runnable { > > private static final Logger _log = Logger.getLogger(); > private static final ThreadFactory factory = new > ONExceptionThreadFactory(new ONExceptionHandler()); > private static final ExecutorService executorService = > Executors.newSingleThreadScheduledExecutor(factory); > private static final BinaryLatch binaryLatch = new BinaryLatch(); > > public void run() { > if (binaryLatch.isRunning()) { > // skip task if its already running > return; > } > Future future = executorService.submit(new ParseJob()); > try { > // wait and timeout or return result > future.get(300, TimeUnit.SECONDS); > } catch (InterruptedException ex) { > _log.error(ex.getMessage(), ex); > // Re-assert the thread's interrupted status > Thread.currentThread().interrupt(); > // We don't need the result, so cancel the task too > future.cancel(true); > } catch (Exception ex) { > _log.error(ex.getMessage(), ex); > throw ThreadUtils.launderThrowable(ex.getCause()); > } > } > } > > This ParseTask is started via a Servlet init() as: > > ScheduledExecutorService ses = > Executors.newSingleThreadScheduledExecutor(); > ScheduledFuture <?> scheduledParseJob = ses.scheduleWithFixedDelay(new > ParseTask(), > 0L, 1, TimeUnit.MINUTES); > > Problem: I want to write a latch, perhaps a semaphore, that skips this > task if its already running. So I want to acquire some type of lock > when I start this task, skip the task if its already been started, and > release the lock when this single task is completed. CountDownLatch > doesn't seem right for a repeatable task. I've written a few versions > of BinaryLatch but nothing seems right to me. Any ideas? > > Robert > _______________________________________________ > Concurrency-interest mailing list > Concurrency-interest@... > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
|
|
|
|
|
|
|
| Free Forum Powered by Nabble | Forum Help |