我在fixedThreadPool中调用了10个线程的外部API.现在我想动态减少这个线程数,因为API主机无法处理10个线程中的那些请求.我想将线程数减少到5.我想在没有应用程序重启的情况下执行此操作,即我想动态更改为固定大小的线程池的大小.我可以在ThreadPoolExecutor上调用setCorePoolSize(int)和setMaximumPoolSize(int).然而,javadoc说:
设置核心线程数.这将覆盖构造函数中设置的任何值.如果新值小于当前值,则当下一个空闲时,多余的现有线程将被终止.如果需要更大,则新线程将启动以执行任何排队任务.
因此,当他们说:"当他们下一次变为空闲时,将会终止多余的现有线程",当多余的现有线程将变为空闲时?如果我的任务队列总是满的,这些多余的现有线程是否可以空闲?
我的问题与以下问题相同;
Jackson POJOPropertyBuilder在POJO中找到多个setter
但是,因为我使用"springfox-swagger2",我不使用SwaggerSpringMvcPlugin.
有没有办法解决这个问题?
Application.java
/* Makes this Application Run as Spring Boot Application */
@SpringBootApplication
/* Enables Swagger2API for documentation */
@EnableSwagger2
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
LogService.info(Application.class.getName(), "CustomerAPI Service Started");
}
@Bean
public Docket customerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Customer Application")
.apiInfo(apiInfo())
.select()
.paths(myAppPaths())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Customer API")
.description("Some Description to Show")
.termsOfServiceUrl(null)
.contact("Test Test")
.license("Apache License Version 2.0") …Run Code Online (Sandbox Code Playgroud) 在中Java8,让List<Item> list我按以下顺序进行处理:
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
for (int i1 = 0; i1 < list.size() - 1; i1++) {
Item item1 = list.get(i1);
for (int i2 = i1 + 1; i2 < list.size(); i2++) {
Item item2 = list.get(i2);
doSomething(item1, item2);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我处理列表中所有有序的项目对(索引item1<的索引,<的索引item2)。现在,我想doSomething(item1, item2)为每个有序对并行运行函数。实现这一目标的最佳策略是什么?对最快的代码感兴趣。Java8流欢迎。
doSomething比如做:map.put(item1.key + " " + item2.key, item1.val + item2.val);。
有序对的数量n * (n - 1) / 2,其中 …
为选错的头衔道歉,想不出一个更好的头衔.请随时建议一个新的标题.
我想使用Java 8 lambda表达式编写以下代码
List<Function<MyClass, Optional<String>>> functions //assume initialized
for (final Function<MyClass, Optional<String>> function : functions) {
final Optional<String> result = function.apply(objectOfMyClass);
if (result.isPresent()) { //as soon as one of the function returns a non-null string, return
return result; //
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何去做的任何建议?
我在下面的代码中使用函数接口有一个问题.这会将字符串转换为整数,并将结果整数转换回String.
Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
Run Code Online (Sandbox Code Playgroud)
我期待backToString的类型参数<Integer, String>代替<String, String>.这是因为,我们将toInteger函数的整数结果传递给String :: valueOf方法.
请解释.
实际任务是能够通过从文件中读取来返回Stream,但是这样做而不将整个文件(或解析的集合)完全加载到内存中.该流的目的可以在以后确定 - 例如保存到DB.开发人员将拥有反序列化流的句柄(而不是反序列化的集合).
问题是,不能保证文件中的一行等于一个MyEntity对象(在这种情况下我可以使用这篇文章:http://blog.codeleak.pl/2014/05/parsing-file-with -stream-api-in-java-8.html)
通常,可以找到这样的情况:如果输入流,则需要返回通过将可变数量的输入流项映射到一个输出流项而构造的输出流.
所以,到目前为止,我的解决方案是使用供应商,如下所示:
public class Parser{
public Stream<MyEntity> parse(final InputStream stream) {
return Stream.generate(new AggregatingSupplier(stream));
}
private class AggregatingSupplier implements Supplier<MyEntity> {
private final Scanner r;
public AggregatingSupplier(final InputStream source) {
this.r= new Scanner(source);
}
@Override
public MyEntity get() {
MyEntity re=new MyEntity();
while (r.hasNextLine() &&!re.isComplete()){
String line=r.nextLine();
// ... do some processing
}
return re;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于使用Stream.generate获得的流是无限的.没有停止条件.抛出一个异常(有点).或者选择完全不同的(经典)方法.
我们最近决定将我们的解决方案从java 7迁移到8.但是在第一步我们发现java 8中有一些实现更改,比如String.split()方法这可能会导致用Java 7编写的代码出现问题!
我们现在将我们的项目推向Java 7,以便更多地研究这些差异.
你知道Java 8中的任何其他变化都会导致同样的问题吗?
PS:我知道Java 8的新功能通过更改我的意思是那些使用相同API(方法签名)的更改,但不同的实现可能会导致不同的结果!! 喜欢这个方法 - > String.split()
在投票前阅读评论!!
我有一个Java 8流的问题,其中数据是以突然的批量处理,而不是在请求时.我有一个相当复杂的流 - 流,必须并行化,因为我concat用来合并两个流.
我的问题源于这样一个事实,即数据似乎在大量分钟内 - 有时甚至是数小时 - 被解析.我希望一旦Stream读取传入数据就会发生这种处理,以分散工作量.批量处理几乎在所有方面都是违反直觉的.
所以,问题是为什么这个批量收集发生以及如何避免它.
我的输入是一个未知大小的Spliterator,我使用forEach作为终端操作.
import java.util.*;
class TreeMapDemo
{
public static void main(String args[])
{
Comparator <String> c1 = (str1, str2) -> 0;
Comparator <String> c2 = (str1, str2) -> 1;
TreeMap <String, Double> tm1 = new TreeMap(c1.thenComparing(c2));
//Working fine
TreeMap <String, Double> tm2 = new TreeMap(((str1, str2) -> 0).thenComparing((str1, str2) -> 1));
//Error: Lambda expression not expected here
//<none> can not be dereferenced
}
}
Run Code Online (Sandbox Code Playgroud)
我的查询是:
如果
c1 = (str1, str2) -> 0而且c2 = (str1, str2) -> 1,
那么为什么
c1.thenComparing(c2) …
我有Map以下类型
public class MapUtils {
private Map<String, Integer> queryCounts = new HashMap<>();
public void averageCounters(){
int totalCounts = queryCounts.values().stream().reduce(0, Integer::sum);
queryCounts = queryCounts.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
(Map.Entry::getValue)/totalCounts
));
}
Run Code Online (Sandbox Code Playgroud)
这不会编译并在此行中显示错误(Map.Entry::getValue)/totalCounts.我该如何解决?有没有更好的方法来获得Map使用Java 8 API的平均值?
编辑:这是一个更好的方法吗?
queryCounts.entrySet()
.forEach(entry -> queryCounts.put(entry.getKey(),
entry.getValue()/totalCounts));
Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×9
java-stream ×4
lambda ×2
api ×1
dereference ×1
dictionary ×1
dot-operator ×1
list ×1
spring-boot ×1
stream ×1
swagger-2.0 ×1
swagger-ui ×1