public class JavaExam {
public static void main(String[] args) {
System.out.println("Start");
Thread threadA = new MyThread("Thread AAA");
threadA.start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadA.interrupt();
System.out.println("------ 끝 ------");
}
}
class MyThread extends Thread {
MyThread(String name) {
setName(name);
}
public void run() {
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println(" " +getName() + "InterruptedException예외 발생");
}
System.out.println(" " +getName() +" 끝");
}
}
*************************************************
class JavaExam {
public static void main(String[] args) {
System.out.println("------- 시작 -------");
Thread threadA = new MyThread("Thread AAA");
threadA.start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadA.interrupt();
try {
threadA.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------ 끝 ------");
}
}
class MyThread extends Thread {
MyThread(String name) {
setName(name);
}
public void run() {
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println(" " +getName() + "InterruptedException예외 발생");
}
try {
Thread.sleep(10); // main 스레드 출력 순서 테스트
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" " +getName() +" 끝");
}
}
**************************************************
class JavaExam {
public static void main(String[] args) {
System.out.println("------- 시작 -------");
MyThread threadA = new MyThread("Thread AAA");
threadA.setRunning(true);
threadA.start();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadA.interrupt();
try {
threadA.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean done = true;
threadA.setRunning(false);
while (done) {
try {
threadA.join();
done = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("------ 끝 ------");
}
}
class MyThread extends Thread {
boolean isRun;
MyThread(String name) {
setName(name);
}
public void setRunning(boolean run) {
this.isRun = run;
}
public void run() {
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println(" " +getName() + "InterruptedException예외 발생");
}
try {
Thread.sleep(10); // main 스레드 출력 순서 테스트
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" " +getName() +" 끝");
}
}
'자바 Java' 카테고리의 다른 글
ReadRecentCoordinate (0) | 2015.09.22 |
---|---|
오늘은 무슨 요일 (0) | 2014.12.24 |
입력 문자열에서 숫자를 뽑아 문자로 반환 (0) | 2014.11.26 |
자바 환경 변수 설정 CLASSPATH, JAVA_HOME, Path (0) | 2014.07.24 |
구글 개발자 등록 과정 (0) | 2014.07.24 |