首页 \ 问答 \ 为什么这个FutureTask会导致我的程序永远挂起?(Why does this FutureTask cause my program to hang forever?)

为什么这个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
更新时间:2024-02-10 10:02

最满意答案

在解决方案属性中,在“配置属性”下,确保将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 ...
  • 请看这里: 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 ++项目设置为构建。 另外,确保项目依赖关系正确设置。 这两件事控制了在运行构建时构建哪些项目。 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 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 ...
  • 通常我会用三件事之一看到这一点 机器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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。