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();
}
}
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();
}
}
'프로그래밍 정리 > 자바' 카테고리의 다른 글
Multi Thread(멀티쓰레드) (0) | 2013.07.10 |
---|---|
Thread Join - 쓰레드가 작동할 경우 끝날 때까지 기다려주고 다음 명령문을 실행. (0) | 2013.07.09 |
쓰레드(Thread) (0) | 2013.07.06 |
인터페이스에서 사용할 수 있는 멤버 변수 (0) | 2013.06.19 |
일반적인 인터페이스의 구조 (0) | 2013.06.19 |