Fullstack-Study

1. Collection & Thread


<1> 자바 컬렉션 프레임워크(Java Collection Framework)

Collection 인터페이스 종류

1. List

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("A"); // 중복 허용
System.out.println(list.get(0)); // A

2. Set

3. Map


<2> Thread

1. Thread 생성방법

(1) Thread 상속

class MyThread extends Thread {
 public void run() {
     System.out.println("Thread 실행");
 }
}
MyThread t = new MyThread();
t.start(); // run() 호출 X, start() 호출 O

(2) Runnable 구현

class MyRunnable implements Runnable {
 public void run() {
     System.out.println("Runnable Thread 실행");
 }
}
Thread t = new Thread(new MyRunnable());
t.start();

(3) 차이점

MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
// t1,t2는 별도의 객체, 공유 자원 없음
class MyRunnable implements Runnable {
    private int count = 0; // Runnable 객체 내부 필드  >> 
    @Override
    public void run() {
        for(int i = 0; i < 1000; i++) {
            count++;
        }
        System.out.println(Thread.currentThread().getName() + ": " + count);
    }
}  

public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyRunnable r = new MyRunnable();  
        Thread t1 = new Thread(r, "Thread1");
        Thread t2 = new Thread(r, "Thread2");  
        t1.start();
        t2.start();   
        t1.join();
        t2.join();   
        System.out.println("최종 count: " + r.count);
    }
}

2. Thread 동작 원리

3. Thread 종류

4. Thread 안전(Thread Safety)

ex : ConcurrentHashMap, CopyOnWriteArrayList, CopyOnWriteArraySet