是否可以为Java 8 并行流指定自定义线程池?我找不到任何地方.
想象一下,我有一个服务器应用程序,我想使用并行流.但是应用程序很大且是多线程的,因此我想将它划分为区分.我不想在另一个模块的应用程序块任务的一个模块中执行缓慢的任务.
如果我不能为不同的模块使用不同的线程池,这意味着我无法在大多数现实情况下安全地使用并行流.
请尝试以下示例.在单独的线程中执行一些CPU密集型任务.这些任务利用并行流.第一个任务被破坏,因此每个步骤需要1秒(通过线程休眠模拟).问题是其他线程卡住并等待损坏的任务完成.这是一个人为的例子,但想象一下servlet应用程序和有人向共享fork连接池提交长时间运行的任务.
public class ParallelTest {
public static void main(String[] args) throws InterruptedException {
ExecutorService es = Executors.newCachedThreadPool();
es.execute(() -> runTask(1000)); //incorrect task
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.execute(() -> runTask(0));
es.shutdown();
es.awaitTermination(60, TimeUnit.SECONDS);
}
private static void runTask(int delay) {
range(1, 1_000_000).parallel().filter(ParallelTest::isPrime).peek(i -> Utils.sleep(delay)).max()
.ifPresent(max -> System.out.println(Thread.currentThread() + " " + max));
}
public static boolean isPrime(long n) {
return n > 1 && rangeClosed(2, (long) sqrt(n)).noneMatch(divisor …Run Code Online (Sandbox Code Playgroud) 我遇到过Java 8 CompletableFuture.exceptionally方法的奇怪行为.如果我执行此代码,它可以正常工作并打印java.lang.RuntimeException
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new RuntimeException());
future.exceptionally(e -> {
System.out.println(e.getClass());
return null;
});
Run Code Online (Sandbox Code Playgroud)
但是如果我在将来的处理中添加另一个步骤,就像thenApply异常类型更改为java.util.concurrent.CompletionException包含在内部的原始异常.
CompletableFuture<String> future = new CompletableFuture<>();
future.completeExceptionally(new RuntimeException());
future.thenApply(v-> v).exceptionally(e -> {
System.out.println(e);
return null;
});
Run Code Online (Sandbox Code Playgroud)
有什么理由说这应该发生吗?在我看来,这是非常令人惊讶的.
我正在将我们的项目从Hibernate 3.0升级到Hibernate 4.1.6.(我们目前正在使用弹簧3.1)
我在很多文章和HibernateTemplate文档中都读过,因为版本4.0不支持HibernateTemplate,我应该用调用sessionFactory.getCurrentSession()来取代会话来替换它的用法.
由于这个项目是在较早版本的Hibernate中启动的,HibernateTemplate因此鼓励使用它,我们目前在HibernateTemplate整个项目中有124个用法.我担心替换所有这些事件sessionFactory.getCurrentSession()可能会在我们的项目中插入回归错误.此外,还有一些地方HibernateTemplate用于非交易环境,没有"当前"会话.在这些情况下我该怎么办?打开一个新会话并自己处理(关闭)它?我使用时并非如此HibernateTemplate.
你有解决这些问题的好策略吗?
谢谢.
相关阅读:
运行这个简单的程序:
public static void main(final String... args)
{
System.out.println(BigDecimal.ZERO.scale());
System.out.println(new BigDecimal("0").scale());
System.out.println(new BigDecimal("0.0").stripTrailingZeros().scale());
System.out.println(new BigDecimal("1.0").stripTrailingZeros().scale());
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
0
1
0
Run Code Online (Sandbox Code Playgroud)
我的问题很简单:为什么不是第三个println输出0?那似乎合乎逻辑......
编辑:好的,所以,这是一个非常古老的错误:
事实上,它"适用于"任意数量的零:new BigDecimal("0.0000").stripTrailingZeroes().scale()是4!
我正在玩Java 8可完成的期货.我有以下代码:
CountDownLatch waitLatch = new CountDownLatch(1);
CompletableFuture<?> future = CompletableFuture.runAsync(() -> {
try {
System.out.println("Wait");
waitLatch.await(); //cancel should interrupt
System.out.println("Done");
} catch (InterruptedException e) {
System.out.println("Interrupted");
throw new RuntimeException(e);
}
});
sleep(10); //give it some time to start (ugly, but works)
future.cancel(true);
System.out.println("Cancel called");
assertTrue(future.isCancelled());
assertTrue(future.isDone());
sleep(100); //give it some time to finish
Run Code Online (Sandbox Code Playgroud)
使用runAsync我计划执行等待锁存器的代码.接下来我取消了未来,期望被抛入中断的异常.但似乎线程在await调用上仍然被阻塞,即使未来被取消(断言传递),也不会抛出InterruptedException.使用ExecutorService的等效代码按预期工作.它是CompletableFuture中的错误还是我的示例中的错误?
我正在尝试迁移到Gradle 7.3 中引入的测试套件。我想做的是将testImplementation依赖项添加到我的集成测试中。
testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
val integrationTest by registering(JvmTestSuite::class) {
dependencies {
implementation(project) // This adds dependencies to the prod code
// What to add to automatically use testImplementation deps?
}
...
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正试图从Logentries向Pingdom发送警报.Logentries支持webhooks,Pingdom可以与外部服务集成.基于http://royal.pingdom.com/2015/02/10/integrate-nagios-pingdom/我已经发现可以使用
POST https://anonymous:anonymous@api.pingdom.com/api/3.0/ims.incidents?triggerid=SERVICE_ID&source=service&data_type=nagios&data={}
Run Code Online (Sandbox Code Playgroud)
问题是我找不到有关此端点的任何文档.我想使用除nagios data_type之外的其他数据,但我找不到支持哪些类型和格式.
我遇到过Java 8 CompletableFuture thenCompose方法的奇怪行为.我有两个测试,仅在执行顺序上有所不同.两个测试都模拟thenCompose中生成的CompletableFuture中的失败.
@Test
public void completedAfter() {
CompletableFuture<String> future1 = new CompletableFuture<>();
CompletableFuture<String> future2 = new CompletableFuture<>();
future1.thenCompose(x -> future2).whenComplete((r, e) -> System.out.println("After: " + e));
future1.complete("value");
future2.completeExceptionally(new RuntimeException());
}
@Test
public void completedBefore() {
CompletableFuture<String> future1 = new CompletableFuture<>();
CompletableFuture<String> future2 = new CompletableFuture<>();
future1.complete("value");
future2.completeExceptionally(new RuntimeException());
future1.thenCompose(x -> future2).whenComplete((r, e) -> System.out.println("Before: " +e));
}
Run Code Online (Sandbox Code Playgroud)
输出是:
After: java.util.concurrent.CompletionException: java.lang.RuntimeException
Before: java.lang.RuntimeException
Run Code Online (Sandbox Code Playgroud)
问题是,为什么异常包含CompletionException在一个案例中但不包含在另一个案例中?
更新:这是相关的错误报告.它已被标记并解析为JDK中的错误.
当使用最新的Lombok v1.6.20生成的构造函数时,Jackson无法使用构造函数.
例如,反序列化时
@Data
@AllArgsConstructor
private static class TestObject {
private Integer a;
private String b;
}
Run Code Online (Sandbox Code Playgroud)
我正进入(状态
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of TestObject: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"a":0,"b":"0"}; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1206)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
Run Code Online (Sandbox Code Playgroud)
它适用于Lombok 1.6.18.迁移到Spring Boot 2时可能会发生此问题.
我正在使用JsonUnit的assertJsonEquals
我在代码中执行以下操作:
assertJsonEquals(resource("ExpecedResponse.json"),
ActualResponse, when(IGNORING_ARRAY_ORDER));
Run Code Online (Sandbox Code Playgroud)
的ActualResponse具有从HTTP POST的响应。
该ExpectedResponse.json是一些字段的JSON文件如如下:
{
"columnNames": [
"date",
"signalType",
"userId",
],
"values": [
[
222555888,
"OUT",
"000-000-111-444"
],
[
333666999,
"IN",
"000-000-222-333"
],
],
"lastUpdatedTimestamp": "2018-01-26T00:00:00Z"
}
Run Code Online (Sandbox Code Playgroud)
我将这两个响应与assertJsonEquals进行比较。
我的问题是:如何告诉它忽略检查lastUpdatedTimestamp字段,而是使用assertJsonEquals或您推荐的任何其他库检查其他所有内容?
如果我从ExpectedResponse.json中删除了lastUpdatedTimestamp,那么它会抱怨它丢失了!
谢谢您的帮助,谢谢。
java ×7
java-8 ×4
assert ×1
bigdecimal ×1
concurrency ×1
exception ×1
future ×1
gradle ×1
hibernate ×1
jackson2 ×1
java-stream ×1
json ×1
jsonunit ×1
junit ×1
jvm ×1
logentries ×1
lombok ×1
pingdom ×1
spring ×1