๐Ÿ“š TIL 10์ผ์ฐจ

temprmnยท2023๋…„ 5์›” 26์ผ
0
post-thumbnail

์˜ค๋Š˜ ์ฃผ์š” ์‚ฌํ•ญ (5/26, ๊ธˆ์š”์ผ)

  1. Java ํ•™์Šต
    • Thread

Thread(์“ฐ๋ ˆ๋“œ)

Thread ๊ตฌํ˜„

1) Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” Thread ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์€ ํ›„,run() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด run() ๋ฉ”์„œ๋“œ์— ์ž‘์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ์“ฐ๋ ˆ๋“œ์—์„œ ์ˆ˜ํ–‰๋œ๋‹ค.

public class TestThread extends Thread {
	@Override
	public void run() {
		// Thread๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—…
	}
}

๋งŒ๋“ค์–ด์ง„ TestThread ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ์‹œํ‚ค๋ฉด ์“ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

/* Main.java */

// ์“ฐ๋ ˆ๋“œ ๊ฐ์ฒด ์ƒ์„ฑ
TestThread thread = new TestThread();
// ์“ฐ๋ ˆ๋“œ ์‹คํ–‰
thread.start(); 

2) Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

Java์—์„œ ์ œ๊ณตํ•˜๋Š” Runnable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์œ„์˜ Thread ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค. ์ž๋ฐ”๋Š” ๋‹ค์ค‘์ƒ์†์„ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋‹ค์ค‘์ƒ์† ๋ฏธ์ง€์›์ด๋ผ๋Š” ๋‹จ์ ์„ ๋ณด์™„ํ•  ์ˆ˜ ์žˆ๋‹ค.

public class TestRunnable implements Runnable {
	@Override
	public void run() {
		// Thread๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—…
	}
}
/* Main.java */

// Runnable ๊ฐ์ฒด ์ƒ์„ฑ
Runnable run = new TestRunnable();
// Thread ๊ฐ์ฒด ์ƒ์„ฑ, ์ƒ์„ฑ์ž์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ Runnable์„ ์ „๋‹ฌ
Thread thread = new Thread(run); 

// ์“ฐ๋ ˆ๋“œ ์‹คํ–‰
thread.start(); 

3) Runnable + ๋žŒ๋‹ค์‹

์•„์ง ๋žŒ๋‹ค์‹์— ๋Œ€ํ•ด ๋ฐฐ์šฐ์ง„ ์•Š์•˜์ง€๋งŒ ๊ฐ€๋ณ๊ฒŒ ์‚ดํŽด๋ณด์ž.

Runnable ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ๊ณผ ๋™์‹œ์—, ๋žŒ๋‹ค์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ์ œ 2)์—์„œ Runnable.run()์— ์ž‘์„ฑํ–ˆ๋˜ ๊ตฌ๋ฌธ๋“ค์ด ๋žŒ๋‹ค์‹ ๋‚ด๋ถ€์— ๋“ค์–ด๊ฐ„๋‹ค. ๊ฐ์ฒด๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•ด์„œ Runnable.run()์„ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„ ๊ฒƒ์ด๋‹ค. (์ ์–ด๋„ ์ด ์ฝ”๋“œ ์ƒ์—์„œ๋Š”)

public class Main {
    public static void main(String[] args) {
    	
        /* ์ด task ๋ถ€๋ถ„์ด ๋žŒ๋‹ค์‹ */
        // ๊ธฐ์กด Runnable.run() ๋ฉ”์„œ๋“œ์— ๋„ฃ์„ ์‹คํ–‰๋ฌธ์„
        // ์—ฌ๊ธฐ์— ๋Œ€์‹  ์ž‘์„ฑํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
        
        /* ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์‹คํ–‰๋ฌธ ์ž‘์„ฑ */
        Runnable task = () -> {
            
           // Thread๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—…...            
           
           // ์“ฐ๋ ˆ๋“œ ๋น„๊ต๋ฅผ ์œ„ํ•œ 
           for(int i = 0; i < 5; i++) {
				
                // ์“ฐ๋ ˆ๋“œ ์ด๋ฆ„ ์ถœ๋ ฅ           		
               System.out.print(Tread.currentThread().getName());
           }
           
        };

		// Thread ๊ฐ์ฒด ์ƒ์„ฑ, Runnable ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ 
        Thread thread1 = new Thread(task);
        // thread ์ด๋ฆ„ ์„ค์ •
        thread1.setName("thread1");
        
        Thread thread2 = new Thread(task);
        thread2.setName("thread2");
		
        // ์“ฐ๋ ˆ๋“œ ์‹คํ–‰
        thread1.start();
        thread2.start();
    }
}

์ด ์˜ˆ์ œ์—๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค. ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ์“ฐ๋ ˆ๋“œ ์ด๋ฆ„์ด ๋ฒˆ๊ฐˆ์•„ ๊ฐ€๋ฉด์„œ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ถœ๋ ฅ๊ฒฐ๊ณผ (์‹คํ–‰ ์‹œ๋งˆ๋‹ค ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)
thread1
thread1
thread2
thread1
thread2
thread2
thread1
thread1
thread2
thread2

