java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask$Sync.innerGet

此报告详细记录了一例在使用NetBeans部署应用到GlassFish服务器时出现的java.util.concurrent.TimeoutException异常。该问题主要出现在尝试部署sun-resources.xml文件时,服务器仍处于停止状态的情况下。经过多次尝试,发现该问题已在后续版本中作为副作用被解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Bug 142996 -  [65cat]java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask$Sync.innerGet
Status: RESOLVED FIXED
 
Product: serverplugins
Component: GlassFish
6.x
PC Windows XP
 
: P2 (vote)
: 6.x
Assigned To: Vince Kraemer
issues@serverplugins
 
http://statistics.netbeans.org/except...
 
: RANDOM
 
: 146390 151428 155285 (view as bug list)
Depends on:
Blocks:
  Show dependency tree / graph
 
Reported: 2008-08-06 05:40 UTC by rajivderas
Modified: 2009-04-17 19:57 UTC (History)
7 users (show)
 
See Also:  
Issue Type: DEFECT
:
 


Attachments

NoteYou need to  log in before you can comment on or make changes to this bug.
 
Description rajivderas  2008-08-06 05:40:23 UTC
java.util.concurrent.TimeoutException
        at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228)
        at java.util.concurrent.FutureTask.get(FutureTask.java:91)
        at org.netbeans.modules.glassfish.javaee.db.Hk2DatasourceManager.registerResourceDir(Hk2DatasourceManager.java:152)
        at org.netbeans.modules.glassfish.javaee.db.Hk2DatasourceManager.deployDatasources(Hk2DatasourceManager.java:140)
        at org.netbeans.modules.j2ee.deployment.impl.ServerInstance.deployDatasources(ServerInstance.java:666)
        at
org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider.deployDatasources(J2eeModuleProvider.java:251)
        at org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment.deploy(Deployment.java:148)
        at org.netbeans.modules.j2ee.ant.Deploy.execute(Deploy.java:104)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
        at sun.reflect.GeneratedMethodAccessor63.invoke(GeneratedMethodAccessor63.java:0)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:357)
        at org.apache.tools.ant.Target.performTasks(Target.java:385)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
        at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:277)
        at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:460)
        at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:151)
Comment 1 _ pcw  2008-08-08 06:31:36 UTC
Odd.  Can you provide any more information about the circumstances in which you received this exception?
Comment 2 _ pcw  2008-09-04 02:49:25 UTC
To reproduce: 

Create a web project and add some entity classes from database (so that a sun-resources.xml file is created).

Then make sure the server is STOPPED and "Run" the project.  When checking to make sure everything is deployed and
ready, the server is deploying the sun-resources.xml file when the server is still stopped (or something along those
lines) and this exception occurs.

Essentially auto deployment of sun-resources.xml is not correctly implemented.

Workaround: Start the server.  Once it's started, then choose "Run".
Comment 3 rajivderas  2008-09-04 05:35:57 UTC
pcw, cannot perform your process since am getting issues [1]#14611 when am trying to create a persistence unit in New 
Entity Class wizard in Dev Build 200809031401.

