Tag: 超时

60秒后停止异步任务

我想在60秒后停止异步任务 我知道有一个方法get(time,timeunit)我应该在doInBackground中使用,但我不知道如何检查是否已达到超时请提供该样本一行代码。 谢谢

超时会抛出什么exception?

HTMLUnit中连接超时引发了什么exception?

Google Guava Cache – 在运行时更改驱逐超时值

我使用以下内容: LoadingCache inQueueLoadingCache = CacheBuilder.newBuilder() .expireAfterWrite(120, TimeUnit.SECONDS) .removalListener(inQueueRemovalListener) .build(inQueueCacheLoader); 每120秒后,缓存条目被逐出,并按预期工作。 我的问题是:如何更改当前缓存的超时值,比如120到60秒? 在此更改期间缓存条目会发生什么?

Hystrix在运行时忽略超时

我正在试验一下Hystrix。 我支持文档,即使是通过’run’同步调用Hystrix命令也默认在一个线程中运行,并且应该受Hystrix中配置的超时限制。 但是当我尝试它时,似乎没有超时。 我是否误解了文档? 或者我做错了什么? 有没有办法通过同步调用获得超时行为? 更具体:我有一个’SimpleService’需要5秒才能返回。 这包含在Hystrix命令中,超时为500ms: public class WebRequestCommand extends HystrixCommand { private final SimpleService baneService; protected WebRequestCommand(SimpleService baneService) { super( Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(“test”)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter() .withExecutionIsolationThreadTimeoutInMilliseconds(500))); this.baneService = baneService; } @Override protected String run() { return baneService.connectToBane(); } @Override protected String getFallback() { return “SERVICE NOT AVAILABLE”; } } 如果我这样称呼它: WebRequestCommand webService = new […]

如何通过Junit获得成功并暂停?

@Test (expected=TimeoutException.class,timeout=1000) public void fineForFiveSeconds() { foo.doforever(); fail(“This line should never reached”); } 这是我的测试代码。 我想要的是运行doforever()一段时间然后让测试成功。

在固定时间后中断线程,它是否必须抛出InterruptedException?

我想在一段固定的时间后中断一个线程。 其他人问了同样的问题,最高投票的答案( https://stackoverflow.com/a/2275596/1310503 )给出了下面的解决方案,我稍微缩短了。 import java.util.Arrays; import java.util.concurrent.*; public class Test { public static void main(String[] args) throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.invokeAll(Arrays.asList(new Task()), 2, TimeUnit.SECONDS); executor.shutdown(); } } class Task implements Callable { public String call() throws Exception { try { System.out.println(“Started..”); Thread.sleep(4000); // Just to demo a long running task of […]

无法在Eclipse Indigo中启动Tomcat 7.0

我将开始研究我安装了Eclipse Indigo和Tomcat 7.0的Web应用程序项目。 环境中还有JRE 7,Android SDK。 但每当我启动服务器时,它都会超时! 此外,我能够在Eclipse之外启动服务器并在浏览器中成功执行Web应用程序。 但是为了调试,我宁愿在Eclipse中使用它。 错误消息是: Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor. 在控制台中,我得到: Mar 14, 2012 11:51:18 PM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows […]

Basicdatasource连接超时问题(使用mysql)

我在我的应用程序中使用BasicDatasource。 此应用程序正在处理大量原始数据。 有时1次查询可能需要15分钟以上。 (使用mysql作为db) 这是我的问题,我从池中获取连接,然后对其执行多个查询。 但是当我使用相同的连接超过15分钟时,我得到以下错误。 在mysql服务器中,max_wait设置为180小时,因此保持连接活动并且没有防火墙规则设置为终止超过一定时间的活动连接不应该是一个问题。 你觉得我在这里想念的是什么? The last packet successfully received from the server was 928,374 milliseconds ago. The last packet sent successfully to the server was 928,374 milliseconds ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113) […]

Hashtable的超时机制

我有一个哈希表,在流量很大的情况下。 我想为哈希表添加超时机制,删除太旧的记录。 我担心的是, – 它应该是轻量级的 – 删除操作没有时间关键。 我的意思是(超时值是1小时)删除操作可以在1小时或1小时15分钟后。 没有问题。 我的意见是,我创建了一个大数组(作为环形缓冲区),存储时间和哈希表键,当添加到哈希表时,使用数组索引查找数组上的下一个插槽时间,如果数组插槽为空,则插入时间和HT键,如果数组槽不为空,则比较发生超时的插入时间。 如果超时发生从Hashtable中删除(如果尚未删除)则不会发生超时,增加索引直到找到空槽或时间数组槽。 从哈希表中删除时,大数组上没有操作。 不久,对于Hashtable的每个添加操作,可以从哈希表中删除1个timeouted元素或不执行任何操作。 您的优雅和轻量级解决方案是什么? 谢谢你的帮助,

Java中的resettable超时

(类似于“Resettable Java Timer”,但我需要探索一些细微之处) 我需要一个可重置的超时function,这样如果我的类在时间间隔T0内没有执行特定动作(其中T0在50-1000毫秒附近),则会调用一个方法: class MyClass { static final private timeoutTime = 50; final private SomeTimer timer = new SomeTimer(timeoutTime, new Runnable () { public void run() { onTimeout(); }}); private void onTimeout() { /* do something on timeout */ } public void criticalMethod() { this.timer.reset(); } } 我可以用什么来实现这个? 我熟悉ScheduledExecutorService ,并且调用ScheduledFuture.cancel()然后重新安排任务的想法似乎应该可以工作,但是如果cancel()失败并且计划任务在不应该执行时执行则存在潜在危险。 我觉得我在这里错过了一个微妙的东西。 另外(也许更重要的是),有没有办法测试我的实现/certificate它正常工作? 编辑:我特别关注的是经常调用criticalMethod()的情况(可能每毫秒几次)…如果我使用ScheduledExecutorService,那么继续创建新的计划任务+取消旧任务似乎是一个潜在的资源问题,而不是直接重新安排任务。