📋 스레드

✅ Thread

package section17.ex01;

class Sample extends Thread {
    // Sample() {
    // setName("MyTask-1"); // 스레드 이름 수정1
    // }

		@Override
    public void run() {
        // System.out.println("thread run.");
        System.out.println(Thread.currentThread().getName()); // 현재 스레드명
    }
}

public class Jump1701 {
    public static void main(String[] args) {
        // System.out.println(Thread.currentThread().getName()); // 현재 스레드명
        
        Sample sample = new Sample();
        sample.setName("MyTask-1"); // 스레드 이름 수정2
        sample.start();
    }
}

✅ Join

package section17.ex02;

import java.util.ArrayList;

class Sample extends Thread {
    int seq;

    public Sample(int seq) {
        this.seq = seq;
    }

		@Override
    public void run() {
        System.out.println(this.seq + " thread start.");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(this.seq + " thread end.");
    }
}

public class Jump1702 {
    public static void main(String[] args) {

        Thread t = new Sample(1);
        t.start();

        try {
            t.join(); // 스레드가 종료될때 까지 대기
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 10개 스레드의 경우
        // ArrayList<Thread> threads = new ArrayList<>();

        // for (int i = 0; i < 10; i++) {
        // Thread t = new Sample(i);
        // t.start();
        // threads.add(t);
        // }

        // for (int i = 0; i < threads.size(); i++) {
        // Thread t = threads.get(i);
        // try {
        // t.join();
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
        // }

        System.out.println("main end.");
    }
}

✅ Runnable 인터페이스

package section17.ex03;

import java.util.ArrayList;

class Sample implements Runnable {
    int seq;

    public Sample(int seq) {
        this.seq = seq;
    }

    @Override
    public void run() {
        System.out.println(this.seq + " thread start.");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(this.seq + " thread end.");
    }
}

public class Jump1703 {
    public static void main(String[] args) {
        ArrayList<Thread> threads = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new Sample(i)); // 수정
            t.start();
            threads.add(t);
        }

        for (int i = 0; i < threads.size(); i++) {
            Thread t = threads.get(i);
            try {
                t.join();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("main end.");
    }
}

✅ 스레드 동기화

package section17.ex04;

class Counter {
    private int count;

    public int getCount() {
        return this.count;
    }

    public void setCount() {
        this.count++;
    }

    // 1. 메서드 동기화
    // public synchronized void setCount() {
    // this.count++;
    // }
}

class Sample extends Thread {
    private Counter counter;
    int seq;

    public Sample(int seq, Counter counter) {
        this.seq = seq;
        this.counter = counter;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                counter.setCount();
                System.out.println(this.seq + "thread add count " + counter.getCount());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 2. 블록 동기화: 성능 고려 전체 메서드가 아닌 특정 영역만 동기화 처리
        // synchronized (counter) {
        // try {
        // for (int i = 0; i < 10; i++) {
        // Thread.sleep(1000);
        // counter.setCount();
        // System.out.println(this.seq + "thread add count " + counter.getCount());
        // }
        // } catch (Exception e) {
        // e.printStackTrace();
        // }
        // }
    }
}

public class Ex1701 {
    public static void main(String[] args) {
        Counter counter = new Counter();

        Sample sample1 = new Sample(1, counter);
        Sample sample2 = new Sample(2, counter);
        sample1.start();
        sample2.start();
    } 
}

✳️ printStackTrace()