类型(变量)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 syntaxtype(variable)
withint(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
最满意答案
Executors#newSingleThreadScheduledExecutor()
返回的ScheduledExecutorService
线程池使用非守护线程。 在关闭线程池之前,这些仍然在等待任务。 当非守护线程处于活动状态时,JVM不会结束。您可以使用重载的
Executors#newSingleThreadScheduledExecutor(ThreadFactory)
并提供您自己的创建守护程序线程的ThreadFactory
实现。 请注意,这可能会导致您的任务甚至可能无法运行,因为JVM会在任务计划的时间之前退出。按照您发现并关闭的方式进行操作。 请注意,您应该将其始终关闭在安全的地方,在那里操作不会失败。
The
ScheduledExecutorService
thread pool returned byExecutors#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 ownThreadFactory
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 ...
-
ScheduledExecutorService - 程序在一次操作后不会结束(ScheduledExecutorService - program not ending after one-shot action)[2023-05-22]
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 ... -
ScheduledExecutorService不会像Timer一样结束线程(ScheduledExecutorService does not end thread like Timer)[2022-05-06]
在第二个片段中,您创建了一个Thread实例,但ScheduledExecutorService已将该实例用作Runnable ,即。 它只关心它的run方法。 它实际上并不start Thread 。 ScheduledExecutorService维护自己的Thread以执行任务。 使用newSingleThreadScheduledExecutor ,这是一个非守护进程Thread 。 您需要shutdown ScheduledExecutorService才能完成该Thread 。 Timer的ja ...