LoginSignup
1
0

More than 5 years have passed since last update.

http://www5c.biglobe.ne.jp/~ecb/java/12_05.html の「Runnable r = new MyRunnable();」の意味がわかりません. Runnableクラスというのがあるんですか? 他のサイトも見てみましたが(この例だと)「MyRunnable r = new MyRunnable();」のようになってました. どういうことなのか教えて下さい.

Last updated at Posted at 2012-03-16
/*    これはコピペです, すみません.     */

// 独自のクラス
class MyRunnable implements Runnable{
    // このrunメソッドがスレッド本体
    public void run(){
        System.out.println("MyRunnable Start!");

        // 10回500ミリ秒毎に"Hello"と表示する
        for(int i = 0 ; i < 10 ; i++ ){
            System.out.println("Hello");
            try{
                // sleepメソッドはThreadクラスの静的メソッドで、
                // nミリ秒(この場合500ミリ秒)待つ(待機状態になる)メソッド。
                // 例外を投げる可能性があるので、キャッチしないといけない
                Thread.sleep(500);
            }catch(Exception e){}
        }

        System.out.println("MyRunnable End!");
    }
}

public class Test{
    public static void main(String args[]){
        // 独自のスレッドクラスのインスタンスを作成
        Runnable r = new MyRunnable(); /* ここがわからない */
        Thread t = new Thread(r);

        // スレッドを開始
        t.start();

        // 1秒毎に"Hi!"と10回表示する
        for(int i = 0 ; i < 10 ; i++ ){
            System.out.println("Hi!");
            try{
                Thread.sleep(1000);
            }catch(Exception e){}
        }
    }
}

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0