본문 바로가기

학습 기록 (Learning Logs)/김영한 자바

(4)
자료구조
memory visiblity 변경 전main threadcpu core1main thread cache memoryrunFlug=true // 캐시 메모리에 있는 값을 읽기때문에 빠르게 읽을 수 있다.  work threadcpu core2work thread cache memoryrunFlug=true  main memoryrunFlug=true  변경 후main threadcpu core1main thread cache memoryrunFlug=false // 변경  work threadcpu core2work thread cache memoryrunFlug=true // 따라서 이 thread는 멈추지 않는다.  main memoryrunFlug=false // 변경  캐시 메모리를 main 메모리에 반영, main 메모리의..
Thread 생성 방법 Thread 생성 방법은? 1. Thread 클래스 상속 public class MyThread extends Thread { @Override public void run() { // 스레드가 수행할 작업을 여기에 정의 System.out.println("Thread running."); } public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); // 스레드 시작 }}Thread class 상속 시  장점: - run을 재정의만 하면 된다. -> 간단 - 쓰레드 기능 다 쓸 수 있다. 단점: - 상속 제한: 단일 상속..
thread join 아아 테스트 하는데왜 조인하는 것인가가 명확히 기억이 안난다   스레드 join은 왜 필요한가?  main thread의 코드 실행은 sub thread를 기다리지 않는다. [ main] [RUNNABLE] task1.result>> 0 [ main] [RUNNABLE] task2.result>> 0 이미 계산 되었는데 0으로 출력된다.이게 문제다 즉 2초가 지나야 thread-1, thread-2에 있는 코드가 수행되면서[ thread-1] [RUNNABLE] result>> 1275 [ thread-2] [RUNNABLE] result>> 3775heap 메모리에 있는 task1, task2의 result의 값을 대입한다. thread-1, thread-2의 계산이 끝날 때까지 main은 기다려야 한..