프로그래밍 정리/자바

Runnable, NotRunnable, Dead

주누다 2013. 7. 7. 03:21
반응형

Runnable 

- Runnable 인터페이스를 구현

-> run() 메소드를 반드시 재정의해야 함.

-> run() 메소드를 재정의한 클래스를 Thread클래스의 매개변수로 삽입


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


import java.awt.Frame;


class SoloFrame extends Frame implements Runnable{

private int number = 0;

public SoloFrame(int n) {

// TODO Auto-generated constructor stub

number = n;

Thread t = new Thread(this);

t.start();

System.out.println(t.getName() + ":쓰레드시작");

}


@Override

public void run() {

// TODO Auto-generated method stub

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();

}

}


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


NotRunnable

- 대기와 복구 시키기

-> sleep(long millis) 사용

=> 원하는 대기 시간을 1/1000초 단위로 삽입

=> 설정 시간 경과 후 자동 복귀

-> Object클래스의 wait()와 notify() 사용

=> wait()로 대기 시키고, notify()로 복귀 시킴.


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


import java.util.Date;




class NotRunnableThread extends Thread{

private int number = 0;

public NotRunnableThread(int n) {

// TODO Auto-generated constructor stub

System.out.println(this.getName() + "쓰레드 시작");

number = n;

}

@Override

public void run() {

// TODO Auto-generated method stub

int i = 0;

while(i < number){

System.out.println(this.getName() + ":" + i + new Date().toString() + "\t");

try{

this.sleep(2000);

}catch (Exception e) {

// TODO: handle exception

System.out.println(e);

}

i++;

}

System.out.println(":쓰레드 종료");

}

}


public class NotRunnableThreadMain {

public static void main(String[] args) {

System.out.println("getConstructors() : " + NotRunnableThreadMain.class.getConstructors());

System.out.println("getComponentType() : " + NotRunnableThreadMain.class.getComponentType());

System.out.println("getClassLoader() : " + NotRunnableThreadMain.class.getClassLoader());

System.out.println("getClasses() : " + NotRunnableThreadMain.class.getClasses());

System.out.println("getClass() : " + NotRunnableThreadMain.class.getClass());

System.out.println("getAnnotations() : " + NotRunnableThreadMain.class.getAnnotations());

System.out.println("getSimpleName() : " + NotRunnableThreadMain.class.getSimpleName());

System.out.println("toString() : " + NotRunnableThreadMain.class.toString());

System.out.println("getModifiers() : " + NotRunnableThreadMain.class.getModifiers());

System.out.println("getCanonicalName() : " + NotRunnableThreadMain.class.getCanonicalName());

System.out.println("getName() : " + NotRunnableThreadMain.class.getName());

NotRunnableThread s = new NotRunnableThread(5);

s.start();

}

}


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

Dead
- 쓰레드 죽이기
-> stop()은 시스템 안전상 deprecated 됨.
-> interrupt
=> 호출 받은 쓰레드가 Non-Runnable 상태에 있지 않으면 InterruptedException 예외가 예약
=> Non-Runnable상태에 있거나 InterruptedException을 선언한 메소드를 호출하였으면 InterruptedException 발생
=> 호출 받은 쓰레드가 입출력 함수를 호출하여 Non-Runnable 상태에 있으며 InterruptedException 발생
=> boolean isInterrupted()
=> boolean Thread.interrupted()
-> break와 return 을 사용




sopt을 이용한 쓰레드 종료 예제

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



public class ThreadEnd extends Thread{

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

try{

System.out.println("run - try");

sleep(500);

}catch (InterruptedException e) {

// TODO: handle exception

System.out.println("run - catch");

System.out.println(getName());

}

}

}

public static void main(String[] args) {

ThreadEnd thr = new ThreadEnd();

thr.start();

try{

System.out.println("main - try");

sleep(2000);

}catch (InterruptedException e) {

// TODO: handle exception

System.out.println("main - catch");

}

System.out.println("Stop");

thr.stop();

}


}

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


Interrupt을 이용한 쓰레드 종료 예제

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



public class InterruptTest extends Thread{


static int count;

@Override

public void run() {

// TODO Auto-generated method stub

for(int i=0; i<count; i++){

System.out.println(i+1 + ". before sleep...");

try{

sleep(500);

}catch (Exception e) {

// TODO: handle exception

System.out.println("InterruptedException");

this.stop();

break;

}

}

}

public static void main(String[] args) {

// count = Integer.parseInt(args[0]);

count = 10;

InterruptTest thr = new InterruptTest();

thr.start();

System.out.println("interrupt");

thr.interrupt();

}

}


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


반응형