凪瀬 Blog
Programming SHOT BAR

目次

Blog 利用状況
  • 投稿数 - 260
  • 記事 - 0
  • コメント - 46859
  • トラックバック - 192
ニュース
広告
  • Java開発者募集中
  • 経歴不問
  • 腕に自信のある方
  • 富山市内
  • (株)凪瀬アーキテクツ
アクセサリ
  • あわせて読みたい
凪瀬悠輝(なぎせ ゆうき)
  • Java技術者
  • お茶好き。カクテル好き。
  • 所属は(株)凪瀬アーキテクツ
  • Twitter:@nagise

書庫

日記カテゴリ

 

Programming SHOT BARへようこそ。 ここのところマルチスレッド関連の話題で盛り上がっています。 今日はかつのりさまからのリクエストでスレッドのキャンセルにまつわる話です。

参考資料

以下の書籍を参考にしています。手元にある方は参照してみてください。

Thread#stop()とその問題点

Thread#stop()、Thread#suspend()、Thread#resume()という一連のスレッドの停止・再開メソッドは 非推奨メソッドなっており、使用してはいけません
この件については、sunからわざわざ詳細な説明がアナウンスされています。 http://java.sun.com/j2se/1.5.0/ja/docs/ja/guide/misc/threadPrimitiveDeprecation.html

スレッドは外部から強制的に、しかし、安全に停止させることはできません。 なんらかの処理をしている最中に安全に中断するということは本質的に難しいのです。
その状況によって後始末を必要とします。 停止の要求に対して、「ちょっとまって。今中断するから」といった協力なしには安全な停止はなしえません。

フラグを立ててスレッドを停止させる

スレッドに対し、割り込みフラグを用意することで自発的な停止をさせることが出来ます。

    /** 割り込みフラグ */
    private volatile boolean stopFlag;
    @Override
    public void run() {
        while(!this.stopFlag) {
            // 実処理
        }
    }

上記サンプルではvolatileによってメモリ同期化されたフラグを利用して、 whileループを抜けるようにしてあります。 このように、外部からフラグを変更してやることでスレッドを自発的に停止させています。

しかし、Object#wait()やObject#join()などのスレッドをブロックするメソッドを 利用している場合、フラグを判定する箇所(キャンセルポイントといいます)まで 永遠にたどり着くことはないかもしれません。

個々のプログラマが独自に定義したスレッドの割り込みフラグの限界はここにあります。 APIのような不特定多数に利用してもらう場合には、やはり標準的な仕組みが欲しいですね。

Thread#interrupt()はその標準的なスレッドの割り込みフラグなのです。

Thread#interrupt()

Thread#interrupt()はThread標準で提供される停止フラグの通知機構です。 この呼び出しで対象のスレッドに対して割り込みフラグが立ちます。
この割り込みフラグはThread#isInterrupt()によって参照することができます。 staticなメソッドThread#interrupted()を使うと、現在のスレッドの割り込みフラグを 参照した上でフラグをリセットすることが出来ます。詳しくはjavadocを参照してください。

Object#wait()はThread#interrupt()によってスレッドの停止フラグが立つと、 waitすることを止めて、InterruptedExceptionをthrowします。
waitに限らず、ブロックされるメソッドは、基本的にスレッド停止フラグが立つと InterruptedExceptionをthrowする作りになっています。

また、先のサンプルのように、ブロックされないスレッドにおいては、Thread#interrupt()を 利用しなくてもスレッドの停止を行うことが出来ます。

では、我々プログラマが独自に作成したスレッドではこの どのような対処をする必要があるのでしょうか?

  • InterruptedExceptionをthrowするように作る
  • Thread#interrupt()で立てられた割り込みフラグを維持し、どこかのキャンセルポイントまで流す
  • 自力でキャンセル処理をし、スレッドを終了させる

という3つの選択肢があります。 スレッドから呼び出される想定のメソッドで、処理が長引くことが想定される場合は InterruptedExceptionをthrowする作りにしておくとよいでしょう。 例えば集計処理などの時間が掛かる処理ですね。
そもそも、そんなに時間の掛からない処理であれば、キャンセルポイントを作る必要もありません。 フラグに触らずそのまま処理を終えてどこかのキャンセルポイントにお任せします。
Thread#run()のような場所ではExceptionをthrowsするように宣言できませんから、 こういう箇所では自力でキャンセルポイントを実装してやります。

