小编Kon*_*gin的帖子

Java 8并行流是否为序列使用相同的线程

让我们说我们有这样的事情:

LongStream.range(0, 10).parallel()
.filter(l -> {
  System.out.format("filter: %s [%s]\n", l, Thread.currentThread().getName());
  return l % 2 == 0;
})
.map(l -> {
  System.out.format("map:    %s [%s]\n", l, Thread.currentThread().getName());
  return l;
});
Run Code Online (Sandbox Code Playgroud)

如果你运行这个程序输出将是这样的:

filter: 6 [main]
map:    6 [main]
filter: 5 [main]
filter: 4 [ForkJoinPool.commonPool-worker-2]
map:    4 [ForkJoinPool.commonPool-worker-2]
filter: 1 [ForkJoinPool.commonPool-worker-3]
filter: 2 [ForkJoinPool.commonPool-worker-1]
filter: 0 [ForkJoinPool.commonPool-worker-3]
filter: 3 [ForkJoinPool.commonPool-worker-2]
filter: 8 [main]
filter: 7 [ForkJoinPool.commonPool-worker-2]
filter: 9 [ForkJoinPool.commonPool-worker-2]
map:    0 [ForkJoinPool.commonPool-worker-3]
map:    2 [ForkJoinPool.commonPool-worker-1]
map:    8 [main]`
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的,每个long的每个任务序列都由同一个线程执行.这是我们可以依赖的东西,还是巧合?执行期间线程可以"共享"任务吗?

java java-8 java-stream

10
推荐指数
1
解决办法
970
查看次数

Java String中的hashCode实现

只是好奇,在String的hashCode实现中,在hashCode实现中创建额外引用的原因是什么(v 1.8.0_65):

public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}
Run Code Online (Sandbox Code Playgroud)

考虑到value最终只在构造函数中创建(即线程安全)为什么我们需要变量val []引用?

即,这将工作:

public int hashCode() {
    if (hash == 0 && value.length > 0) {
        int h = 0;
        for (int i = 0; i < value.length; i++) { …
Run Code Online (Sandbox Code Playgroud)

java

7
推荐指数
1
解决办法
557
查看次数

如何在quarkus microprofile案例中配置rest客户端

当使用 Quarkus microprofile 作为 REST 客户端时,如何配置底层 HttpClient?比如重试次数、每个主机的连接池大小等等?另外是否可以以某种方式强制客户端重新启动(因此连接池将重新启动)?

microprofile quarkus

3
推荐指数
1
解决办法
7420
查看次数

标签 统计

java ×2

java-8 ×1

java-stream ×1

microprofile ×1

quarkus ×1