| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | *******************************************************************************/ |
| 9 | |
| 10 | package org.eclipse.tracecompass.internal.provisional.tmf.ui.views.timegraph2.swtjfx; |
| 11 | |
| 12 | import java.lang.ref.WeakReference; |
| 13 | |
| 14 | import org.eclipse.core.runtime.jobs.Job; |
| 15 | import org.eclipse.jdt.annotation.Nullable; |
| 16 | |
| 17 | public class LatestJobExecutor { |
| 18 | |
| 19 | /** |
| 20 | * The latest job that was schedule in this queue. |
| 21 | */ |
| 22 | private WeakReference<@Nullable Job> fLatestJob = new WeakReference<>(null); |
| 23 | |
| 24 | public LatestJobExecutor() { |
| 25 | } |
| 26 | |
| 27 | public synchronized void schedule(Job newJob) { |
| 28 | Job latestJob = fLatestJob.get(); |
| 29 | if (latestJob != null) { |
| 30 | /* |
| 31 | * Cancel the existing job. Here's hoping it cooperates and ends |
| 32 | * quickly! |
| 33 | */ |
| 34 | latestJob.cancel(); |
| 35 | } |
| 36 | |
| 37 | /* Start the new job */ |
| 38 | fLatestJob = new WeakReference<>(newJob); |
| 39 | newJob.schedule(); |
| 40 | } |
| 41 | |
| 42 | } |