ForkJoinPool重置线程中断状态(ForkJoinPool resets thread interrupted state)
当我取消ForkJoinPool返回的Future时,我注意到以下现象。 给出以下示例代码:
ForkJoinPool pool = new ForkJoinPool(); Future<?> fut = pool.submit(new Callable<Void>() { @Override public Void call() throws Exception { while (true) { if (Thread.currentThread().isInterrupted()) { // <-- never true System.out.println("interrupted"); throw new InterruptedException(); } } } }); Thread.sleep(1000); System.out.println("cancel"); fut.cancel(true);
该程序永远不会打印
interrupted
。 ForkJoinTask#cancel(boolean)的文档说:mayInterruptIfRunning - 此值在默认实现中无效,因为中断不用于控制取消。
如果ForkJoinTasks忽略了中断,你应该如何在提交给ForkJoinPool的Callables中检查取消?
I just noticed the following phenomena when cancelling a Future returned by ForkJoinPool. Given the following example code:
ForkJoinPool pool = new ForkJoinPool(); Future<?> fut = pool.submit(new Callable<Void>() { @Override public Void call() throws Exception { while (true) { if (Thread.currentThread().isInterrupted()) { // <-- never true System.out.println("interrupted"); throw new InterruptedException(); } } } }); Thread.sleep(1000); System.out.println("cancel"); fut.cancel(true);
The program never prints
interrupted
. The docs of ForkJoinTask#cancel(boolean) say:mayInterruptIfRunning - this value has no effect in the default implementation because interrupts are not used to control cancellation.
If ForkJoinTasks ignore interrupts, how else are you supposed to check for cancellation inside Callables submitted to a ForkJoinPool?
原文:https://stackoverflow.com/questions/21320156
最满意答案
使用
FileInfo
获取输入文件的大小,并且只有在大于0的情况下才运行该进程:FileInfo fi = new FileInfo("input.txt"); if(fi.Length > 0) { Process.Start("cmd.exe", @"/c test.exe -f input.txt > output.txt").WaitForExit(); }
Use
FileInfo
to get the size of the input file and only run the process if it is larger than 0:FileInfo fi = new FileInfo("input.txt"); if(fi.Length > 0) { Process.Start("cmd.exe", @"/c test.exe -f input.txt > output.txt").WaitForExit(); }
相关问答
更多-
应用领域和跨域互动是一件非常薄的事情,所以应该确保他真正了解事情在做任何事情之前的工作...嗯...让我们说,“非标准”:-) 首先,您的流创建方法实际上在您的“默认”域执行(惊喜!)。 为什么? 简单:您传入AppDomain.DoCallBack的方法是在AppDomainDelegateWrapper对象上定义的,该对象存在于您的默认域上,因此它的方法将被执行。 MSDN不会说这个小小的“功能”,但是很容易检查:只需在AppDomainDelegateWrapper.Invoke设置一个断点。 所以, ...
-
即使文件存在,Process.start也会导致找不到文件(Process.start results in file not found even though the file exists)[2022-05-08]
你在文件路径中有空格吗? File.Exists可以很好地处理空间,可能与Windows资源管理器一样,但Process.Start可能无法处理。 我想这就是你想要的萌芽。 使用ProcessStartInfo对象的进程开始,文件路径将空格分别作为字符串传递。 在路径中使用带参数和空格的Process.Start Do you have spaces in the file path? File.Exists can handle spaces fine, presumably as can Windows ... -
如何使用C#以Process.Start()全屏打开视频文件?(How to open video file in full screen with Process.Start() using C#?)[2024-02-07]
您的代码将启动用户与视频扩展相关联的任何视频播放器。 所以,全屏打开它可能是一个不同的参数,具体取决于所使用的播放器。 Your code will launch whatever video player the user has associated with the video extension. So, open it in full screen would be probably a different parameter depending on the player used. -
我想我自己也得到了答案。 :) Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = MyExe; startInfo.Arguments = ArgumentsForMyExe; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); // This ...
-
Process.Start抛出“系统找不到指定的文件”(Process.Start throws “The system cannot find the file specified”)[2022-07-07]
尝试 Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/"); try Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/"); -
我想你只是偶然发现了http://msdn.microsoft.com/en-us/library/aa384187.aspx %windir%\ System32目录保留用于64位应用程序。 创建64位版本的DLL时,大多数DLL文件名未被更改,因此32位版本的DLL存储在>不同的目录中。 WOW64通过使用文件系统重定向器隐藏了这种差异。 发生的情况是您的启动请求(来自32位进程)被重定向到%windir%\SysWOW64\sysprep\sysprep.exe 。 由于SysWOW64上没有此特定可 ...
-
使用FileInfo获取输入文件的大小,并且只有在大于0的情况下才运行该进程: FileInfo fi = new FileInfo("input.txt"); if(fi.Length > 0) { Process.Start("cmd.exe", @"/c test.exe -f input.txt > output.txt").WaitForExit(); } Use FileInfo to get the size of the input file and only run the proc ...
-
我想你的exe文件通过输入参数找到你的文本文件。 但是文本文件的完整路径包含空格,这会将args参数分解为多个元素。 您的程序引用的第一个参数是不完整的路径。 args[0] = "C:\users\asim" ..... 对于txt文件的路径名中存在的每个空间,依此类推。 一个简单的解决方案是指定WorkingDirectory并从参数中删除完整路径 Dim a As New ProcessStartInfo a.WorkingDirectory = "C:\Users\Asim Rahman\Des ...
-
您还可以禁用64位操作系统的文件夹重定向 [System.Runtime.InteropServices.DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")] public static extern bool EnableWow64FSRedirection(bool enable); EnableWow64FSRedirection(false); try { ...
-
当命令等待用户输入时,Process.Start()挂起服务(Process.Start() Hangs Service When Command Waits for User Input)[2023-01-15]
一种方法是添加/Q如果没有找到: process.StartInfo.Arguments = arguments.AddQuietSwitch(); 扩展方法: private static Dictionary_quietSwitchMap = new Dictionary { { "rmdir", "/Q" }, { "xcopy", "/y" } }; public static string AddQuietSwitch( ...