X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=inline;f=bundles%2Forg.simantics%2Fsrc%2Forg%2Fsimantics%2FSleepingDatabaseJob.java;h=358947e8e62714f3f788b6f22a589427d4635a9d;hb=refs%2Fchanges%2F73%2F4673%2F1;hp=879183ddd0fae1ac9e0cc35bbb45f75c77d0a49d;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java b/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java index 879183ddd..358947e8e 100644 --- a/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java +++ b/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java @@ -1,111 +1,111 @@ -/******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Semantum Oy - initial API and implementation - *******************************************************************************/ -package org.simantics; - -import java.util.concurrent.Semaphore; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.jobs.Job; - -/** - * A dummy database-family job that can be used for signaling that a large - * database job is in progress although it is technically not running in a - * {@link Job}. - * - *
- * To start the job and wait for it to start, use - * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die, - * use {@link #disposeAndJoin()}. - * - * @author Tuukka Lehtonen - */ -public class SleepingDatabaseJob extends DatabaseJob { - - private final Semaphore start = new Semaphore(0); - private final Semaphore end = new Semaphore(0); - - public SleepingDatabaseJob(String name) { - super(name); - } - - @Override - protected final IStatus run(IProgressMonitor monitor) { - start.release(); - try { - return work(monitor); - } finally { - try { - end.acquire(); - } catch (InterruptedException e) { - // Some other party wanted to kill the job. So be it. - } - } - } - - protected IStatus work(IProgressMonitor monitor) { - return Status.OK_STATUS; - } - - public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException { - schedule(); - start.acquire(); - start.release(); - return this; - } - - public void scheduleAndWaitForRunningUninterruptibly() { - schedule(); - start.acquireUninterruptibly(); - start.release(); - } - - public void disposeAndJoin() throws InterruptedException { - end.release(); - join(); - } - - /** - * @param name - * @param runnable - * @throws InterruptedException - */ - public static void sleepWhile(String name, Runnable runnable) throws InterruptedException { - SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name); - try { - dbjob.scheduleAndWaitForRunning(); - runnable.run(); - } finally { - dbjob.disposeAndJoin(); - } - } - - /** - * @param name - * @param runnable - * @throws InterruptedException - */ - public static void sleepUninterruptiblyWhile(String name, Runnable runnable) { - SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name); - try { - dbjob.scheduleAndWaitForRunningUninterruptibly(); - runnable.run(); - } finally { - try { - dbjob.disposeAndJoin(); - } catch (InterruptedException e) { - } - } - } - -} +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics; + +import java.util.concurrent.Semaphore; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; + +/** + * A dummy database-family job that can be used for signaling that a large + * database job is in progress although it is technically not running in a + * {@link Job}. + * + *
+ * To start the job and wait for it to start, use + * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die, + * use {@link #disposeAndJoin()}. + * + * @author Tuukka Lehtonen + */ +public class SleepingDatabaseJob extends DatabaseJob { + + private final Semaphore start = new Semaphore(0); + private final Semaphore end = new Semaphore(0); + + public SleepingDatabaseJob(String name) { + super(name); + } + + @Override + protected final IStatus run(IProgressMonitor monitor) { + start.release(); + try { + return work(monitor); + } finally { + try { + end.acquire(); + } catch (InterruptedException e) { + // Some other party wanted to kill the job. So be it. + } + } + } + + protected IStatus work(IProgressMonitor monitor) { + return Status.OK_STATUS; + } + + public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException { + schedule(); + start.acquire(); + start.release(); + return this; + } + + public void scheduleAndWaitForRunningUninterruptibly() { + schedule(); + start.acquireUninterruptibly(); + start.release(); + } + + public void disposeAndJoin() throws InterruptedException { + end.release(); + join(); + } + + /** + * @param name + * @param runnable + * @throws InterruptedException + */ + public static void sleepWhile(String name, Runnable runnable) throws InterruptedException { + SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name); + try { + dbjob.scheduleAndWaitForRunning(); + runnable.run(); + } finally { + dbjob.disposeAndJoin(); + } + } + + /** + * @param name + * @param runnable + * @throws InterruptedException + */ + public static void sleepUninterruptiblyWhile(String name, Runnable runnable) { + SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name); + try { + dbjob.scheduleAndWaitForRunningUninterruptibly(); + runnable.run(); + } finally { + try { + dbjob.disposeAndJoin(); + } catch (InterruptedException e) { + } + } + } + +}