์ „์ฒด ๊ธ€

์ „์ฒด ๊ธ€

    [PGM_42890] ํ›„๋ณดํ‚ค (java)

    [PGM_42890] ํ›„๋ณดํ‚ค (java)

    ๋ฌธ์ œ๋งํฌ https://programmers.co.kr/learn/courses/30/lessons/42890 ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ›„๋ณดํ‚ค [["100","ryan","music","2"],["200","apeach","math","2"],["300","tube","computer","3"],["400","con","computer","4"],["500","muzi","music","3"],["600","apeach","music","2"]] 2 programmers.co.kr ๋ฌธ์ œํ’€์ด ๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป ํ•ต์‹ฌ ์Šคํ‚ฌ ๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป ์™„์ „ํƒ์ƒ‰ 1. ๋ฌธ์ œ ์ดํ•ด DB์— ๋Œ€ํ•œ ๋ฆด๋ ˆ์ด์…˜์ด ์ฃผ์–ด์ง€๋ฉด ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ํ›„๋ณดํ‚ค๊ฐ€ ์ด ๋ช‡๊ฐœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์ฐพ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค. 2. ์ ‘๊ทผ๋ฐฉ๋ฒ• 1) ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ‚ค๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์„ ๊ตฌํ•ด์ค€๋‹ค. ..

    ์ž๋ฐ” ์“ฐ๋ ˆ๋“œ Callable๊ณผ Future

    ์ž๋ฐ” ์“ฐ๋ ˆ๋“œ Callable๊ณผ Future

    Callable๊ณผ Future Runnable๋กœ ๋™์ž‘์„ ์‹คํ–‰ํ•  ๋•Œ return๋ฐ›๋Š” ๊ฐ’์ด ์—†๋‹ค. ๋”ฐ๋ผ์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋กœ ๊ฐ’์„ ์–ป์–ด์•ผํ•œ๋‹ค๋ฉด Runnable์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค. Callable์€ ์ œ๋„ˆ๋ฆญํƒ€์ž…์„ ๋ฆฌํ„ดํƒ€์ž…์œผ๋กœ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— returnํƒ€์ž…์ด ํ•„์š”ํ•˜๋‹ค๋ฉด Callable์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. Future๋Š” ์ด๋ ‡๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๋น„๋™๊ธฐ ์ž‘์—…์˜ ํ˜„์žฌ์ƒํƒœ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ExecutorService ex = Executors.newFixedThreadPool(3); Callable hello = () -> { Thread.sleep(5000L); return "Hello"; }; Future future = ex.submit(hello); System.out.prin..

    ์ž๋ฐ” Excuter๋กœ ์“ฐ๋ ˆ๋“œ ์ฒ˜๋ฆฌํ•˜๊ธฐ

    ์ž๋ฐ” Excuter๋กœ ์“ฐ๋ ˆ๋“œ ์ฒ˜๋ฆฌํ•˜๊ธฐ

    Excutors ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์‹ค์ƒ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ Executor๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์“ฐ๋ ˆ๋“œ๋ฅผ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. Executer๋Š” ExcutorServie์™€ ScheduledExecutorService๊ฐ€ ์žˆ๋‹ค. ExcutorService๋Š” ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๋™์ž‘ํ•˜๋Š” ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด, ScheduledExecutorService๋Š” ์“ฐ๋ ˆ๋“œ๋“ค์„ ๊ฐ์ž ์–ผ๋งˆ๋‚˜ ๋’ค์— ์‹คํ–‰ํ• ์ง€๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์–ด, ์Šค์ผ€์ฅด๋งํ•˜์—ฌ ์‹คํ–‰ํ•ด์ค€๋‹ค. ExecutorService //์‚ฌ์šฉ๋˜๋Š” runnable ๋ฉ”์†Œ๋“œ private static Runnable getRunnable(String message) { return () -> { System.out.println(message + ":" + Thread.cu..

    ์ž๋ฐ” ์“ฐ๋ ˆ๋“œ์˜ ๊ธฐ์ดˆ

    ์ž๋ฐ” ์“ฐ๋ ˆ๋“œ์˜ ๊ธฐ์ดˆ

    Thread ๊ธฐ์ดˆ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• Thread thread = new Thread(()->{ System.out.println("Hello Thread"); }); ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ new Thread()๋ฅผ ํ•  ๋•Œ Runnable ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋„ฃ์–ด์คŒ์œผ๋กœ์จ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ์“ฐ๋ ˆ๋“œ start thread.start(); ์“ฐ๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” start()๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์“ฐ๋ ˆ๋“œ sleep thread.sleep(1000); ์“ฐ๋ ˆ๋“œ์—์„œ ์ž ์‹œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋นผ์•—๊ธฐ ์œ„ํ•ด์„œ sleep์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์“ฐ๋ ˆ๋“œ๊ฐ€ ํ•˜๋˜ ์ผ์„ ์ž ์‹œ ๋ฉˆ์ถ”๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค๋ฅธ ์“ฐ๋ ˆ๋“œ์—์„œ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋œ๋‹ค. ์“ฐ๋ ˆ๋“œ interrupt Thread thread = new Thread(()->{ while (..