本文整理匯總了Java中java.util.concurrent.FutureTask類的典型用法代碼示例。如果您正苦於以下問題:Java FutureTask類的具體用法?Java FutureTask怎麽用?Java FutureTask使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FutureTask類屬於java.util.concurrent包,在下文中一共展示了FutureTask類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: runFuture
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Test
public void runFuture() {
for (int i = 0; i < 500; i++) {
CompositeDisposable set = new CompositeDisposable();
final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
set.add(run);
final FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null);
Runnable r1 = new Runnable() {
@Override
public void run() {
run.call();
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
run.setFuture(ft);
}
};
TestCommonHelper.race(r1, r2);
}
}
示例2: func_181617_a
import java.util.concurrent.FutureTask; //導入依賴的package包/類
public static <V> V func_181617_a(FutureTask<V> p_181617_0_, Logger p_181617_1_)
{
try
{
p_181617_0_.run();
return p_181617_0_.get();
}
catch (ExecutionException executionexception)
{
p_181617_1_.fatal((String)"Error executing task", (Throwable)executionexception);
}
catch (InterruptedException interruptedexception)
{
p_181617_1_.fatal((String)"Error executing task", (Throwable)interruptedexception);
}
return (V)null;
}
示例3: waitForTasksCompleted
import java.util.concurrent.FutureTask; //導入依賴的package包/類
private Set<Location> waitForTasksCompleted() {
try {
Set<Location> targets = new HashSet<>();
FutureTask<Set<Location>> task;
while ((task = tasks.poll()) != null) {
// wait for completion
if (!task.isDone())
targets.addAll(task.get());
}
return targets;
} catch (InterruptedException|ExecutionException e) {
throw new Error(e);
}
}
示例4: call
import java.util.concurrent.FutureTask; //導入依賴的package包/類
private <T> Future<T> call(Callable callable, boolean async){
if(async){
return Pool.submit(callable);
}else{
FutureTask<T> future = new FutureTask(callable);
future.run();
return future;
}
}
示例5: textProperty
import java.util.concurrent.FutureTask; //導入依賴的package包/類
public StringProperty textProperty() throws InterruptedException, ExecutionException {
if (Platform.isFxApplicationThread()) {
return ((javafx.scene.control.Button) body).textProperty();
}
final FutureTask<StringProperty> task = new FutureTask<>(() -> ((javafx.scene.control.Button) body).textProperty());
Platform.runLater(task);
return task.get();
}
示例6: afterExecute
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Override
protected void afterExecute(Runnable task, Throwable exception) {
if (task instanceof FutureTask<?>) {
FutureTask<?> futureTask = (FutureTask<?>) task;
if (!futureTask.isCancelled()) {
try {
futureTask.get();
} catch (ExecutionException ee) {
exception = ee.getCause();
} catch (InterruptedException ie) {
exception = ie;
}
}
}
if (exception != null) {
LOG.error("Exception during execution of task in DeletionService",
exception);
}
}
示例7: EWFOutput
import java.util.concurrent.FutureTask; //導入依賴的package包/類
/**
* Creates a new EWFOutput with the given parameters.
* @param toImage The device this image is of.
* @param chunkQueue The source of data chunks to build the {@link SectorsSection Sectors} segments with.
* @param outputFile The file to write the output to. Should end in .E01.
* @param md5Hash The source of the MD5 digest hash.
* @param sha1Hash The source of the SHA1 digest hash.
* @param options The options used for this image.
* @throws IOException
*/
public EWFOutput(Device toImage, BlockingQueue<Future<DataChunk>> chunkQueue, File outputFile,
FutureTask<byte[]> md5Hash, FutureTask<byte[]> sha1Hash, AdvancedOptions options) throws IOException {
super("EWFWriter");
this.fileNumber = 1;
this.outputFile = outputFile;
this.expectedSize = toImage.getSize();
this.serialNumber = toImage.getSerialNumber();
this.volumeManager = new VolumeSectionManager(toImage);
if (!outputFile.exists()) {
if (this.outputFile.getParentFile() != null) {
this.outputFile.getParentFile().mkdirs();
}
this.outputFile.createNewFile();
}
this.md5HashSource = md5Hash;
this.sha1HashSource = sha1Hash;
currentOutputFile = new RandomAccessFile(this.outputFile.getAbsolutePath(), "rw");
currentOutputFile.setLength(0);
this.chunkQueue = chunkQueue;
this.sectorSize = 512; // TODO: remove hardcoded
this.options = options;
this.startTime = LocalDateTime.now();
}
示例8: run
import java.util.concurrent.FutureTask; //導入依賴的package包/類
public void run(Consumer<HttpResponse> consumer) throws IOException {
System.out.println("Expecting a response after GET from: " + this.url.toString() + "\n\n");
try (SocketChannel socketChannel = SocketChannel.open(this.address)) {
socketChannel.configureBlocking(true);
writeGetRequestTo(socketChannel);
FutureTask<Void> futureTask = HttpResponseReader.fromChannel(socketChannel, consumer);
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(futureTask);
executor.awaitTermination(this.timeoutSeconds, TimeUnit.SECONDS);
executor.shutdown();
System.out.println("\n\nExecutor shutdown after waiting: " + this.timeoutSeconds + "s.");
} catch (InterruptedException e) {
System.out.println("Error occured during task execution: " + e.getMessage());
}
}
示例9: resolve
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Override
public java.util.concurrent.Future<ProjectProblemsProvider.Result> resolve() {
ProjectProblemsProvider.Result res;
if (action != null) {
action.actionPerformed(null);
String text = (String) action.getValue(ACT_START_MESSAGE);
if (text != null) {
res = ProjectProblemsProvider.Result.create(ProjectProblemsProvider.Status.RESOLVED, text);
} else {
res = ProjectProblemsProvider.Result.create(ProjectProblemsProvider.Status.RESOLVED);
}
} else {
res = ProjectProblemsProvider.Result.create(ProjectProblemsProvider.Status.UNRESOLVED, "No resolution for the problem");
}
RunnableFuture<ProjectProblemsProvider.Result> f = new FutureTask(new Runnable() {
@Override
public void run() {
}
}, res);
f.run();
return f;
}
示例10: main
import java.util.concurrent.FutureTask; //導入依賴的package包/類
public static void main(String[] args) throws InterruptedException, ExecutionException {
Date begin = new Date();
Calculate calculate = new Calculate();
int c = 0;
//異步計算
int two = 0;
CallableDemo cDemo = new CallableDemo();
cDemo.setCalculate(calculate);
FutureTask<Integer> fTask = new FutureTask<>(cDemo);
new Thread(fTask, "有返回值的線程").start();
int one = calculate.calOne();
two = fTask.get();
//求和
c = one + two;
Date end = new Date();
System.out.println("計算結果:" + c);
long aaa = end.getTime() - begin.getTime();
System.out.println("計算耗時:" + aaa);
}
示例11: getArrowRender
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Override
public TimeGraphArrowRender getArrowRender(TimeGraphTreeRender treeRender, TimeRange timeRange, FutureTask<?> task) {
TimeGraphArrowSeries series = getArrowSeries();
List<TimeGraphTreeElement> treeElems = treeRender.getAllTreeElements();
/* Draw 2 arrows total */
TimeGraphEvent startEvent = new TimeGraphEvent(ts(timeRange, 0.3), treeElems.get(6));
TimeGraphEvent endEvent = new TimeGraphEvent(ts(timeRange, 0.8), treeElems.get(4));
TimeGraphArrow arrow1 = new TimeGraphArrow(startEvent, endEvent, series);
startEvent = new TimeGraphEvent(ts(timeRange, 0.5), treeElems.get(10));
endEvent = new TimeGraphEvent(ts(timeRange, 0.6), treeElems.get(7));
TimeGraphArrow arrow2 = new TimeGraphArrow(startEvent, endEvent, series);
List<TimeGraphArrow> arrows = ImmutableList.of(arrow1, arrow2);
return new TimeGraphArrowRender(timeRange, arrows);
}
示例12: getStateRender
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Override
public TimeGraphStateRender getStateRender(TimeGraphTreeElement treeElement,
TimeRange timeRange, long resolution, FutureTask<?> task) {
int entryIndex = Integer.valueOf(treeElement.getName().substring(TestModelProvider.ENTRY_NAME_PREFIX.length()));
long stateLength = entryIndex * DURATION_FACTOR;
List<TimeGraphStateInterval> intervals = LongStream.iterate(timeRange.getStartTime(), i -> i + stateLength)
.limit((timeRange.getDuration() / stateLength) + 1)
.mapToObj(startTime -> {
long endTime = startTime + stateLength - 1;
StateDefinition stateDef = getNextStateDef();
return new BasicTimeGraphStateInterval(startTime, endTime, treeElement, stateDef, stateDef.getName(), Collections.emptyMap());
})
.collect(Collectors.toList());
return new TimeGraphStateRender(timeRange, treeElement, intervals);
}
示例13: testGet_NegativeInfinityTimeout
import java.util.concurrent.FutureTask; //導入依賴的package包/類
/**
* timed get with most negative timeout works correctly (i.e. no
* underflow bug)
*/
public void testGet_NegativeInfinityTimeout() throws Exception {
final ExecutorService pool = Executors.newFixedThreadPool(10);
final Runnable nop = new Runnable() { public void run() {}};
final FutureTask<Void> task = new FutureTask<>(nop, null);
final List<Future<?>> futures = new ArrayList<>();
Runnable r = new Runnable() { public void run() {
for (long timeout : new long[] { 0L, -1L, Long.MIN_VALUE }) {
try {
task.get(timeout, NANOSECONDS);
shouldThrow();
} catch (TimeoutException success) {
} catch (Throwable fail) {threadUnexpectedException(fail);}}}};
for (int i = 0; i < 10; i++)
futures.add(pool.submit(r));
try {
joinPool(pool);
for (Future<?> future : futures)
checkCompletedNormally(future, null);
} finally {
task.run(); // last resort to help terminate
}
}
示例14: paintArrowsOfProvider
import java.util.concurrent.FutureTask; //導入依賴的package包/類
private void paintArrowsOfProvider(TimeGraphTreeRender treeRender, TimeRange timeRange,
TimeGraphModelArrowProvider arrowProvider, @Nullable FutureTask<?> task) {
ArrowConfig config = fArrowProvidersConfig.get(arrowProvider);
if (config == null) {
/* Should not happen... */
return;
}
TimeGraphArrowRender arrowRender = arrowProvider.getArrowRender(treeRender, timeRange, task);
Collection<Arrow> arrows = prepareArrows(treeRender, arrowRender, config.getStroke());
Platform.runLater(() -> {
config.getGroup().getChildren().clear();
config.getGroup().getChildren().addAll(arrows);
});
}
示例15: getArrowRender
import java.util.concurrent.FutureTask; //導入依賴的package包/類
@Override
public TimeGraphArrowRender getArrowRender(TimeGraphTreeRender treeRender, TimeRange timeRange, @Nullable FutureTask<?> task) {
TimeGraphArrowSeries series = getArrowSeries();
List<TimeGraphTreeElement> treeElems = treeRender.getAllTreeElements();
/* Draw 2 arrows total */
TimeGraphEvent startEvent = new TimeGraphEvent(ts(timeRange, 0.3), treeElems.get(6));
TimeGraphEvent endEvent = new TimeGraphEvent(ts(timeRange, 0.8), treeElems.get(4));
TimeGraphArrow arrow1 = new TimeGraphArrow(startEvent, endEvent, series);
startEvent = new TimeGraphEvent(ts(timeRange, 0.5), treeElems.get(10));
endEvent = new TimeGraphEvent(ts(timeRange, 0.6), treeElems.get(7));
TimeGraphArrow arrow2 = new TimeGraphArrow(startEvent, endEvent, series);
List<TimeGraphArrow> arrows = ImmutableList.of(arrow1, arrow2);
return new TimeGraphArrowRender(timeRange, arrows);
}