-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * Semantum Oy - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics;\r
-\r
-import java.util.concurrent.Semaphore;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-\r
-/**\r
- * A dummy database-family job that can be used for signaling that a large\r
- * database job is in progress although it is technically not running in a\r
- * {@link Job}.\r
- * \r
- * <p>\r
- * To start the job and wait for it to start, use\r
- * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die,\r
- * use {@link #disposeAndJoin()}.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class SleepingDatabaseJob extends DatabaseJob {\r
-\r
- private final Semaphore start = new Semaphore(0);\r
- private final Semaphore end = new Semaphore(0);\r
-\r
- public SleepingDatabaseJob(String name) {\r
- super(name);\r
- }\r
-\r
- @Override\r
- protected final IStatus run(IProgressMonitor monitor) {\r
- start.release();\r
- try {\r
- return work(monitor);\r
- } finally {\r
- try {\r
- end.acquire();\r
- } catch (InterruptedException e) {\r
- // Some other party wanted to kill the job. So be it.\r
- }\r
- }\r
- }\r
-\r
- protected IStatus work(IProgressMonitor monitor) {\r
- return Status.OK_STATUS;\r
- }\r
-\r
- public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException {\r
- schedule();\r
- start.acquire();\r
- start.release();\r
- return this;\r
- }\r
-\r
- public void scheduleAndWaitForRunningUninterruptibly() {\r
- schedule();\r
- start.acquireUninterruptibly();\r
- start.release();\r
- }\r
-\r
- public void disposeAndJoin() throws InterruptedException {\r
- end.release();\r
- join();\r
- }\r
-\r
- /**\r
- * @param name\r
- * @param runnable\r
- * @throws InterruptedException\r
- */\r
- public static void sleepWhile(String name, Runnable runnable) throws InterruptedException {\r
- SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
- try {\r
- dbjob.scheduleAndWaitForRunning();\r
- runnable.run();\r
- } finally {\r
- dbjob.disposeAndJoin();\r
- }\r
- }\r
-\r
- /**\r
- * @param name\r
- * @param runnable\r
- * @throws InterruptedException\r
- */\r
- public static void sleepUninterruptiblyWhile(String name, Runnable runnable) {\r
- SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
- try {\r
- dbjob.scheduleAndWaitForRunningUninterruptibly();\r
- runnable.run();\r
- } finally {\r
- try {\r
- dbjob.disposeAndJoin();\r
- } catch (InterruptedException e) {\r
- }\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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}.
+ *
+ * <p>
+ * 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) {
+ }
+ }
+ }
+
+}