最終更新:2017-11-14 (火) 10:58:58 (8d)  

java.lang.Thread はてなブックマークを見る
Top / java.lang.Thread

https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Thread.html

public class Thread extends Object implements Runnable

コンストラクタ

  • Thread()Allocates a new Thread object.
    Thread(Runnable target)Allocates a new Thread object.
    Thread(Runnable target, String name)Allocates a new Thread object.
    Thread(String name)Allocates a new Thread object.
    Thread(ThreadGroup? group, Runnable target)Allocates a new Thread object.
    Thread(ThreadGroup? group, Runnable target, String name)Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
    Thread(ThreadGroup? group, Runnable target, String name, long stackSize)Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.
    Thread(ThreadGroup? group, String name)Allocates a new Thread object.

メソッド

  • Thread.startこのスレッドの実行を開始します。
    Thread.sleep?
    Thread.interruptこのスレッドに割り込みます。
    Thread.joinこのスレッドが終了するのを待機します。
    Thread.stop?推奨されていません
    Thread.suspend?推奨されていません
    Thread.resume?推奨されていません
    Thread.yield?
    Thread.wait?
    Thread.setUncaughtExceptionHandler?

スレッドの作成

  • Thread クラスを継承したクラスを作る
  • Thread.run() メソッドをオーバーライドする
    class PrimeThread extends Thread {
    	long minPrime;
    	PrimeThread(long minPrime) {
    	this.minPrime = minPrime;
    	}
    
    	public void run() {
    		// compute primes larger than minPrime
    	. . .
    	}
    }
  • このクラスをインスタンス化する
  • Thread.start() メソッドを呼び出す
    PrimeThread p = new PrimeThread(143);
    p.start();

  • new Thread(new Runnable() {
            public void run() {
                Bitmap b = loadImageFromNetwork("http://example.com/image.png");
                mImageView.setImageBitmap(b);
            }
        }).start();

関連