かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

JavaVMの終了時に処理をするには

凪瀬 BlogのJavaVM終了時に処理をするには - シャットダウンフック - で紹介されてるシャットダウンフックについて実験してみた。
コメントにfinalizeは後処理にはつかえないぜ!って感じのことも書いてあったのでそれも実験。

コードはミニマムなものを書いたので、超シンプル!

Program.java
package com.kazuki.shutdown;

public class Program {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new ShutdownProcess());
        new FinalizeObject();
    }
}

class ShutdownProcess extends Thread {
    @Override
    public void run() {
        System.out.println("ShutdownProcess#run called.");
    }
}

class FinalizeObject {
    public FinalizeObject() {
        System.out.println("FinalizeObject#init called");
    }
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize called.");
    }
}

FinalizeObjectにfinalizeメソッドを書いてるのがポイント。これを実行すると下のような結果になる。

FinalizeObject#init called
ShutdownProcess#run called.

確かにシャットダウンフックのShutdownProcess#runメソッドが呼び出されてる。
ただ、悲しい事にFinalizeObjectのfinalizeは呼ばれてない。
呼び出すには…mainメソッドでrunFinalizersOnExitでtureを設定すれば呼ばれることは呼ばれる。

Program.javaから一部抜粋
public class Program {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new ShutdownProcess());
        new FinalizeObject();
        Runtime.runFinalizersOnExit(true);
    }
}

実行結果は、finalizeが呼ばれてるのがわかる。

FinalizeObject#init called
ShutdownProcess#run called.
finalize called.

でも、このrunFinalizersOnExitメソッドは非推奨!!
色々探して見たけど、確実にfinalizeを呼び出す方法は、わからなかった。

まぁ、普通はfinalizeなんかに頼らないコードを書くから関係ないはず。

投稿日時 : 2007年11月27日 23:10

Feedback

# re: JavaVMの終了時に処理をするには 2007/11/27 23:14 凪瀬

finalizeは確実に動く保証がないという仕様だったはずです。

なので、セーフティネットとして以上の使い方ができません。
(たとえばStream系のクラスでのclose()忘れの場合の対応など)
このあたりがC++のデストラクタとは違うところですね。

# re: JavaVMの終了時に処理をするには 2007/11/29 21:07 かずき

デストラクタはC++をずっとやっててJavaを勉強しはじめたときに何でないんだ!?不便すぎるんじゃないか!?って思ったところでした。

# full welded ball valve 2012/10/19 0:41 http://www.dwkvalve.com/product_cat_list/Full-Weld

you are truly a just right webmaster. The web site loading velocity is amazing. It kind of feels that you are doing any unique trick. Also, The contents are masterwork. you've done a great activity in this subject!

# sacs lancel 2012/10/19 15:50 http://www.saclancelpascher2013.com

I like this web blog so much, saved to my bookmarks. "Nostalgia isn't what it used to be." by Peter De Vries.

# longchamp le pliage 2012/12/14 22:55 http://www.soldesacslongchamp.info/category/longch

keep up the good do the job!

# sac longchamp pliage 2012/12/15 16:00 http://www.saclongchampachete.com/category/longcha

Very beneficial info. Hope to find out more articles or blog posts soon!

# burberry online 2012/12/16 4:33 http://www.burberryuksale.co/burberry-uk-scarf-c-6

That's just what exactly earbuds usually are for.

# トリーバーチ バッグ 2012/12/16 21:58 http://www.torybruchjp.info/category/トリーバーチ-靴

The fashion don't flip flat as well as Philips doesn't give a travel pouch inside the package.

# sac longchamp 2012/12/17 8:02 http://www.longchampfr.info/category/sac-longchamp

I am sure I is going to visit this place once again soon.

# burberry outlet 2012/12/17 21:11 http://www.sacburberryecharpe.fr/category/burberry

That's just what exactly earbuds are generally for.

# http://sacmichaelkorssoldes.monwebeden.fr/ 2012/12/18 5:59 http://sacmichaelkorssoldes.monwebeden.fr

I fully understand everybody definitely will hate on them, but I do not think they start looking so terrible.

# burberry coats 2012/12/18 22:19 http://burberryukoutlets.wordpress.com/category/bu

Go with these mature items and just look for your personal that tickle any fancy.

# coachoutletonline999.com 2013/04/07 23:03 http://www.coachoutletonline999.com/

Rarely ever lower, no matter if that you're pathetic, since we can never predict who's cascading gets interested your grin.

# nKRVQySwdidxA 2015/04/17 12:15 sally

lBqUAv http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# Great info. Lucky me I came across your website by accident (stumbleupon). I have saved as a favorite for later! 2019/04/18 16:24 Great info. Lucky me I came across your website by

Great info. Lucky me I came across your website by accident (stumbleupon).
I have saved as a favorite for later!

# Good day! I could have sworn I've been to this blog before but after browsing through many of the articles I realized it's new to me. Nonetheless, I'm certainly delighted I stumbled upon it and I'll be bookmarking it and checking back often! 2019/06/12 10:49 Good day! I could have sworn I've been to this blo

Good day! I could have sworn I've been to this blog before but after browsing through many of the articles
I realized it's new to me. Nonetheless, I'm certainly
delighted I stumbled upon it and I'll be bookmarking
it and checking back often!

# Excellent beat ! I wish to apprentice at the same time as you amend your website, how could i subscribe for a blog web site? The account aided me a acceptable deal. I were tiny bit familiar of this your broadcast offered bright transparent concept 2019/09/17 7:58 Excellent beat ! I wish to apprentice at the same

Excellent beat ! I wish to apprentice at the same time as
you amend your website, how could i subscribe for a blog
web site? The account aided me a acceptable deal.

I were tiny bit familiar of this your broadcast offered bright transparent concept

タイトル
名前
Url
コメント