1 / 11

L3. Necessary Java Programming Techniques

L3. Necessary Java Programming Techniques. (Thread). Java API. スレッド (thread) : アプリケーションから一部の機能をスレッドとして実行します。            スレッドはプログラムに一部の機能を実行している主体です。. マルチスレッド: 同時に複数のスレッドが存在する。それらのスレッドが並行して動作するのは,マルチスレッド処理機能です。これは Java 言語の特徴の一つです。. Java では、スレッドというのは、一つのオブジェクトです。. クラススレッドのコンストラクタ

harper
Download Presentation

L3. Necessary Java Programming Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L3. Necessary Java Programming Techniques (Thread) Java API

  2. スレッド(thread): アプリケーションから一部の機能をスレッドとして実行します。            スレッドはプログラムに一部の機能を実行している主体です。 マルチスレッド: 同時に複数のスレッドが存在する。それらのスレッドが並行して動作するのは,マルチスレッド処理機能です。これはJava言語の特徴の一つです。 Javaでは、スレッドというのは、一つのオブジェクトです。 クラススレッドのコンストラクタ public Thread() public Thread(String threadName) …… クラススレッドのインスタンス Thread thread1, thread2; thread1 = new Thread(); thread2 = new Thread(“student”); java.lang.Object | +--java.lang.Thread

  3. スレッド状態 (スレッドのライフサイクル):誕生=>レディ=>実行=>死亡。スレッド状態 (スレッドのライフサイクル):誕生=>レディ=>実行=>死亡。 誕生 start 待機  休眠  中断  ブロック 実行可能 yield dispatch notify, notifyAll 実行 入出力が完了 入出力要求 wait sleep suspend 待機 休眠 中断 ブロック resume スリープ時間が経過 stop 死亡 スレッドのライフサイクル (Life cycle of a thread)

  4. クラスThreadのメソッド (The Thread methods) 実行結果 ex2 public void start() runメソッドを起動し、期待された仕事を行う。 public final void stop() ThreadDeathオブジェクトを送出することによってスレッドを停止させる。 public final void suspend()   スレッドの実行を中断される。 public final void resume() 中断されたスレッドを再開させる。   public static void sleep(long millis)   スレッドを眠らせる。 public final void wait() ターゲットオブジェクトを待つためにwaitメソッドを呼び出し、待機状態に入る。 public final void notify() 待ち行列の先頭にあるスレッドが実行可能状態に移る。 public final void notifyAll() 待ち行列にあるスレッドがすべて実行可能状態に移る。 public final void setName(Stringname) スレッドの名前を設定する。 public final String getName() スレッドの名前を返す。 public static Thread currentThread()  カレントスレッドへの参照を返す。 public final boolean isAlive()   スレッドが生きているかを判断する。 IllegalThreadStateException SecurityException InterruptedException IllegalMonitorStateException SecurityException public void run()   スレッドstart()を呼び出すとき、 run()メソッドを起動します。

  5. スレッドクラスの定義 Definition of a thread class キーワード  スレッドクラス名前  キーワード  キーワード class thread_name extendsThread { ……// 必要な変数とコンストラクタの宣言 public void run(){ …… } } 必ずrun()メソッドの再定義が必要です。 class CountTenThread extends Thread { public void run() { for (int i = 0; i < 10; i++) { System.out.println("run:i = " + i);}} } public class ThreadTest { public static void main(String[] args) { CountTenThread ctt = new CountTenThread(); ctt.start(); for (int i = 0; i < 10; i++) { System.out.println("main:i = " + i);}} } CountTenThreadのスレッド。 runメソッドが終ると消滅。 スレッドCountTenThreadの定義 スレッドcttのrunメソッドの実行開始 mainのスレッド

  6. CoffeeShop class CoffeeShop class ShopMaster class Counter class CoffeeDrinker itao counter putCoffee() getCoffee() master honkon higashino

  7. Master made a COFFEE [Coffee] itao can drink a COFFEE! [] honkon can NOT drink a COFFEE! higashino can NOT drink a COFFEE! Master made a COFFEE [Coffee] honkon can drink a COFFEE! [] higashino can NOT drink a COFFEE! Master made a COFFEE [Coffee] higashino can drink a COFFEE! [] Master made a COFFEE [Coffee] Master made a COFFEE [Coffee, Coffee] honkon can drink a COFFEE! [Coffee] higashino can drink a COFFEE! [] itao can NOT drink a COFFEE! honkon can NOT drink a COFFEE! Master made a COFFEE [Coffee] itao can drink a COFFEE! [] honkon can NOT drink a COFFEE! higashino can NOT drink a COFFEE! Master made a COFFEE [Coffee] Output:

  8. Exercise 1 – Thread & Vector • プログラム「CoffeeShop」を改良して、プログラム「VideoShop」を作る。 • プログラム「VideoShop」は、ビデオのレンタルを仮想的に実現させる。 • プログラム「VideoShop」は以下の3つのクラスで成り立つ: • VideoShop • mainメソッドを持ち、RackとUserを生成後、Userをスタートさせる。 • Rack • Vectorとしてビデオ郡を保持する。コンストラクタで、適当にビデオを登録しておく。「借りる」メソッドと「返す」メソッドを提供する。 • User • Threadとして稼動する。乱数によって借りるビデオを決め、Rackから「借りる」。乱数によって決まった時間分Sleepし、Rackにビデオを「返す」。これを繰り返す。

  9. VideoShop class VideoShop class Rack Rack returnVideo() borrowVideo() class User A thread class itao honkon higashino

  10. Exercise 1 – Class Diagram has has has

  11. Exercise 1 – 実行例 板尾が「スターウォーズ1」を借りているため、東野は借りれない(waitする)。 板尾が「スターウォーズ1」を返した(notifyAllしてwaitを解除させる)。 東野は無事、「スターウォーズ1」を借りることができた。 VideoShopEx.java

More Related