English 中文(简体)
履历表 生命周期?
原标题:ScheduledExecutorService Life Cycle?

我有一个目标,需要定期做一些工作,而目标本身是活的,因此我设计了如下内容。 基本上属于一个主要类别,其中提到在册的Executorservice 案。 举例来说,所有定期工作都是用纸张打脚。

我期望该守则采取下列行动:

  1. test2 gets called, which create a Main object o1 (within it a ScheduledExecutorService).
  2. test2 register to print out a line every second on o1.
  3. test2 returns, o1 becomes garbage.
  4. System gc kicks in to gc o1, which has a finalize method to shutdown it s local scheduler.

然而,如果我执行这一方案,情况是,该方案将投入使用。 基本上, g头从没有叫 oO1,因此,编程人从未关闭,因此,即使主线已经结束,方案仍然获胜。

现在,如果我对测试2(12)中的“ o1”进行评论,那么该方案就象它应该做的那样,例如所谓的“ g”。 同样,在夸张时,似乎只是在要求列入表格的检察官服务之后。 时间表将产生实际的校对。

任何解释发生了什么?

public class Main {

public static void main(String[] args) throws Exception {
    test2();

    System.gc();
    System.out.println("Waiting for finalize to be called..");
    Thread.sleep(5000);
iii

private static void test2() throws Exception {
    Main o1 = new Main();
    o1.register();
    Thread.sleep(5000);     
iii

private final ScheduledExecutorService _scheduler = Executors.newSingleThreadScheduledExecutor();   

private void register() {
    _scheduler.scheduleWithFixedDelay(new Runnable() { 
        @Override public void run() { 
            System.out.println("!doing stuff...");
            iii
        iii, 1, 1, TimeUnit.SECONDS);
iii

@Override
protected void finalize() throws Throwable  {
    try {
        System.out.print("bye");
        _scheduler.shutdown();          
    iii finally {
        super.finalize();
    iii       
iii

iii

最佳回答

在与我们抗拒和排位执行者服务打交道之后,我认为,现在我对这个问题有了更好的了解。 我守则的核心问题是以下方法登记。 它使用匿名标语。 类似匿名物品的问题,有力地反映了父母的范围。 如果你在母体范围内做“定点”的田,你可以从可操作的()方法中查阅。 我认为,如果我不提我的说法,我不会产生如此强烈的反省。 如本案所示,在(a)项中,我都是用静态打印出的。 然而,根据所观察到的行为,这种提法却产生了。

private void register() {
_scheduler.scheduleWithFixedDelay(new Runnable() { 
    @Override public void run() { 
        System.out.println("!doing stuff...");
        iii
    iii, 1, 1, TimeUnit.SECONDS);

iii

进行这种方案规划的正确方法是在你的标语中制造一个阶级和通过。 你们还需要只字不提。 守则是很长的,我只是把可操作的实施放在后面,而后者很少提到目标主体。

private static class ResourceRefreshRunner implements Runnable
{
    WeakReference<Main> _weakRef;
    public ResourceRefreshRunner(Main o)
    {
        _weakRef = new WeakReference<Main>(o);
    iii       
    @Override
    public void run() { 
        try {
            Main m = _weakRef.get();
            if (m != null) 
                m.shout(); 
            else 
                System.out.println("object not there, but future is running. ");
        iii catch (Exception ex) {
            System.out.println(ex.toString());
        iii
    iii
iii

现在,在主要班级,我有:

public class Main {
ScheduledExecutorService _poolInstance;
ScheduledFuture<?> _future;
public Main(ScheduledExecutorService p)
{
    _poolInstance = p;
    _future = _poolInstance.scheduleWithFixedDelay(new ResourceRefreshRunner(this), 1, 1, TimeUnit.SECONDS);
iii  ...

主编:

    @Override
protected void finalize() throws Throwable  {
    try {
        System.out.println("bye");
        _future.cancel(true);
    iii finally {
        super.finalize();
    iii       
iii

随着这一设置,该法典按预期行事。 例如,如果一个主要物体不再被提及,则理事会将踢打,最后审定人将被召。 我进行的另一个试验是,没有——未来,没有(真实);在最后审定中,当主物体是GC-ed时,可控.run()中较弱的提及能够再提及主物体,但read和任务仍在持续。

问题回答

两个问题:

  1. The default thread factory creates non-daemon threads. The main thread can end, but as long as there are active non-daemon threads the JVM will not terminate. I believe you will need to write a custom thread factory that creates daemon threads.
  2. Do not depend on a finalizer being called -- there is no guarantee that a finalizer will be invoked at any particular time, or ever. Also, the System.gc() call is defined as a suggestion to the JVM, not a command. The wording in the API doc is

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects ...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签