首页
\
问答
\
ExecutorService workStealingPool和cancel方法(ExecutorService workStealingPool and cancel method)
ExecutorService workStealingPool和cancel方法(ExecutorService workStealingPool and cancel method)
你能想一想这个代码不起作用的任何原因并且总是输出“完成”,但第二个例子没有任何问题。 我正在使用最新的JDK(8u45)。
public static class MyClass implements Runnable { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException ex) { System.out.println("Interrupted"); return; } System.out.println("Finished"); } public static void main(String[] args) { // spot the difference -> ExecutorService executorService = Executors.newWorkStealingPool(4); Future future = executorService.submit(new MyClass()); Thread.sleep(100); future.cancel(true); } }以下示例完美无缺:
public static class MyClass implements Runnable { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException ex) { System.out.println("Interrupted"); return; } System.out.println("Finished"); } public static void main(String[] args) { ExecutorService executorService = Executors.newSingleThreadExecutor(); Future future = executorService.submit(new MyClass()); Thread.sleep(100); future.cancel(true); } }编辑:添加了返回和更新的睡眠时间和另一个例子。
Can you think about any reason why this code doesn't work and always outputs "finished", but the second example works without any problems. I'm using latest JDK (8u45).
public static class MyClass implements Runnable { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException ex) { System.out.println("Interrupted"); return; } System.out.println("Finished"); } public static void main(String[] args) { // spot the difference -> ExecutorService executorService = Executors.newWorkStealingPool(4); Future future = executorService.submit(new MyClass()); Thread.sleep(100); future.cancel(true); } }And the following example works flawlessly:
public static class MyClass implements Runnable { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException ex) { System.out.println("Interrupted"); return; } System.out.println("Finished"); } public static void main(String[] args) { ExecutorService executorService = Executors.newSingleThreadExecutor(); Future future = executorService.submit(new MyClass()); Thread.sleep(100); future.cancel(true); } }EDIT: Added return and updated sleep times and another example.
原文:https://stackoverflow.com/questions/29903804
更新时间:2023-07-12 09:07
最满意答案
我对currentDevice.orientation遇到了很多问题试试看:
UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
I have had a ton of problems with currentDevice.orientation Try this:
UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
相关问答
更多-
在这里添加和删除“子”视图控制器视图的主要问题是视图控制器生命周期方法( viewWillAppear: , viewDidAppear:等)将永远不会在子视图控制器上调用。 像UINavigationController和UITabBarController这样的容器一直都知道如何将这些方法适当地委托给它们的子UIViewController ,但UIViewController并没有正式支持在iOS 5之前将视图控制器嵌套在你自己的自定义容器下的能力。这是可能的,但它花了很多时间做得更好,做得对。 如果 ...
-
对于iOS 7 - 10: Objective-C的: [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"]; Swift 3: let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") 只需在- ...
-
您也可以使用: [self.navigationController popToRootViewControllerAnimated:YES]; You can also use the: [self.navigationController popToRootViewControllerAnimated:YES];
-
为什么要对两个方向使用两个视图? 您可以设置控件的autoresizemask属性,如果纵向和横向两种模式具有相似的控件,则可以在同一位置显示。 否则,您需要使用两个视图 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; - (void)didRotateFromInterfaceOrientation:(UIIn ...
-
我想你希望else块中的代码只能在横向模式下运行3.5英寸设备吗? 在这种情况下,您需要在第二个之前添加else,否则else语句之后的第三个代码块将始终运行,如果我理解正确,它将创建一个横向布局,无论您传入的方向如何。 I think you want the code in the else block to run only for 3.5 inches devices in landscape mode right? In that case you need to add an else befo ...
-
对于我对currentDevice.orientation遇到了很多问题试试看: UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) I have had a ton of problems with currentDevice.orientation Try this: UIDeviceOrientationIsPortrait([[UIApplication sharedApplic ...您需要创建一个容器视图控制器。 虽然iOS 5明确支持容器控制器,但您可以在以前的版本中创建容器控制器。 所有iOS 5都会做一些旋转/外观事件的自动转发(可选......我个人觉得它们很烦人,在我准备好之前发送事件)并给你一些额外的方法来实现。 创建容器视图控制器的真正问题是将所有相应的事件发送到子控制器,并确保以与Apple实现一致的方式管理控制器。 否则,您的子控制器会出现奇怪的行为。 在执行此操作之前,您确实需要确保完全了解视图控制器的完整工作方式。 我建议阅读以下内容: 以下是一些信息链接: ht ...我终于解决了这个问题,我想有其他选择,但这个工作正常: 事实上,我只保留了纵向限制。 然后当我向右或向左转动手机时,我会以模态方式调用我的ARViewController,但在加载之前,我强制将此视图控制器设置为横向(在viewWillAppear中),方法如下: - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self transformView2ToLandscape];} -(void) transf ...您可以尝试使用此方法(在app store构建中工作): [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)]; You can try to use this method (working in app store build): ...
相关文章
更多- 研磨设计模式之工厂方法模式(Factory Method)-模式讲解1
- 研磨设计模式之工厂方法模式(Factory Method)-模式讲解2
- 研磨设计模式之工厂方法模式(Factory Method)-场景问题
- 研磨设计模式之工厂方法模式(Factory Method)-模式讲解3
- 研磨设计模式之工厂方法模式(Factory Method)-解决方案
- Java 方法
- getMethod方法调用疑惑
- 关于Thread类中的start()方法和run()方法
- 利用MyEclipse快速实现方法重构
- 报错说找不到abbrev这个方法,但Array有这个方法的吧?
最新问答
更多- 获取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的基本操作命令。。。