[1] http://www.netbeans.org/issues/show_bug.cgi?id=146111
Comment 4 _ pcw  2008-09-04 05:43:25 UTC
Well, Vince has already fixed 14611, but don't worry about reproducing it.  I already did.  I just wrote up the steps
required for posterity.
Comment 5 rajivderas  2008-09-04 05:46:26 UTC
ok cool
Comment 6 _ pcw  2008-09-06 04:19:12 UTC
*** Issue 151428 has been marked as a duplicate of this issue. ***
Comment 8 _ pcw  2008-10-27 20:00:21 UTC
This issue as 50+ duplicates (see Issue 155285 has been marked as a duplicate of this issue. ***
Comment 11 pgebauer  2009-01-14 09:47:08 UTC
The issue hasn't passed the nomination process for 65patch2 by cut-off date. It has been moved to 65patch3.
Comment 12 Marian Mirilovic  2009-02-19 11:19:42 UTC
75 duplicates so far ...
Comment 13 Vince Kraemer  2009-02-19 20:21:38 UTC
I have just been trying to reproduce this issue with a recent pull of main.  I have not been able to reproduce the problem. 

Has anyone run into this with a recent nightly build?

It is possible that one of the changes introduced since 6.5 shipped could have corrected this problem as a
side-effect... but I don't know which... so including this as part of patch 3 may not be possible.
Comment 14 Vince Kraemer  2009-02-21 17:06:37 UTC
folks have not been able to reproduce with recent trunk builds.

Closing as worksforme.

If someone does reproduce it with the trunk... please reopen and provide details.

I don't know which changes resolved this...

Comment 15 _ pcw  2009-02-23 21:41:35 UTC
This is a 6.5 patch 3 candidate.  Whether it's reproducible on the trunk or not is irrelevant as long as it's
reproducible in 6.5 patch 2 or earlier.  Otherwise what you are saying is that we do not consider this a patch candidate
and won't fix it in 6.5 which personally, I disagree with unless the fix is really risky (in this case, it might be but
I don't know that we know that).
Comment 16 Vince Kraemer  2009-02-25 20:46:10 UTC
OK.

I reset the 65fixes3-candidate flag...

The issue appears to be resolved in the trunk... which is why it was closed as WFM.

I do not know which code change resolved this issue (I am assuming that it has been resolved since multiple folks have
attempted to reproduce it and failed).

I guess you will need to find a build where this issue can be reproduced and then determine the code change that fixes
it.  After that happens, you will need to verify that the essence of the fix is not already in-place in the
release65_fixes clone (and in the trunk).

If the essence of the fix is in the release65_fixes clone, you should probably add a note to that effect here.

If the essence of the fix is in the trunk, I am not sure what to do... I would guess apply any improvements of your
patch over the changes that have resolved the issue in the trunk and then leave a pointer to both sets of changes (the
essence and the clean-up)...

I will leave that up to you.

Note: if this issue appears to be an impediment to a 6.7 milestone, I will close it as WFM again.... but that won't be
for a couple week... at least.
Comment 17 Marian Mirilovic  2009-03-02 10:51:25 UTC
looks like it's still not clear about the fix ... moving to next patch
Comment 18 _ pcw  2009-03-16 21:02:17 UTC
I was unable to reproduce this in 6.5 patch 2, though the steps mentioned below will work to reproduce this in the
original FCS release of 6.5.  It looks like the callflow in j2eeserver was altered in a subsequent patch and as a side
effect, this issue was resolved.

Vince, Nitya, and Rochelle have posted that they cannot reproduced this in 6.7 codeline (probably due to the same effect
mentioned above, closing as WORKFORME.
Comment 19 pgebauer  2009-04-08 14:00:42 UTC
The status whiteboard "65fixes4-candidate" has been removed.
At this time our proactive patches for the NetBeans 6.5.x IDE have concluded.

If you own a Sun service plan contract for NetBeans, you may wish to contact
Sun Service http://www.sun.com/contact/support.jsp to request a fix via the
product defect escalation process.

For more information on purchasing a Sun service plan contract for NetBeans,
refer to the service plan item "Sun Software Service Plans (S3P) for Developers"
in the Sun Service table found on our NetBeans Support Resources
page http://www.netbeans.org/kb/support.html
Comment 20 Vince Kraemer  2009-04-08 17:04:34 UTC
I have seen new reports of this from the trunk build and 6.5.1... so it isn't fixed.
Comment 21 Vince Kraemer  2009-04-14 23:16:47 UTC
Multiple developers have tried to reproduce this and been unsuccessful.  Users are still running into this with recent
builds. Marking as random.  doing more investigation.
Comment 22 Vince Kraemer  2009-04-17 17:07:10 UTC
http://hg.netbeans.org/web-main/rev/a5f251358ed6
Comment 23 Quality Engineering  2009-04-17 19:57:10 UTC
Integrated into 'main-golden', will be available in build *200904171401* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/a5f251358ed6
User: Vince Kraemer <vkraemer@netbeans.org>
Log: #142996: bogus use of Level.WARNING when the timeout hits...
 


package ajiayou.index0711.demo03; import java.util.Random; import java.util.concurrent.Callable; public class Match2 implements Callable<Integer> { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public Integer call() throws Exception { Random r = new Random(); //这里是随机数 int speed = r.nextInt(100); int dist = 0; for(int i = 1; i<=100; i++) { //睡眠100毫秒 Thread.sleep(30); //Thread.currentThread().getName() //获得当前线程的名称 dist += i * speed; System.out.println(this.getName() + "已前进" + (dist) + "米(" + speed + "米/秒)"); } return dist; } } /* 注释补充:创建一个线程 Java 提供了三种创建线程的方法: 1.通过继承 Thread 类本身; 2.通过实现 Runnable 接口; 3.通过 Callable 和 Future 创建线程。 创建 Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值。 创建 Callable 实现类的实例,使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值。 使用 FutureTask 对象作为 Thread 对象的 target 创建并启动新线程。 调用 FutureTask 对象的 get() 方法来获得子线程执行结束后的返回值。 */ package ajiayou.index0711.demo03; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Test2 { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService es = Executors.newFixedThreadPool(3); //创建了3个线程 Match2 m = new Match2(); m.setName("java"); Match2 m1 = new Match2(); m1.setName("mysql"); Match2 m2 = new Match2(); m2.setName("boot"); //开始对上面的线程进行执行 Future<Integer> future = es.submit(m); Future<Integer> future1 = es.submit(m1); Future<Integer> future2 = es.submit(m2); Integer i = future.get(); Integer i1 = future1.get(); Integer i2 = future2.get(); System.out.println(m.getName() + "共跑了" + i + "米"); System.out.println(m1.getName() + "共跑了" + i1 + "米"); System.out.println(m2.getName() + "共跑了" + i2 + "米"); } }
最新发布
07-15
<think>我们正在回答用户的问题:如何使用Callable和Future来创建并管理线程,并获取线程执行后的返回值。 根据引用[1][2][3]可知,Callable和Future是在Java 1.5版本开始引入的,用于解决Runnable无法返回结果的问题。 实现步骤: 1. 创建Callable接口的实现类,并实现call()方法,在call()方法中编写线程执行逻辑并返回结果。 2. 使用FutureTask类来包装Callable对象,FutureTask实现了RunnableFuture接口,而RunnableFuture又继承了Runnable和Future接口。 3.FutureTask对象作为Thread的target创建线程并启动。 4. 调用FutureTask对象的get()方法获取线程执行后的返回值(该方法会阻塞直到任务完成)。 下面我们通过一个完整的示例代码来展示如何使用Callable和FutureTask获取线程返回值。</think>### Java 多线程中使用 Callable 和 Future 获取返回值的实现方法 在 Java 多线程编程中,`Callable` 和 `Future` 提供了比 `Runnable` 更强大的线程管理能力,主要解决了 **线程返回值获取** 和 **异常处理** 的问题。以下是完整实现方法: #### 1. 核心组件说明 | 组件 | 作用 | 特点 | |------|------|------| | `Callable<V>` | 替代 Runnable 的任务接口 | 可返回结果($V$类型),可抛出异常[^3] | | `Future<V>` | 异步计算结果容器 | 提供结果获取、任务取消、状态检查等方法[^1] | | `FutureTask` | Future 的基础实现类 | 可包装 Callable 对象,同时实现 Runnable 接口[^2] | #### 2. 实现步骤 ```java import java.util.concurrent.*; public class CallableDemo { public static void main(String[] args) throws Exception { // 步骤1:创建Callable任务 Callable<Integer> task = () -> { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; Thread.sleep(10); // 模拟耗时操作 } return sum; // 返回计算结果[^1] }; // 步骤2:包装为FutureTask FutureTask<Integer> futureTask = new FutureTask<>(task); // 步骤3:启动线程执行任务 new Thread(futureTask).start(); // 步骤4:获取异步计算结果(阻塞等待) int result = futureTask.get(); System.out.println("计算结果: " + result); // 输出: 5050 } } ``` #### 3. 关键操作详解 1. **创建 Callable 任务** ```java Callable<String> task = () -> { // 业务逻辑... return "执行结果"; }; ``` 2. **获取结果(阻塞方式)** ```java String result = future.get(); // 阻塞直到任务完成[^2] ``` 3. **超时控制(避免永久阻塞)** ```java try { result = future.get(5, TimeUnit.SECONDS); // 最多等待5秒 } catch (TimeoutException e) { future.cancel(true); // 取消任务[^4] } ``` 4. **状态检查** ```java if (!future.isDone()) { System.out.println("任务仍在运行"); } ``` #### 4. 线程池集成(推荐方式) ```java ExecutorService executor = Executors.newFixedThreadPool(3); // 提交多个任务 Future<Integer> future1 = executor.submit(() -> 100); Future<Integer> future2 = executor.submit(() -> 200); // 批量获取结果 int total = future1.get() + future2.get(); // 300 executor.shutdown(); // 关闭线程池 ``` #### 5. 异常处理机制 ```java Callable<Integer> task = () -> { if (errorCondition) { throw new RuntimeException("自定义异常"); } return 42; }; try { future.get(); } catch (ExecutionException e) { Throwable cause = e.getCause(); // 获取真实异常[^3] System.out.println("任务异常: " + cause.getMessage()); } ``` #### 6. 适用场景对比 | 场景 | Runnable | Callable+Future | |------|----------|-----------------| | 需要返回值 | ❌ 不支持 | ✅ 支持 | | 异常传播 | ❌ 受限 | ✅ 完整异常栈 | | 超时控制 | ❌ 困难 | ✅ 内置支持 | | 任务取消 | ❌ 有限 | ✅ 精细控制 | > 通过 `Callable` 和 `Future` 的组合,Java 多线程实现了 **任务执行** 与 **结果获取** 的解耦,使异步计算更加灵活可靠[^1][^3]。实际开发中建议配合线程池使用,避免频繁创建线程的开销[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值