본문 바로가기

프로그래밍/Java

Runnable 구현하기

 

 

Ex12_5.java

 

package ex12_5;

public class Ex12_5 {

    public static void main(String[] args) {
        GooGoo g2 = new GooGoo(2);
        Thread t2 = new Thread(g2);
        Thread t3 = new Thread(new GooGoo(3));
        Thread t4 = new Thread(new GooGoo(4));
        Thread t5 = new Thread(new GooGoo(5));
        Thread t6 = new Thread(new GooGoo(6));
        Thread t7 = new Thread(new GooGoo(7));
        Thread t8 = new Thread(new GooGoo(8));
        Thread t9 = new Thread(new GooGoo(9));
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();
        t8.start();
        t9.start();
        System.out.println("main 종료");
    }
}
 

 

 

GooGoo.java

package ex12_5;

import java.io.IOException;

public class GooGoo implements Runnable  {
    private int dan;


    public GooGoo(int dan){
    this.dan = dan;
    }


    public void run() {
        long sleepTime = (long)(Math.random() * 500);
        System.out.println(dan+"단이"+sleepTime+"만큼 sleep..");
        try{
            Thread.sleep(sleepTime);
        }catch(Exception e){e.printStackTrace();}
       
        for(int i = 1; i <= 9; i++){
            System.out.print(dan+"단 : "+dan+"*"+i+"="+dan*i);
        }
    }
}
 

 

결과

........

 

 

 

'프로그래밍 > Java' 카테고리의 다른 글

if 문  (0) 2012.06.19
이차원 배열  (0) 2012.06.19
Thread 구현하기  (0) 2012.06.19
Thread 스레드  (0) 2012.06.19
File API  (0) 2012.06.19