Daemon Thread(๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ)

๋ณด์ด์ง€ ์•Š๋Š” ๊ณณ(background)์—์„œ ์‹คํ–‰๋˜๋Š”, ๋‚ฎ์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งํ•œ๋‹ค. ๋ณด์กฐ์ ์ธ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋ฉฐ, ๋Œ€ํ‘œ์ ์ธ ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ๋กœ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์„ ์ •๋ฆฌํ•ด์ฃผ๋Š” ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ(Garbage Collector, GC)๊ฐ€ ์žˆ๋‹ค.

๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •๋„ ์œ„์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์“ฐ๋ ˆ๋“œ ๊ตฌํ˜„ ์˜ˆ์ œ๋“ค๊ณผ ์œ ์‚ฌํ•˜๋‹ค. Runnable ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ์“ฐ๋ ˆ๋“œ์—์„œ ์‹คํ–‰ํ•  ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ , Thread ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ Thread.setDaemon({Boolean}) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•˜๊ณ , ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

public static void main(String[] args) {
	
    // ๋Ÿฌ๋„ˆ๋ธ” ์ƒ์„ฑ ๋ฐ ๊ตฌํ˜„
    Runnable daemon = () -> {
    	
        for(int i = 0; i < 100; i++) {
        	System.out.println("demon: "+i);
        }
        
        // ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ ์ƒ์„ฑ ๋ฐ ๋Ÿฌ๋„ˆ๋ธ” ์ „๋‹ฌ
        Thread thread = new Thread(daemon);
        // ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ ์—ฌ๋ถ€ ์„ค์ •
        thread.setDaemon(true);
        
        // ์“ฐ๋ ˆ๋“œ ์‹คํ–‰
        thread.start();
        
        // ์šฐ์„ ์ˆœ์œ„ ๋น„๊ต๋ฅผ ์œ„ํ•œ for๋ฌธ ์‹คํ–‰
        for(int i = 0; i < 5; i++) {
        	System.out.println("task");            
        }
    }

๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์—, ์ƒ๋Œ€์ ์œผ๋กœ ๋‹ค๋ฅธ ์“ฐ๋ ˆ๋“œ๋“ค์— ๋น„ํ•ด ๋ฆฌ์†Œ์Šค๋ฅผ ์ ๊ฒŒ ํ• ๋‹น๋ฐ›๋Š”๋‹ค. ์‹ฌ์ง€์–ด ๋‹ค๋ฅธ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋ชจ๋‘ ์ข…๋ฃŒ๋˜๋ฉด, ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ๋„ ๊ฐ•์ œ ์ข…๋ฃŒ ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—, 100๋ฒˆ ์ถœ๋ ฅ๋˜์–ด์•ผํ–ˆ์„ "demon: "+i๋Š” ์ถœ๋ ฅ ๋„์ค‘์— ๋Š๊ฒจ๋ฒ„๋ฆฐ๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ (์‹คํ–‰ ์‹œ๋งˆ๋‹ค ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)
task
task
task
demon: 1
demon: 2
task
task
demon: 3

์‚ฌ์šฉ์ž ์“ฐ๋ ˆ๋“œ

๋ณด์ด๋Š” ๊ณณ(foregorund) ์—์„œ ์‹คํ–‰๋˜๋Š” ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์“ฐ๋ ˆ๋“œ๋กœ, ํ”„๋กœ๊ทธ๋žจ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์‚ฌ์šฉ์ž ์“ฐ๋ ˆ๋“œ๋กœ๋Š” ๋ฉ”์ธ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์žˆ๋‹ค. JVM์€ ์‚ฌ์šฉ์ž ์“ฐ๋ ˆ๋“œ์˜ ์ž‘์—…์ด ๋๋‚˜๋ฉด ๋ฐ๋ชฌ ์“ฐ๋ ˆ๋“œ๋„ ์ž๋™์œผ๋กœ ์ข…๋ฃŒ์‹œ์ผœ ๋ฒ„๋ฆฐ๋‹ค.

(์•ž์—์„œ ๊ตฌํ˜„ํ–ˆ๋˜ ๊ฒƒ๋“ค์ด ๋ชจ๋‘ ์‚ฌ์šฉ์ž ์“ฐ๋ ˆ๋“œ์ด๋‹ค.)

Thread Priority(์“ฐ๋ ˆ๋“œ ์šฐ์„ ์ˆœ์œ„)

์“ฐ๋ ˆ๋“œ๋Š” ์ƒ์„ฑ๋ ๋•Œ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ •ํ•ด์ง€์ง€๋งŒ, ์ค‘์š”๋„์— ๋”ฐ๋ผ์„œ ์“ฐ๋ ˆ๋“œ์— ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋†’๊ฒŒ ์ง€์ •ํ•˜๋ฉด ๋” ๋งŽ์€ ์ž‘์—…์‹œ๊ฐ„์„ ๋ถ€์—ฌ๋ฐ›์•„ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค๊ณ  ๋ฐ˜๋“œ์‹œ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋จผ์ € ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ž์›์„ ๋” ๋งŽ์ด ๋ถ€์—ฌ๋ฐ›์œผ๋‹ˆ, ๋จผ์ € ์ข…๋ฃŒ๋  ํ™•๋ฅ ์ด ๋†’์•„์ง€๋Š” ๊ฒƒ์ผ ๋ฟ์ด๋‹ค.

์šฐ์„ ์ˆœ์œ„๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐ€์ง€ (์ตœ๋Œ€/์ตœ์†Œ/๋ณดํ†ต) ์šฐ์„ ์ˆœ์œ„๋กœ ๋‚˜๋‰œ๋‹ค.

- ์ตœ๋Œ€ ์šฐ์„ ์ˆœ์œ„ (MAX_PRIORITY) = 10
- ์ตœ์†Œ ์šฐ์„ ์ˆœ์œ„ (MIN_PRIORITY) = 1
- ๋ณดํ†ต ์šฐ์„ ์ˆœ์œ„ (NROM_PRIORITY) = 5 (๊ธฐ๋ณธ๊ฐ’)

์šฐ์„ ์ˆœ์œ„๋Š” setPriority() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

Thread thread1 = new Thread(task1);
thread1.setPriority(8);

getPriority() ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

int threadPriority = thread1.getPriority();
System.out.println("threadPriority = " + threadPriority);

Thread Group(์“ฐ๋ ˆ๋“œ ๊ทธ๋ฃน)

์„œ๋กœ ๊ด€๋ จ์ด ์žˆ๋Š” ์“ฐ๋ ˆ๋“œ๋“ค์€ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. ์“ฐ๋ ˆ๋“œ๋“ค์€ ๋ชจ๋‘ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ทธ๋ฃน์— ํฌํ•จ๋˜์–ด์žˆ๋Š”๋ฐ, JVM์ด ์‹คํ–‰๋˜๋ฉด system ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋˜๊ณ , ์“ฐ๋ ˆ๋“œ๋“ค์€ ๊ธฐ๋ณธ์ ์œผ๋กœ system ๊ทธ๋ฃน์— ํฌํ•จ๋œ๋‹ค. ๋ชจ๋“  ์“ฐ๋ ˆ๋“œ๋“ค์€ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์— ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๏ผŠ ์“ฐ๋ ˆ๋“œ ๊ทธ๋ฃน์„ ์ง€์ •๋ฐ›์ง€ ๋ชปํ•œ ์“ฐ๋ ˆ๋“œ๋Š” ์ž์‹ ์„ ์ƒ์„ฑํ•œ ๋ถ€๋ชจ ์“ฐ๋ ˆ๋“œ์˜ ๊ทธ๋ฃน๊ณผ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ๋˜๋Š”๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ์“ฐ๋ ˆ๋“œ๋“ค์€ main ์“ฐ๋ ˆ๋“œ ํ•˜์œ„์— ํฌํ•จ๋œ๋‹ค.

์“ฐ๋ ˆ๋“œ ๊ทธ๋ฃน ์ƒ์„ฑ

ThreadGroup ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , Thread ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์— ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค.

/* 1. ThreadGroup ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. */
ThreadGroup group1 = new ThreadGroup("Group1");

/* 2. Thread ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค. */
// Thread(ThreadGroup group, Runnable target, String name)
Thread thread1 = new Thread(group1, task, "Thread 1");

/* 3. Thread์— ThreadGroup์ด ํ• ๋‹น๋œ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. */
System.out.println("Group of thread1 : " + thread1.getThreadGroup().getName());

์“ฐ๋ ˆ๋“œ ๊ทธ๋ฃน ๊ด€๋ฆฌ

[์‹คํ–‰๋Œ€๊ธฐ ์ƒํƒœ]
ThreadGroup ๊ฐ์ฒด์˜ interrupt() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด, ํ•ด๋‹น ๊ทธ๋ฃน ์“ฐ๋ ˆ๋“œ๋“ค์ด ์‹คํ–‰๋Œ€๊ธฐ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

/* 1. ThreadGroup ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. */
ThreadGroup group1 = new ThreadGroup("Group1");

/* 2. Thread ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์ค€๋‹ค. */
// Thread(ThreadGroup group, Runnable target, String name)
Thread thread1 = new Thread(group1, task, "Thread 1");
Thread thread2 = new Thread(group1, task, "Thread 2");

/* 3. interrupt()๋Š” ์ผ์‹œ์ •์ง€ ์ƒํƒœ์ธ ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰๋Œ€๊ธฐ ์ƒํƒœ๋กœ ๋งŒ๋“ ๋‹ค. */
group1.interrupt();
profile
`ISFJ` T 49% F 51% /

0๊ฐœ์˜ ๋Œ“๊ธ€