2020 |
07,15 |
/*
スレッド処理を実装してみよう!
threadはcomputerの処理単位のことでこれを複数立ち上げれば複数の処理を
同時に実行させることができる。
実は今まで見てきたmain()の処理もthread上で実行されているので今回はもう一つ
threadを立ち上げて同時に実行されるかを確かめてみましょう
*/
//interfaceを実装するのでimplements を使用します
class MyRunnable implements Runnable{
@Override
public void run(){
for(int i = 0; i<500; i++){
System.out.print('*');
}
}
}
public class MyApp {
public static void main(String[] args) {
MyRunnable r = new MyRunnable ();
//threadクラスのインスタンス
Thread t = new Thread(r);
//startというメソッドを呼び出す
t.start();
for(int i = 0; i<500; i++){
System.out.print('.');
}
}
}
PR
Post your Comment