本文整理了Java中com.linkedin.parseq.Task.shareable()
方法的一些代码示例,展示了Task.shareable()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task.shareable()
方法的具体详情如下:
包路径:com.linkedin.parseq.Task
类名称:Task
方法名:shareable
[英]Creates a new task that can be safely shared within a plan or between multiple plans. Cancellation of returned task will not cause cancellation of the original task.
Sharing tasks within a plan or among different plans is generally not safe because task can be cancelled if it's parent has been resolved. Imagine situation where fetch
task that fetches data from a remote server is shared among few plans. If one of those plans times out then all started tasks that belong to it will be automatically cancelled. This means that fetch
may also be cancelled and this can affect other plans that are still running. Similar situation can happen even within one plan if task is used multiple times.
In example below google
task has timeout 10ms what causes entire plan to fail and as a consequence all tasks that belong to it that have been started - in this case bing
task. This may be problematic if bing
task is used somewhere else.
final Task
google = HttpClient.get("http://google.com").task();
final Task
bing = HttpClient.get("http://bing.com").task();
// this task will fail because google task will timeout after 10ms
// as a consequence bing task will be cancelled
final Task both = Task.par(google.withTimeout(10, TimeUnit.MILLISECONDS), bing);
shareable
method solves above problem. Task returned by shareable()
can be can be cancelled without affecting original task.
final Task
google = HttpClient.get("http://google.com").task();
final Task
bing = HttpClient.get("http://bing.com").task();
// this task will fail because wrapped google task will timeout after 10ms
// notice however that original googel and bing tasks were not cancelled
final Task both =
Task.par(google.shareable().withTimeout(10, TimeUnit.MILLISECONDS), bing.shareable());
[中]创建可以在一个计划内或多个计划之间安全共享的新任务。取消返回的任务不会导致取消原始任务。
在一个计划内或在不同计划之间共享任务通常是不安全的,因为如果任务的父级已解决,则可以取消该任务。想象一下这样的情况:从远程服务器获取数据的fetch
任务在几个计划之间共享。如果其中一个计划超时,则属于该计划的所有已启动任务将自动取消。这意味着fetch
也可能被取消,这可能会影响其他仍在运行的计划。如果任务被多次使用,即使在一个计划内也可能发生类似的情况。
在下面的例子中,google
任务有10毫秒的超时,这会导致整个计划失败,因此所有属于它的任务都已启动——在本例中是bing
任务。如果bing
任务在其他地方使用,这可能会有问题。
final Task
google = HttpClient.get("http://google.com").task();
final Task
bing = HttpClient.get("http://bing.com").task();
// this task will fail because google task will timeout after 10ms
// as a consequence bing task will be cancelled
final Task both = Task.par(google.withTimeout(10, TimeUnit.MILLISECONDS), bing);
shareable
方法解决了上述问题。shareable()
返回的任务可以在不影响原始任务的情况下取消。
final Task
google = HttpClient.get("http://google.com").task();
final Task
bing = HttpClient.get("http://bing.com").task();
// this task will fail because wrapped google task will timeout after 10ms
// notice however that original googel and bing tasks were not cancelled
final Task both =
Task.par(google.shareable().withTimeout(10, TimeUnit.MILLISECONDS), bing.shareable());
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple4Task<T1, T2, T3, T4> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple13Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple11Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple15Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple9Task<T1, T2, T3, T4, T5, T6, T7, T8, T9> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple8Task<T1, T2, T3, T4, T5, T6, T7, T8> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple12Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple3Task<T1, T2, T3> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple14Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple5Task<T1, T2, T3, T4, T5> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple6Task<T1, T2, T3, T4, T5, T6> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple2Task<T1, T2> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple7Task<T1, T2, T3, T4, T5, T6, T7> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
/**
* {@inheritDoc}
*/
@Override
public default Tuple10Task<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> shareable() {
return cast(Task.super.shareable());
};
代码示例来源:origin: linkedin/parseq
@Test
public void testShareable() {
Task<String> task = Task.par(_strategy.batchable(0).shareable(), _strategy.batchable(1).shareable())
.map("concat", (s0, s1) -> s0 + s1);
String result = runAndWait("TestSimpleBatchingStrategy.testShareable", task);
assertEquals(result, "01");
}
代码示例来源:origin: linkedin/parseq
@Test
public void testShareableWithPar() {
final AtomicInteger counter = new AtomicInteger();
Task<String> task = Task.callable("increaser", () -> {
counter.incrementAndGet();
return "hello";
} );
Task<String> test =
Task.par(task.shareable().map(x -> x + "1"), task.shareable().map(x -> x + "2")).map((a, b) -> a + b);
runAndWait("TestTaskReuse.testShareableWithPar", test);
assertEquals(counter.get(), 1);
assertEquals(test.get(), "hello1hello2");
}
代码示例来源:origin: linkedin/parseq
@Test
public void testLastTaskAlreadyResolvedShareable() {
final AtomicInteger counter = new AtomicInteger();
final Task<String> bob = Task.value("bob", "bob");
runAndWait("TestTaskReuse.testLastTaskAlreadyResolvedShareable-bob", bob);
Task<String> task = Task.callable("increaser", () -> {
counter.incrementAndGet();
return "hello";
} );
Task<String> test1 = task.andThen(bob.shareable());
runAndWait("TestTaskReuse.testLastTaskAlreadyResolvedShareable", test1);
assertEquals(counter.get(), 1);
}
代码示例来源:origin: linkedin/parseq
@Test
public void testShareableTaskType() {
Task<Integer> value = Task.value(10);
Task<Integer> shareableTask = value.shareable();
assertEquals(shareableTask.getShallowTrace().getTaskType(), TaskType.SHAREABLE.getName());
}
代码示例来源:origin: linkedin/parseq
private Task<?> task() {
return Task.value("kldfjlajflskjflsjfslkajflkasj").map("length", s -> s.length()).map("+1", s -> s + 1)
.map("+2", s -> s + 2).map("+3", s -> s + 3).shareable().recoverWith(t -> Task.value(0))
.flatMap(x -> Task.value(x * 40)).map(x -> x -10);
}
代码示例来源:origin: linkedin/parseq
@Test
public void testShareableCancellationPar() {
Task<String> task = delayedValue("hello", 50, TimeUnit.MILLISECONDS);
Task<String> test1 =
Task.par(task.shareable().map(x -> x + "1"), Task.failure(new RuntimeException("ups"))).map((a, b) -> a + b);
try {
runAndWait("TestTaskReuse.testShareableCancellationPar-test1", test1);
fail("should have failed!");
} catch (Exception ex) {
assertTrue(test1.isFailed());
}
Task<String> test2 =
Task.par(task.shareable().map("1", x -> x + "1"), task.shareable().map("2", x -> x + "2")).map((a, b) -> a + b);
runAndWait("TestTaskReuse.testShareableCancellationPar-test2", test2);
assertEquals(test2.get(), "hello1hello2");
}
内容来源于网络,如有侵权,请联系作者删除!