[ 스레드란 무엇인가 ] : CPU 작업할당 단위
스레드의 생성과 실행
1. Runnable 인터페이스를 구현하는 방법
2. Thread 클래스를 상속받는 방법
아래 그림 [ 2. Thread 클래스를 상속받는 방법 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package mymain; public class MyThread extends Thread { String str; public MyThread(String str) { this.str = str; } @Override public void run() { // TODO 자동 생성된 메소드 스텁 //스레드 종료 방지 위해 작성(무한반복) while (true) { for (int i = 0; i < 10; i++) { System.out.print(str ); //현재 실행중인 스레드 이름 호출 //[ currentThread().getName() ] System.out.print( currentThread().getName() ); try { Thread.sleep(300); } catch (InterruptedException e) { // TODO 자동 생성된 catch 블록 e.printStackTrace(); } } } } public static void main(String[] args) { MyThread m1 = new MyThread("*"); MyThread m2 = new MyThread("-"); m1.start(); m2.start(); System.out.println("메인 쓰레드 실행"); } } | cs |
아래 그림 [ 1. Runnable 인터페이스를 구현하는 방법 ]
'IT > 자바' 카테고리의 다른 글
쓰레드 공정 (0) | 2018.05.18 |
---|---|
[Java 강의37] 자바 스레드 - 3 (Runnable 인터페이스) (0) | 2018.05.17 |
GridLayout (0) | 2018.05.16 |
BorderLayout (0) | 2018.05.16 |
FlowLayout (0) | 2018.05.16 |