1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics;
\r
14 import java.util.concurrent.Semaphore;
\r
16 import org.eclipse.core.runtime.IProgressMonitor;
\r
17 import org.eclipse.core.runtime.IStatus;
\r
18 import org.eclipse.core.runtime.Status;
\r
19 import org.eclipse.core.runtime.jobs.Job;
\r
22 * A dummy database-family job that can be used for signaling that a large
\r
23 * database job is in progress although it is technically not running in a
\r
27 * To start the job and wait for it to start, use
\r
28 * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die,
\r
29 * use {@link #disposeAndJoin()}.
\r
31 * @author Tuukka Lehtonen
\r
33 public class SleepingDatabaseJob extends DatabaseJob {
\r
35 private final Semaphore start = new Semaphore(0);
\r
36 private final Semaphore end = new Semaphore(0);
\r
38 public SleepingDatabaseJob(String name) {
\r
43 protected final IStatus run(IProgressMonitor monitor) {
\r
46 return work(monitor);
\r
50 } catch (InterruptedException e) {
\r
51 // Some other party wanted to kill the job. So be it.
\r
56 protected IStatus work(IProgressMonitor monitor) {
\r
57 return Status.OK_STATUS;
\r
60 public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException {
\r
67 public void scheduleAndWaitForRunningUninterruptibly() {
\r
69 start.acquireUninterruptibly();
\r
73 public void disposeAndJoin() throws InterruptedException {
\r
81 * @throws InterruptedException
\r
83 public static void sleepWhile(String name, Runnable runnable) throws InterruptedException {
\r
84 SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);
\r
86 dbjob.scheduleAndWaitForRunning();
\r
89 dbjob.disposeAndJoin();
\r
96 * @throws InterruptedException
\r
98 public static void sleepUninterruptiblyWhile(String name, Runnable runnable) {
\r
99 SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);
\r
101 dbjob.scheduleAndWaitForRunningUninterruptibly();
\r
105 dbjob.disposeAndJoin();
\r
106 } catch (InterruptedException e) {
\r