Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

multithreading - How to fix ForkJoinPool Size Using lettuce?

Hi I want to fix number of thread in ForkJoinPool

example ..

 int parallelism = 2;
 ForkJoinPool forkJoinPool = new ForkJoinPool(parallelism);
 forkJoinPool.submit(() -> {
          return IntStream.range(1,10).parallel().peek(i->{
                System.out.println(Thread.currentThread().getName());
           }).sum();
 }).join();

Result----------

2021-01-28 16:56:57.103  INFO 12465 --- [    Test worker] 
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-0
ForkJoinPool-1-worker-0
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-0
ForkJoinPool-1-worker-0
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-1
2021-01-28 16:56:57.297  INFO 12465 --- 

It is Good but Using Redis(lettuce)

ValueOperations<String, Object> stringObjectValueOperations = 
                                              redisTemplate.opsForValue();
int parallelism = 2; 
ForkJoinPool forkJoinPool = new ForkJoinPool(parallelism);
    forkJoinPool.submit(() -> {
       return IntStream.range(1,10).parallel().peek(i->{
               stringObjectValueOperations.get("TEST");
               System.out.println(Thread.currentThread().getName());
            }).sum();
       }).join();
    }

--- Result 
2021-01-28 17:04:01.867  INFO 13531 --- [    Test worker] 
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-0
ForkJoinPool-1-worker-2
ForkJoinPool-1-worker-3
ForkJoinPool-1-worker-4
ForkJoinPool-1-worker-6
ForkJoinPool-1-worker-7
ForkJoinPool-1-worker-5
ForkJoinPool-1-worker-11

It is terrible ... non fixed thread

I guess Because it is Related Async&non-block of lettuce But I don't know exactly

If Fixed Thread is possible, please give me an answer

But If Fixed Thread is impossible, please give me an exact reason .. I wander

question from:https://stackoverflow.com/questions/65933355/how-to-fix-forkjoinpool-size-using-lettuce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...