あらかじめThread#interrupt()による割り込みをチェックして 中断させるキャンセルポイントを作っておくことで、無駄な処理を続けることなく 速やかに停止させることが出来ます。 せっかちな現代人には必須の機能かもしれませんね。

業務での応用

集計処理など時間が掛かることが想定されるメソッドは、Thread#interrupt()による 停止をサポートしておくと、途中キャンセルされた場合に処理を止めることで CPUリソースを無駄に食わずに済みます。

java.nio.channels.InterruptibleChannelによる通信であれば、 通信に割り込んで停止させることも可能です。

このあたり、業務要件で顧客から直接要望されることは少ないでしょうが、 パフォーマンスにこだわりがあるお客さん相手なら、 設計段階から中断についても考慮しておくと良いかもしれません。

投稿日時 : 2007年8月28日 20:28
コメント
  • # re: スレッドのキャンセル
    かつのり
    Posted @ 2007/08/29 0:17
    スレッドシリーズが充実してきましたね。

    この前の質問の
    try{
    ほげほげ
    ]catch(InterruptedException e){
    Thread.currentThread().interrupt();
    throw e;
    }
    のThread.currentThread().interrupt();のやる意味がわかりました。
  • # re: スレッドのキャンセル
    凪瀬
    Posted @ 2007/08/29 12:01
    キャンセルポイントを実装しておかないとあまり意味はないのですけど、
    少なくともThread.currentThread().interrupt()された状態でwait()などに遭遇すると
    その時点でInterruptedExceptionが出るので無駄にwait()されないメリットがあります。

    実装例とかはまた書かないといけませんね。
  • # adina
    bogemi
    Posted @ 2011/09/25 20:23

    http://www.buysale.ro/anunturi/diverse/donatii-si-sponsorizari/brasov.html - brasov
  • # Genuinely when someone doesn't be aware of after that its up to other people that they will help, so here it happens.
    Genuinely when someone doesn't be aware of after t
    Posted @ 2021/08/28 20:22
    Genuinely when someone doesn't be aware of after that its up to
    other people that they will help, so here it happens.
  • # I visit every day some blogs and websites to read articles, but this website presents quality based content.
    I visit every day some blogs and websites to read
    Posted @ 2021/09/01 4:10
    I visit every day some blogs and websites to read
    articles, but this website presents quality based content.
  • # I visit every day some blogs and websites to read articles, but this website presents quality based content.
    I visit every day some blogs and websites to read
    Posted @ 2021/09/01 4:11
    I visit every day some blogs and websites to read
    articles, but this website presents quality based content.
  • # I visit every day some blogs and websites to read articles, but this website presents quality based content.
    I visit every day some blogs and websites to read
    Posted @ 2021/09/01 4:12
    I visit every day some blogs and websites to read
    articles, but this website presents quality based content.
  • # I visit every day some blogs and websites to read articles, but this website presents quality based content.
    I visit every day some blogs and websites to read
    Posted @ 2021/09/01 4:13
    I visit every day some blogs and websites to read
    articles, but this website presents quality based content.
  • # Great delivery. Outstanding arguments. Keep up the great spirit.
    Great delivery. Outstanding arguments. Keep up the
    Posted @ 2021/09/01 19:46
    Great delivery. Outstanding arguments. Keep up the great
    spirit.
  • # I'm curious to find out what blog system you are working with? I'm experiencing some minor security problems with my latest website and I'd like to find something more safe. Do you have any solutions?
    I'm curious to find out what blog system you are w
    Posted @ 2021/09/04 22:54
    I'm curious to find out what blog system you are working with?
    I'm experiencing some minor security problems with my latest website and I'd like to find something more
    safe. Do you have any solutions?
  • # I'm curious to find out what blog system you are working with? I'm experiencing some minor security problems with my latest website and I'd like to find something more safe. Do you have any solutions?
    I'm curious to find out what blog system you are w
    Posted @ 2021/09/04 22:55
    I'm curious to find out what blog system you are working with?
    I'm experiencing some minor security problems with my latest website and I'd like to find something more
    safe. Do you have any solutions?
  • # I'm curious to find out what blog system you are working with? I'm experiencing some minor security problems with my latest website and I'd like to find something more safe. Do you have any solutions?
    I'm curious to find out what blog system you are w
    Posted @ 2021/09/04 22:56
    I'm curious to find out what blog system you are working with?
    I'm experiencing some minor security problems with my latest website and I'd like to find something more
    safe. Do you have any solutions?
  • # I'm curious to find out what blog system you are working with? I'm experiencing some minor security problems with my latest website and I'd like to find something more safe. Do you have any solutions?
    I'm curious to find out what blog system you are w
    Posted @ 2021/09/04 22:57
    I'm curious to find out what blog system you are working with?
    I'm experiencing some minor security problems with my latest website and I'd like to find something more
    safe. Do you have any solutions?
  • # Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is wonderful blog. A great read. I'll ce
    Its like you read my mind! You seem to know a lot
    Posted @ 2021/09/06 0:25
    Its like you read my mind! You seem to know a lot about this, like you wrote
    the book in it or something. I think that you could do with some pics
    to drive the message home a little bit, but other than that, this is wonderful blog.
    A great read. I'll certainly be back.
  • # Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is wonderful blog. A great read. I'll ce
    Its like you read my mind! You seem to know a lot
    Posted @ 2021/09/06 0:26
    Its like you read my mind! You seem to know a lot about this, like you wrote
    the book in it or something. I think that you could do with some pics
    to drive the message home a little bit, but other than that, this is wonderful blog.
    A great read. I'll certainly be back.
  • # Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is wonderful blog. A great read. I'll ce
    Its like you read my mind! You seem to know a lot
    Posted @ 2021/09/06 0:27
    Its like you read my mind! You seem to know a lot about this, like you wrote
    the book in it or something. I think that you could do with some pics
    to drive the message home a little bit, but other than that, this is wonderful blog.
    A great read. I'll certainly be back.
  • # Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is wonderful blog. A great read. I'll ce
    Its like you read my mind! You seem to know a lot
    Posted @ 2021/09/06 0:28
    Its like you read my mind! You seem to know a lot about this, like you wrote
    the book in it or something. I think that you could do with some pics
    to drive the message home a little bit, but other than that, this is wonderful blog.
    A great read. I'll certainly be back.
  • # Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to this brilliant piece of writing. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
    Hi i am kavin, its my first time to commenting any
    Posted @ 2021/09/14 11:53
    Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to
    this brilliant piece of writing. scoliosis surgery
    https://coub.com/stories/962966-scoliosis-surgery scoliosis
    surgery
  • # Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to this brilliant piece of writing. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
    Hi i am kavin, its my first time to commenting any
    Posted @ 2021/09/14 11:54
    Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to
    this brilliant piece of writing. scoliosis surgery
    https://coub.com/stories/962966-scoliosis-surgery scoliosis
    surgery
  • # Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to this brilliant piece of writing. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
    Hi i am kavin, its my first time to commenting any
    Posted @ 2021/09/14 11:55
    Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to
    this brilliant piece of writing. scoliosis surgery
    https://coub.com/stories/962966-scoliosis-surgery scoliosis
    surgery
  • # Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to this brilliant piece of writing. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
    Hi i am kavin, its my first time to commenting any
    Posted @ 2021/09/14 11:56
    Hi i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment due to
    this brilliant piece of writing. scoliosis surgery
    https://coub.com/stories/962966-scoliosis-surgery scoliosis
    surgery
  • # Hello i am kavin, its my first time to commenting anyplace, when i read this piece of writing i thought i could also make comment due to this sensible paragraph.
    Hello i am kavin, its my first time to commenting
    Posted @ 2021/11/24 1:40
    Hello i am kavin, its my first time to commenting anyplace,
    when i read this piece of writing i thought
    i could also make comment due to this sensible paragraph.
  • # Excellent beat ! I wish to apprentice whilst you amend your website, how can i subscribe for a blog website? The account helped me a acceptable deal. I were tiny bit acquainted of this your broadcast provided shiny clear idea
    Excellent beat ! I wish to apprentice whilst you a
    Posted @ 2021/12/14 11:50
    Excellent beat ! I wish to apprentice whilst you amend your website,
    how can i subscribe for a blog website? The account helped me a acceptable
    deal. I were tiny bit acquainted of this your broadcast provided shiny
    clear idea
  • # Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays. I seriously appreciate individuals like you! Take care!!
    Excellent blog you've got here.. It's hard to find
    Posted @ 2022/03/25 8:14
    Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays.
    I seriously appreciate individuals like you! Take care!!
  • # Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays. I seriously appreciate individuals like you! Take care!!
    Excellent blog you've got here.. It's hard to find
    Posted @ 2022/03/25 8:15
    Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays.
    I seriously appreciate individuals like you! Take care!!
  • # Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays. I seriously appreciate individuals like you! Take care!!
    Excellent blog you've got here.. It's hard to find
    Posted @ 2022/03/25 8:16
    Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays.
    I seriously appreciate individuals like you! Take care!!
  • # Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays. I seriously appreciate individuals like you! Take care!!
    Excellent blog you've got here.. It's hard to find
    Posted @ 2022/03/25 8:17
    Excellent blog you've got here.. It's hard to find excellent writing like yours nowadays.
    I seriously appreciate individuals like you! Take care!!
  • # ルイ ヴィトン ピアス zozo
    aziezxiy@excite.co.jp
    Posted @ 2022/09/03 17:30
    先日購入しました。いろいろ購入前は悩みましたが、ショップレビュー等を見て大丈夫かなと思い購入を決めました。状態もキレイで買って正解でした。梱包も気を使っていただきありがとうございました。また機会があったら是非利用したいお店です!
    ルイ ヴィトン ピアス zozo https://www.gmt78.com/product/detail/11094.htm
  • # ロレックス ヨットマスター オーバーホール
    zoyedohdjvq@hotmail.co.jp
    Posted @ 2022/09/28 3:23
    良いお店です。梱包に感動しました。今まで、他の店で購入した事がありますが、こんなに、丁寧な梱包は初めて見た。商品が、崩れない、梱包。ありがとうございます。
    【最短当日発送】遅くとも1~2営業で出荷可能、全国一律送料無料。販売価格はフリーダイヤルにてご相談させてください♪クロエ トートバッグ エクリプス 8AS527 ブロンズ レザー新品 Chloe
    ロレックス ヨットマスター オーバーホール https://www.kopijp.com/product/detail.aspx-id=11033
  • # ロレックス デイデイト2 プラチナ
    unrylj@live.jp
    Posted @ 2022/09/28 3:26
    ブランド 激安、安心、安全大特価
    【新品】バッグ、財布、靴、帽子、アパレル、ベルト、その他小物 シャネル グッチ エルメス ロレックス ROLEX VUITTON
    シャネル グッチ エルメス ROLEX S級 シャネル S グッチ S級 エルメス S級 ROLEX
    AAA級 VUITTON AAA級 シャネル AAA級 グッチ AAA級 エルメス AAA級品 N級品ルイ ヴィトンS級品
    シャネルS級品 グッチS級品 エルメスS級品 ロレックスS級品 ルイ ヴィトンAAA級品 シャネルAAA級品 グッチAAA級品 エルメスAAA級品
    ■スタイルが多い、品質がよい、価格が低い!
    ■ 送料無料(日本全国) ご注文を期待しています!
    ■信用第一、良い品質、低価格は
    ■当社の商品は絶対の自信が御座います
    激安、安心、安全にお届けします.品数豊富な商
    商品数も大幅に増え、品質も大自信です
    100%品質保証!満足保障!リピーター率100%!
    ロレックス デイデイト2 プラチナ https://www.2bcopy.com/product/product.aspx-id=4441.htm
  • # ルイ ヴィトン ランキング iphone
    zydzrhdadrl@aol.jp
    Posted @ 2022/09/28 3:27
    手書きの挨拶文が入っていてとても感動しました。
    一つ一つ丁寧に梱包されていてまた利用しようかなと思うショップでした。
    ルイ ヴィトン ランキング iphone https://www.gmt78.com/product/detail/4432.htm
  • # ロレックス デイトジャスト 名古屋
    ocaasit@msn.com
    Posted @ 2022/09/28 3:28
    いくつか女性の時間を節約するために、異なる場合はすべて同じバッグは、時にとオシャレに見える合わない、
    不協和。最高のいくつかのバッグに出勤して、それぞれ、レジャーやディナーなどの異なる場合。出勤する時用の
    バッグは大きく、保存の必需用品が多いが、出勤しなければならないと仕様気前が良くて、イメージに符合し、
    ブリーフケースデザインのバッグなどには最適の。
    経営方針: 顧客は至上、品質を重視、納期も厳守、信用第一!
    品質がよい、価格が低い、実物写真。
    当社の商品は絶対の自信が御座います。
    100%品質保証 !満足保障100%!
    ロレックス デイトジャスト 名古屋 https://www.2bcopy.com/product/product.aspx-id=7681.htm
タイトル
名前
Url
コメント