首页 \ 问答 \ 线程池线程?(pthread threadpool?)

线程池线程?(pthread threadpool?)

pthread库中是否包含一个线程池实现? 还是有常用的库,人们使用/


does the pthread lib include a threadpool implementation? or are there commonly used libs that folks use/


原文:https://stackoverflow.com/questions/4568517
更新时间:2024-01-19 06:01

最满意答案

由于混合类型,这有点令人费解,但这可以完全用lambdas来完成:

Stream<Object> stream = Stream.concat(list1.stream(), list2.stream());
System.out.println(stream.sorted((Object o1, Object o2) ->
    (o1 instanceof String ? 2*((String)o1).length() : 1+2*(Integer)o1)
    - (o2 instanceof String ? 2*((String)o2).length() : 1+2*(Integer)o2)
  ).collect(Collectors.toList()));

基本的想法是使用标准的compareTo逻辑a - b来定义一个比较长度(如果它是一个字符串)和该值(如果它是一个整数)的lambda。 但是,为了处理一个关系的情况,我将所有值乘以2 ,然后在Integer数值中加上一个额外的1 。 这会导致Integer在排序中最后排序。

演示: http//ideone.com/39ppGD


This is slightly convoluted because of the mixed types, but here is one way it could be done entirely with lambdas:

Stream<Object> stream = Stream.concat(list1.stream(), list2.stream());
System.out.println(stream.sorted((Object o1, Object o2) ->
    (o1 instanceof String ? 2*((String)o1).length() : 1+2*(Integer)o1)
    - (o2 instanceof String ? 2*((String)o2).length() : 1+2*(Integer)o2)
  ).collect(Collectors.toList()));

The basic idea is to define a lambda that compares the length (if it's a string) to the value (if it's an integer) using the standard compareTo logic a - b. However, to handle the case of a tie I've multiplied all the values by 2, and then added an extra 1 to the Integer values. This causes Integer to be sorted last in the case of a tie.

Demo: http://ideone.com/39ppGD

相关问答

更多