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