Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
SwingWorkerSequentialExecutor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
20 
21 import com.google.common.util.concurrent.ThreadFactoryBuilder;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.stream.Collectors;
29 import javax.swing.SwingWorker;
30 
39 
40  private final ExecutorService executorService = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setNameFormat("SwingWorkerSequentialExecutor-%d").build());
41  private List<? extends SwingWorker<?, ?>> workers = Collections.emptyList();
42  private List<Future<?>> futures = Collections.emptyList();
43 
50  public synchronized void submit(List<? extends SwingWorker<?, ?>> submittedWorkers) {
51  // cancel currently running operations
52  cancelRunning();
53 
54  // if no workers, there is nothing to run
55  if (submittedWorkers == null) {
56  return;
57  }
58 
59  this.workers = new ArrayList<>(submittedWorkers);
60 
61  // start running the workers and capture the futures if there is a need to cancel them.
62  this.futures = this.workers.stream()
63  .map((w) -> executorService.submit(w))
64  .collect(Collectors.toList());
65  }
66 
70  public synchronized void cancelRunning() {
71  futures.forEach((f) -> f.cancel(true));
72  workers = Collections.emptyList();
73  futures = Collections.emptyList();
74  }
75 
81  public synchronized boolean isRunning() {
82  // borrowed from this stack overflow answer:
83  // https://stackoverflow.com/a/33845730
84 
85  for (Future<?> future : futures) {
86  if (!future.isDone()) {
87  return true;
88  }
89  }
90 
91  return false;
92  }
93 }
synchronized void submit(List<?extends SwingWorker<?,?>> submittedWorkers)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.