Thread

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

๐Ÿ“ŒThread ๊ฐ€๋ณด์ž๊ณ ~


๐Ÿ“Œ1์ดˆ ์ฃผ๊ธฐ๋กœ save() ๋ฉ”์†Œ๋“œ ํฌ์ถœํ•˜๋Š” ๋ฐ๋ชฌ ์Šค๋ ˆ๋“œ

๐Ÿ“Œ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ

๐Ÿ“Œ์‹คํ–‰ ๊ฒฐ๊ณผ


๐Ÿ“Œp.539 2๋ฒˆ๋ฌธ์ œ

๐Ÿ“ThreadExample.java


public class ThreadExample {


    public static void main(String[] args){
        Thread thread1 = new MovieThread();
        thread1.start();

        MusicRunnable musicRunnable = new MusicRunnable();
        Thread thread = new Thread(musicRunnable);
        thread.start();
    }
}

๋นˆ์นธ: Thread thread = new Thread(musicRunnable);

๐Ÿ“MovieThread.java


public class MovieThread extends Thread {
    @Override
    public void run(){
        for(int i = 0; i<3; i++){
            System.out.println("๋™์˜์ƒ์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.");
        }
        try{
            Thread.sleep( 1000 );
        } catch ( InterruptedException e ){

        }
    }
}

๋นˆ์นธ: extends Thread 

๐Ÿ“MusicRunnable.java

public class MusicRunnable implements Runnable{

    @Override
    public void run () {
        for(int i=0; i<3; i++){
            System.out.println("์Œ์•…์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.");
        }
        try{
            Thread.sleep( 1000 );
        }catch ( InterruptedException e ){

        }
    }
}


๋นˆ์นธ: implements Runnable

๐Ÿ“์‹คํ–‰๊ฒฐ๊ณผ

๋™์˜์ƒ์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.
์Œ์•…์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.
์Œ์•…์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.
๋™์˜์ƒ์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.
์Œ์•…์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.
๋™์˜์ƒ์„ ์žฌ์ƒํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“์–ด๊น€์—†์ด ์ฐพ์•„์˜จ ํ›„๊ธฐ~!@#
thread๋Š” ๊ฒŒ์ž„์ œ์ž‘ ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ์ž์„ธํžˆ ๋ชจ๋ฅด๊ณ  ์•„ ์—ฌ๋Ÿฌ๋™์ž‘์„ ํ•œ๋ฒˆ์— ํ•  ๋–„ ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•˜๋‚˜๋ณด๋‹ค ๋ผ๋Š” ์‹์œผ๋กœ๋งŒ ์•Œ๊ณ  ๋„˜์–ด๊ฐ”์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ๋ฅผ ํ†ตํ•ด์„œ ๋”์šฑ ์ž์„ธํžˆ thread์— ๋Œ€ํ•ด ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ์—ฌ์„œ ์ข‹์•˜๋‹ค!

๐Ÿ“ ์‹ฌ์‹ฌํ•˜๋‹ค ์‹ถ์œผ๋ฉด ์ฐพ์•„์˜ค๋Š” ๋‚˜์˜ ์‹ค์ˆ˜๋Š” ์—ฌ์ „ํ•˜์˜€๋‹ค..ใ…‹ใ…‹...
์•ž์—์„œ 1์ดˆ ์ฃผ๊ธฐ๋กœ save() ๋ฉ”์†Œ๋“œ ํฌ์ถœํ•˜๋Š” ๋ฐ๋ชฌ ์Šค๋ ˆ๋“œ ์ด ๋ฏธ์…˜์„ ์ˆ˜ํ–‰ํ•  ๋•Œ 2๊ฐ€์ง€ class๋ฅผ ์งœ๊ณ  ๋‚˜์„œ ๋งˆ์ง€๋ง‰ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ์ธ DeamonExample.java๋ฅผ ์‹คํ–‰ํ•˜์—ฌ์•ผ ํ•˜์ง€๋งŒ ๋˜ ํ•˜๋‚˜๋งŒ ์ž‘์„ฑํ•˜๊ณ  ์—๋Ÿฌ๋‚˜์„œ ๊ณ ๋ฏผ๊ณ ๋ฏผ ํ–ˆ๋‹ค๋Š”.... ์ฝ”๋“œ์•ผ ์†์•„ ๋จธ๋ฆฌ์•ผ ์ž˜ํ•˜์ž!!!

profile
fire-irror

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