スレッドを起動する
- Threadクラスを拡張したクラスを作る。
- run()メソッドを定義する。
- インスタンスを作る。
- start()メソッドで、別スレッドでrun()メソッドが実行される。
public class Car extends Thread{
public void run(){
}
}
public class Test {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
スレッドを一時停止する
- sleep(1000)メソッドを書く。1000で1秒。
- try~catch文を書く。
- InterruptedExceptionをcatchする。
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
InterruptedException
スレッドの終了を待つ
- join()メソッドを書く。
- try~catch文を書く。
- InterruptedExceptionをcatchする。
Car car = new Car();
try {
car.join();
} catch (InterruptedException e) {
e.printStackTrace();
}