小编Yon*_*oni的帖子

Java NumberFormat忽略美国语言环境中的逗号?

我不理解NumberFormat中的某些东西,在美国语言环境中,它假设将逗号(",")视为一个组分隔符 - 数千.

为什么它会忽略此区域设置的错误位置的逗号?

NumberFormat format = DecimalFormat.getInstance(Locale.US); 
System.out.println(format.parse("5,500").longValue()); //5500
System.out.println(format.parse("550,0").longValue()); //5500
System.out.println(format.parse("5500,").longValue()); //5500
Run Code Online (Sandbox Code Playgroud)

任何其他的想法如何根据区域设置解析String到Long(asumming输入","在语言环境的错误位置应该失败)?

java locale localization number-formatting

5
推荐指数
1
解决办法
2073
查看次数

使用超时阻止ListenableFuture

我正在使用Spring 4.2.3 AsyncRestTemplate.exchange()来调用一些需要几秒钟的API,我期望listenableFuture.get(1,TimeUnit.SECONDS)将阻塞1秒然后抛出TimeOutException.

相反的是,listenableFuture.get()将在api调用的整个时间内阻塞(超过1秒)

    AsyncRestTemplate restTemplate = new AsyncRestTemplate();

    ListenableFuture<ResponseEntity<String>> listenableFuture = restTemplate.exchange(URL, HttpMethod.GET, null, String.class);
    log.debug("before callback");

    //...add callbacks

    try{
        log.debug("before blocking");
        listenableFuture.get(1, TimeUnit.SECONDS);
    }catch (InterruptedException e) {
        log.error(":GOT InterruptedException");
    } catch (ExecutionException e) {
        log.error(":GOT ExecutionException");
    } catch (TimeoutException e) {
        log.info(":GOT TimeoutException");
    }

    log.info("FINISHED");
Run Code Online (Sandbox Code Playgroud)

输出:

    09:15:21.596  DEBUG [main] org.springframework.web.client.AsyncRestTemplate:78 - Created asynchronous GET request for "http://localhost:4567/oia/wait?seconds=5"
    09:15:21.666  DEBUG [main] org.springframework.web.client.RestTemplate:720 - Setting request Accept header to [text/plain, application/xml, text/xml, application/json, application/*+xml, application/*+json, */*]
    09:15:21.679  DEBUG [main] com.zazma.flow.utils.FutureTest:74 …
Run Code Online (Sandbox Code Playgroud)

java concurrency spring future asyncresttemplate

5
推荐指数
1
解决办法
981
查看次数