JAVA Basic 강의자료] Runnable 구현

 

JAVA Basic 강의자료] Runnable 구현

 

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

 

Runnable 구현

 

*Runnable인터페이스 구현
-run()을 반드시 재정의
-Thread클래스의 멤버들을 사용하지 못함
-단, sleep()은 static이기 때문에 사용 가능

 

 

<소스코드>

 

class SoloRunnable implements Runnable{
      private int number = 0;
      public SoloRunnable(int n) {
              System.out.println(":스레드시작");
              number = n;
      }
       public void run() {
               int i = 0;
               while(i < number) {
                     System.out.print(i + "\t");
                     i++;
                }
                System.out.println(":스레드종료");
         }
}

public class SoloRunnableMain {
        public static void main(String args[] ) {
                new Thread(new SoloRunnable(10)).start();
        }
}

 

 

 

=====================

 

 

Runnable

 

 

*Runnable인터페이스를 구현
-run()메서드를 반드시 재정의해야 함
-run()메서드를 재정의한 클래스를 Thread클래스의 매개변수로 삽입

 

 

<소스코드>

 

import java.awt.*;
class SoloFrame extends Frame implements Runnable {
     private int number = 0;
     public SoloFrame(int n) {
             number = n;
             Thread t = new Thread(this);
             t.start();
             System.out.println(t.getName() + ":스레드시작");
    }
    public void run() {
               int i = 0;
               while(i < number) {
                              System.out.print(i + "\t");
                              i++;
               }
               System.out.println(":스레드종료");
    }
}
public class SoloFrameMain {
       public static void main(String[] args) {
                       //스레드의 객체 생성
               SoloFrame s = new SoloFrame(10);
               s.setSize(100, 100);
               s.show();
     }
}

 

+ Recent posts