为什么这个FutureTask会导致我的程序永远挂起?(Why does this FutureTask cause my program to hang forever?)
我正在学习Java并试图了解FutureTasks是如何工作的。 在我的特定情况下,我正在尝试生成一个线程(FutureTask)来执行某些操作。 我其实并不关心它的回应; 我只是希望它异步执行。 我有很多与以下测试代码一起工作:
public class Main { public static void main(String[] args) { ExecutorService exService = Executors.newSingleThreadInstance(); FutureTask<Boolean> asynch = new FutureTask<>(new Callable<Boolean>() { @Override public Boolean call() { try { // wait for 5 seconds to simulate activity Thread.sleep(5000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("Complete"); return true; } }); System.out.println("Started"); exService.execute(asynch); } }
当我运行这个代码时,它将“Started”打印到控制台,然后五秒钟后打印出“Complete”,就像我期望的那样。 然而......该计划在这一点上无限期地悬挂下来。 我假设,因为call()方法已经完成,程序足够聪明,可以在那个时候退出,但显然它会让这个线程永远活着? 我如何让它一次运行任务,然后退出?
I'm learning Java and trying to understand how FutureTasks work. In my particular case I'm trying to spawn a thread (FutureTask) to execute something. I don't actually care about its response; I just want it to execute asynchronously. I've got that much working with the following test code:
public class Main { public static void main(String[] args) { ExecutorService exService = Executors.newSingleThreadInstance(); FutureTask<Boolean> asynch = new FutureTask<>(new Callable<Boolean>() { @Override public Boolean call() { try { // wait for 5 seconds to simulate activity Thread.sleep(5000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("Complete"); return true; } }); System.out.println("Started"); exService.execute(asynch); } }
When I run this code, it prints "Started" to the console, then five seconds later prints "Complete" like I would expect. However... the program just hangs indefinitely at that point. I was assuming that because the call() method had completed that the program would be smart enough to quit at that point, but apparently it keeps that thread alive forever? How do I get it to run the task once and then quit?
原文:https://stackoverflow.com/questions/27181196
最满意答案
在解决方案属性中,在“配置属性”下,确保将C ++项目设置为构建。 另外,确保项目依赖关系正确设置。
这两件事控制了在运行构建时构建哪些项目。
In the solution properties, under "Configuration Properties", make sure the C++ projects are set to build. Also, make sure the project dependencies are setup properly.
These two things control which projects are built when you run a build.
相关问答
更多-
当你说“不能再运行它”时会发生什么? sln文件不兼容; 每个VS版本都需要不同的sln文件 csproj 大多是兼容的,尽管你可能会看到“版本4不被识别,使用3.5而不是”警告或两个,通常是罚款 只要您不使用dynamic或其他新语言功能,cs就是兼容的 在大多数情况下,您只需为VS2010提供一个单独的sln即可脱身。 因此,只需将其重命名为“Whatever_2010.sln”,从源代码库中取回旧的“Whatever.sln”,并将其重命名为“Whatever_2008.sln”。 When you ...
-
一些想法尝试... 正如@David Paxson在评论中所说的,在一些语言(例如C#,VB)中,所有项目似乎都是“内置的”,但编译器会跳过那些不需要更快建立的项目,甚至尽管它们仍然在输出中列出。 所以它可能只是重建所有东西。 此外,使用C#,Visual Studio有时会连续多次运行项目,有时候它会决定没有明显的原因需要“重建”项目,但这通常只需要几秒钟,除非您有数百个项目。 重新启动Visual Studio(甚至完全重启PC)。 做一个“重建所有”的整个解决方案,以清除任何旧的缓存信息,并确保所有的 ...
-
我们倾向于使每个组件成为一个解决方案,包含一个或多个项目(或子组件)和一个测试项目。 测试项目包含所有单元测试。 然后,我们根据模块和组件将解决方案安排到树中,例如: //depot/MyProject/ASubSystem/AComponentOfTheSubSystem/ASubComponentWithAVSSolution 该解决方案将包含几个Visual Studio项目: //depot/MyProject/ASubSystem/AComponentOfTheSubSystem/ASubCom ...
-
Web部署项目失败的构建(Visual Studio 2008)(Web Deployment Project failing build (Visual Studio 2008))[2022-04-05]
请看这里: http : //social.msdn.microsoft.com/Forums/en/tfsbuild/thread/9c14fd1c-4c6c-4a5b-ac2a-05a2972036e9 如果这不起作用,请在Google上搜索“MSB3073”您帖子中的特定错误代码。 祝你好运! Found out what the problem was. The user profile folder had an @ sign in it, which looks like it may brea ... -
确保libeay32.lib和ssleay32.lib位于链接器配置中的单独行上。 错误消息无法打开输入文件'libeay32.lib; ssleay32.lib'显示它正在查找具有此奇怪名称的单个文件。 Make sure libeay32.lib and ssleay32.lib are on the separate lines in the linker config. The error message cannot open input file 'libeay32.lib;ssleay32.l ...
-
该链接描述了如何将VS测试系统与F#一起使用。 你的工作方式与C#非常相似。 不足之处在于VS IDE显然不会自动提取 - 我相信你需要使用mstest.exe从命令行运行。 编辑:哦,F#的另一件很酷的事情是FsCheck 。 这允许您编写简单的测试,并让FsCheck系统尝试通过随机测试来反驳它们。 This link describes using the VS testing system with F#. You do it pretty much the same way as with C#. ...
-
C ++代码不在Visual Studio 2008中使用构建项目(F6)构建(C++ Code not building with build project (F6) in Visual Studio 2008)[2023-08-29]
在解决方案属性中,在“配置属性”下,确保将C ++项目设置为构建。 另外,确保项目依赖关系正确设置。 这两件事控制了在运行构建时构建哪些项目。 In the solution properties, under "Configuration Properties", make sure the C++ projects are set to build. Also, make sure the project dependencies are setup properly. These two things ... -
使用Windows 7 SDK构建Visual Studio 2008安装项目(Building Visual Studio 2008 Setup projects with the Windows 7 SDK)[2024-05-06]
看起来这些文件并不是Windows 6 SDK的一部分,而是Visual Studio 2008安装的一部分。 当我删除Windows 6 SDK时,我也删除了这个文件夹,从而导致上面的问题。 Seems that the files are not really part of the Windows 6 SDK, but part of the Visual Studio 2008 installation. When I removed the Windows 6 SDK I also deleted ... -
您要做的是将C#项目添加到现有解决方案中 。 在解决方案资源管理器中,右键单击解决方案并选择“添加”>“现有项目”。 选择C#项目。 现在,您的解决方案中将有两个项目(一个C#和一个C ++)。 构建解决方案时,都会构建两个项目。 更改的文件是* .sln。 What you want to do is to add the C# project to an existing solution. In solution explorer, right-click on the solution and ch ...
-
您是否有Visual Studio 2008和C#的引用问题?(Do you have references issues with Visual Studio 2008 and C#?)[2022-06-10]
通常我会用三件事之一看到这一点 机器A上的包装,而不是机器B上的包装 机器A上的GAC(全局程序集缓存)中的内容,而不是机器B中的内容 未检入源代码管理的包 Visual Studio通常很好地处理关系路径,我没有把它作为一个路径问题。 还要确保您没有签入.SUO文件和.csproj.user。 这些都有时会带参考。 Typically I see this with one of three things Packages on Machine A, not on Machine B Things in ...