首页 \ 问答 \ 类型(变量)vs(类型)变量[重复](Type(variable) vs (Type)variable [duplicate])

类型(变量)vs(类型)变量[重复](Type(variable) vs (Type)variable [duplicate])

可能重复:
(类型)值和类型(值)之间有什么区别?

我主要是一个C#开发人员,因此使用类似下面的语法进行了大量显式强制转换: (type)variable ,以(int)100.0004d为例。 因此,当用C ++编写代码时,我经常使用相同的语法。 但是,在其他情况下,我已经看到(甚至使用过)代码,其中使用以int(100.0004)为例的语法type(variable)来实现相同的int(100.0004)

我只是好奇两种方法之间的区别是什么,以及在使用这两种方法时是否有任何影响。

例:

double someDouble = 100.00456;

// Cast the double using the (type)variable syntax
int firstCastValue = (int)someDouble;

// Cast the double using the type(variable) syntax
int secondCastValue = int(someDouble);

Possible Duplicate:
What is the difference between (type)value and type(value)?

I am mainly a C# developer and so do a lot of explicit casting using syntax like: (type)variable, with (int)100.0004d as an example. As such, when writing code in C++, I often use the same syntax. However, I have seen (and even used) code in other cases where the same cast is achieved using the syntax type(variable) with int(100.0004) as an example.

I was just curious as to what the difference between the two methods were and whether there were any implications in using one over the other.

Example:

double someDouble = 100.00456;

// Cast the double using the (type)variable syntax
int firstCastValue = (int)someDouble;

// Cast the double using the type(variable) syntax
int secondCastValue = int(someDouble);

原文:https://stackoverflow.com/questions/8881178
更新时间:2023-02-21 19:02

最满意答案

Executors#newSingleThreadScheduledExecutor()返回的ScheduledExecutorService线程池使用非守护线程。 在关闭线程池之前,这些仍然在等待任务。 当非守护线程处于活动状态时,JVM不会结束。

您可以使用重载的Executors#newSingleThreadScheduledExecutor(ThreadFactory)并提供您自己的创建守护程序线程的ThreadFactory实现。 请注意,这可能会导致您的任务甚至可能无法运行,因为JVM会在任务计划的时间之前退出。

按照您发现并关闭的方式进行操作。 请注意,您应该将其始终关闭在安全的地方,在那里操作不会失败。


The ScheduledExecutorService thread pool returned by Executors#newSingleThreadScheduledExecutor() uses non daemon threads. Until you shut down the thread pool, these are still alive awaiting tasks. A JVM does not end while non-daemon threads are alive.

You can use the overloaded Executors#newSingleThreadScheduledExecutor(ThreadFactory) and provide your own ThreadFactory implementation which creates daemon threads. Note that this risks the case where your task may not even run because the JVM would exit before the task's scheduled time.

Do as you've discovered and shut it down. Note that you should shut always it down somewhere safe, where the operation can't fail.

相关问答

更多
  • 最简单的解决方案就是将CSDelightAlertTask包装在只运行底层任务的包装器中,当且仅当时间在0900和1800之间时。 The simplest solution would just be to wrap CSDelightAlertTask in a wrapper that only runs the underlying task if and only if the time is between 0900 and 1800.
  • 您遇到的问题是调度程序在取消嘟嘟声任务后保持活动线程。 如果存在活动的非守护线程,则JVM保持活动状态。 它保持这个线程的原因是你已经告诉它在这一行中这样做: private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 请注意newScheduledThreadPool(int corePoolSize)的文档: corePoolSize - 保留在池中的线程数,即使它们处于 ...
  • http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html 有一个scheduleAtFixedRate方法。 要将某些内容传递给匿名类,需要将其声明为final。 它需要在同一范围内。 此外,您现在拥有的代码是关闭连接,如果您打算将其传递给另一个线程,则需要将其保持打开状态。 !编辑一些示例代码 public class Whatever { public stati ...
  • 只需取消scheduledAtFixedRate()返回的将来: public static void main(String[] args) throws InterruptedException { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); Runnable r = new Runnable() { @Override p ...
  • 是的,您可以只使用一个池来安排您的所有任务。 另外,如果您需要按照您现在正在执行的时间间隔计划任务,则应该使用newScheduledThreadPool因为newCachedThreadPool只返回ExecutorService接口而不返回ScheduledExecutorService 。 Yes, you can use just one pool to schedule all your tasks. Also, if you need the ability to schedule tasks ...
  • 用try catch包装run代码 只是一个猜测:抛出异常。 如果遇到Exception, ScheduledExecutorService静默停止。 run方法的代码应该总是被try-catch包围,以处理和吸收抛出的异常。 @Override public void run() { try { // Let no Exception reach the ScheduledExecutorService. Date date = new Date(System.current ...
  • Executors#newSingleThreadScheduledExecutor()返回的ScheduledExecutorService线程池使用非守护线程。 在关闭线程池之前,这些仍然在等待任务。 当非守护线程处于活动状态时,JVM不会结束。 您可以使用重载的Executors#newSingleThreadScheduledExecutor(ThreadFactory)并提供您自己的创建守护程序线程的ThreadFactory实现。 请注意,这可能会导致您的任务甚至可能无法运行,因为JVM会在任务 ...
  • 两个问题: 默认线程工厂创建非守护线程。 主线程可以结束,但只要存在活动的非守护程序线程,JVM就不会终止。 我相信你将需要编写一个自定义线程工厂来创建守护进程线程。 不要依赖于被调用的终结器 - 不能保证在任何特定时间或任何时候都会调用终结器。 此外, System.gc()调用被定义为对JVM的建议 ,而不是命令。 API文档中的措辞是 调用gc方法表明Java虚拟机花费了对未使用对象进行回收的努力...... After playing with WeakReference and Scheduled ...
  • ThreadLocal local = new ThreadLocal(); local.set("String"); System.out.println(local.get()); 您需要在ThreadLocalVariable中设置一些内容然后检索它。 最初,ThreadLocal为空。 ThreadLocal local = new ThreadLocal(); local.set("String"); System.out.prin ...
  • 在第二个片段中,您创建了一个Thread实例,但ScheduledExecutorService已将该实例用作Runnable ,即。 它只关心它的run方法。 它实际上并不start Thread 。 ScheduledExecutorService维护自己的Thread以执行任务。 使用newSingleThreadScheduledExecutor ,这是一个非守护进程Thread 。 您需要shutdown ScheduledExecutorService才能完成该Thread 。 Timer的ja ...

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。