Language/JAVA

[JAVA] μŠ€λ ˆλ“œ(Thread) μ˜λ―Έμ™€ μ‚¬μš©λ²•

κ΅ λ―Ό 2024. 8. 24. 17:44

Process와 Thread

πŸ“Œ ν”„λ‘œμ„ΈμŠ€(Process) : cpu에 μ˜ν•΄ λ©”λͺ¨λ¦¬μ—μ„œ μ‹€ν–‰ 쀑인 ν”„λ‘œκ·Έλž¨

  • μžλ°” JVM(Java Virtual Machine)은 주둜 ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€λ‘œ μ‹€ν–‰λ˜λ©°, λ™μ‹œμ— μ—¬λŸ¬ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° μœ„ν•΄ λ©€ν‹° μŠ€λ ˆλ“œλ₯Ό μ§€μ›ν•œλ‹€. 

πŸ“Œ μŠ€λ ˆλ“œ(Thread) : λ©”λͺ¨λ¦¬μ— μ˜¬λΌκ°€ μ‹€ν–‰ 쀑인 ν”„λ‘œμ„ΈμŠ€ μ•ˆμ—μ„œ μ‹€μ§ˆμ μœΌλ‘œ μž‘μ—…μ„ μ‹€ν–‰ν•˜λŠ” λ‹¨μœ„

  • 단일 μŠ€λ ˆλ“œ : ν•œ ν”„λ‘œκ·Έλž¨μ— μŠ€λ ˆλ“œκ°€ 1개일 경우
  • λ©€ν‹° μŠ€λ ˆλ“œ : ν•œ ν”„λ‘œκ·Έλž¨μ— μŠ€λ ˆλ“œκ°€ 2개 이상인 경우

 

πŸ‘πŸ»μž‘μ—… λ‹¨μœ„λ‘œ λ³΄λŠ” μš΄μ˜μ²΄μ œμ™€ μŠ€λ ˆλ“œπŸ‘πŸ»

운영 체제의 μž…μž₯μ—μ„œ  →  μž‘μ—…μ˜ λ‹¨μœ„λŠ” ν”„λ‘œμ„ΈμŠ€

cpuμž…μž₯μ—μ„œ →  μž‘μ—…μ˜ λ‹¨μœ„λŠ” μŠ€λ ˆλ“œ

 

Process와 Thread의 차이점

κ°€μž₯ 큰 차이 ν‚€μ›Œλ“œ : 독립성 (각각의 λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ή λ°›λŠ” 것)

ν”„λ‘œμ„ΈμŠ€λŠ” 독립성을 λ„μš°κΈ°μ— μ„œλ‘œ 영ν–₯을 λ―ΈμΉ˜μ§€ μ•Šμ§€λ§Œ μŠ€λ ˆλ“œμ˜ μž‘μ—…μ€ μ„œλ‘œ 영ν–₯을 λ―ΈμΉœλ‹€. 

 

Thread 생성법

  • Thread 클래슀λ₯Ό 상속 λ°›μ•„ μƒμ„±ν•˜λŠ” 방법.
    • run() λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©
class MyThread extends Thread { //μŠ€λ ˆλ“œλ₯Ό 상속받은 클래슀 생성
    public void run() { //ν•΄λ‹Ή ν΄λž˜μŠ€κ°€ ν•΄μ•Όν•  μž‘μ—…μ„ run λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•˜μ—¬ λͺ…μ‹œ
        System.out.println("Thread is running.");
    }

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // main문의 μ‹€ν–‰μœΌλ‘œ μŠ€λ ˆλ“œ 생성 및 start λ©”μ„œλ“œλ₯Ό 톡해 μ‹€ν–‰
    }
}
  • Runnable μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜μ—¬ μƒμ„±ν•˜λŠ” 방법
    • run() λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©
    • ν΄λž˜μŠ€λŠ” 닀쀑 상속이 λΆˆκ°€ν•˜κ³  μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑 상속이 κ°€λŠ₯ν•˜κΈ°μ— 주둜 μ‚¬μš©λ˜λŠ” λ°©λ²•
class MyRunnable implements Runnable { // Runnable μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” 클래슀 생성
    public void run() { // run() λ©”μ„œλ“œλ₯Ό μ˜€λ²„λΌμ΄λ”©ν•˜μ—¬ ν•΄μ•Όν•  μž‘μ—… λͺ…μ‹œ
        System.out.println("Runnable is running.");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable()); // Runnable 객체λ₯Ό μƒμ„±ν•˜μ—¬ μŠ€λ ˆλ“œ 생성
        thread.start(); // μŠ€λ ˆλ“œμ˜ run() λ©”μ„œλ“œλ₯Ό μ‹€ν–‰
    }
}

 

 

πŸ“„ Thread의 μž‘λ™μ„ μ•Œμ•„λ³Ό 수 μžˆλŠ” μ˜ˆμ‹œ μ½”λ“œ

class Task extends Thread {
	@Override
    public void run() {
        for (int i = 1; i <= 1000; i++) {
            System.out.println("Task - Count: " + i);
        }
    }
}

public class ThreadMain {
    public static void main(String[] args) {
        Task t1 = new Task(); // 
        t1.start(); // μƒˆλ‘œμš΄ μŠ€λ ˆλ“œλ‘œ μ‹œμž‘ μ‹€ν–‰
        for (int i = 1; i <= 1000; i++) {
            System.out.println("Main - Count: " + i);
        }
    }
}

 

μ‹€ν–‰ κ²°κ³Ό μ˜ˆμ‹œ

Main - Count: 1
Task - Count: 1
Main - Count: 2
Main - Count: 3
Task - Count: 2
Task - Count: 3
Main - Count: 4
...
Task - Count: 1000
Main - Count: 999
Main - Count: 1000

: Task μŠ€λ ˆλ“œμ™€ main μŠ€λ ˆλ“œκ°€ 각각 1λΆ€ν„° 1000κΉŒμ§€μ˜ 숫자λ₯Ό 좜λ ₯ν•˜λŠ”λ°, 두 μŠ€λ ˆλ“œμ˜ 좜λ ₯이 μ„žμ—¬μ„œ λ‚˜νƒ€λ‚¨.

(두 μŠ€λ ˆλ“œμ˜ μ‹€ν–‰ μˆœμ„œλŠ” 운영체제의 μŠ€μΌ€μ€„λŸ¬μ— μ˜ν•΄ κ²°μ •λ˜κΈ°μ— μ‹€ν–‰ λ•Œλ§ˆλ‹€ λ‹¬λΌμ§ˆ 수 μžˆλ‹€.)

 


μ°Έκ³  μ˜μƒ