자바 Java2014. 12. 5. 00:04






















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() +"   끝");

    }

}

Posted by 코드버무려