- main 쓰레드
- 싱글 쓰레드와 멀티 쓰레드
- 쓰레드의 I/O 블락킹(blocking)
사용자 쓰레드와 데몬 쓰레드(보조 역할 쓰레드) 두 종류가 있다.public class Ex13_11 {
    static long startTime = 0;
    public static void main(String[] args) {
        ThreadEx11_1 th1 = new ThreadEx11_1();
        ThreadEx11_2 th2 = new ThreadEx11_2();
        th1.start();
        th2.start();
        startTime = System.currentTimeMillis();
//        try {
//            th1.join(); // main 쓰레드가 th1의 작업이 끝날 때까지 기다린다.
//            th2.join(); // main 쓰레드가 th2의 작업이 끝날 때까지 기다린다.
//        } catch (InterruptedException e){
//        }
        System.out.println();
        System.out.println("소요시간 : "+(System.currentTimeMillis() - startTime));
    }
}
class ThreadEx11_1 extends Thread {
    @Override
    public void run() {
        for (int i =0; i< 300; i++){
            System.out.print(new String("-"));
        }
    }
} // run ()
class ThreadEx11_2 extends Thread {
    @Override
    public void run() {
        for (int i =0; i< 300; i++){
            System.out.print(new String("|"));
        }
    }
}-----|||||
|||||소요시간 : 1
------||||||||||||||||||||||||||||||||||||||||------------
|||||||||||||||||||||||-----------------------------------||||||||||-----||||-
---------------------------|||||||---------|||||||||||||------||||||----------
---||||||||||||||||||----------||||||----|||||||||----------------------------
----|||||||||----|||||||||--||||||||-----------------------------------------
|||||||||||------||||---------|||||||||-------|||||||||||---|||||------------
||------||||||||||||---------------------|||||||-
||||||||||||||||||||||||||||||||||-----------------------
|||||||||||||||||||||||||||||||||
main 쓰레드가 먼저 종료되었더라도, th1, th2가 종료되지 않으면 프로그램이 종료되지 않은 모습을 확인할 수 있다.
예제 코드에서 join의 의미 : main 메서드가 다른 작업(쓰레드)가 끝날 때 까지 기다리게 하는 것이다.
-------||||||||||||||||||||||||--|||||||||||||||---||||||||||||||||||||----
--------------||||----|||||||||||||||||||||||||||||-----
|||||||||||||||||||||||||||||||-------------------------------||||||-------
----------|||||||||||||||||||||||||||||||||-------------------------------
||||||||||||-----|||||------------------------------------
||||||||||||||||||||||||||||||||||||||-----||||-------------||||||---------
----|||||||||||||||||||-------
|||||||||||||||||||||||||||||||||||||||||||||||||--|||---||----------------
---------------------------------------------------------------------------
-------
   
소요시간 : 8
public class ThreadTest {
    public static void main(String[] args) {
        for (int i = 0; i < 300; i++) { // A
            System.out.println("-");
        }
        for (int i = 0; i < 300; i++) { // B
            System.out.println("|");
        }
    }
}
public class ThreadTest {
    public static void main(String[] args) {
        MyThread1 th1 = new MyThread1();
        MyThread2 th2 = new MyThread2();
        th1.start();
        th2.start();
    }
}
class MyThread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.print("-");
        }
    }
}
class MyThread2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.print("|");
        }
    }
}
- 시간이 조금 더 걸리더라도, 여러 작업을 동시에 할 수 있다는 장점이 있기 때문!
- 작업을 조금 더 효율적으로 할 수 있게 됨!(아래 쓰레드 I/O 블락킹 참고)
import javax.swing.*;
public class ThreadEx7 {
    public static void main(String[] args) {
        ThreadEx7_1 th1 = new ThreadEx7_1();
        th1.start();
        String input = JOptionPane.showInputDialog("아무 값이나 입력하세요.");
        System.out.println("입력하신 값은 "+input+" 입니다.");
    }
}
class ThreadEx7_1 extends Thread {
    @Override
    public void run() {
        for (int i = 10; i > 0; i--) {
            System.out.println(i);
            try {
                sleep(1000);
            } catch (Exception e) {
            }
        }
    }
}
사용자로부터 입력을 기다리는 구간에 th1이 수행된다.

싱글 스레드의 경우 입력값이 들어오지 않으면 다른 작업이 진행되지 않고 멈춰있게 됨.