πŸ“– Java&Spring/λͺ¨λ˜μžλ°”μΈμ•‘μ…˜

[λͺ¨λ˜μžλ°”μΈμ•‘μ…˜] λ™μž‘ νŒŒλΌλ―Έν„°(Behavior Parameter)

bell22 2023. 11. 19. 13:15

 

 

λ™μž‘ νŒŒλΌλ―Έν„°λ₯Ό μ•„μ‹­λ‹ˆκΉŒ?

 

ν”„λ‘œκ·Έλž˜λ° μ‹œ μ‚¬μš©ν•˜λŠ” "λ™μž‘"은 μ•„λž˜μ™€ 같은 것을 μ˜λ―Έν•©λ‹ˆλ‹€.

a와 bλ₯Ό κ³±ν•˜λŠ” ν•¨μˆ˜κ°€ μžˆλ‹€κ³  ν•΄λ΄…μ‹œλ‹€.

 

def multiply_numbers(a, b):
    result = a * b
    return result

 

 

이 ν•¨μˆ˜λŠ” νŒŒλΌλ―Έν„°λ‘œ 받은 κ³±ν•˜λŠ” "λ™μž‘"을 ν•˜λŠ”λ°μš©, 

a와 b의 값에 λ”°λΌμ„œ multiply_numbers ν•¨μˆ˜μ˜ κ²°κ³Ό 값은 λ‹¬λΌμ§€κ²Œ λ©λ‹ˆλ‹€.

μ΄λ•Œ, a와 bλ₯Ό λ™μž‘ νŒŒλΌλ―Έν„°λΌκ³  ν•˜λŠ” κ±°μž–μŠ΄~

 

result1 = multiply_numbers(2, 3)  # a=2, b=3을 μ „λ‹¬ν•˜μ—¬ 2 * 3 = 6이 λ°˜ν™˜λ¨
result2 = multiply_numbers(5, 7)  # a=5, b=7을 μ „λ‹¬ν•˜μ—¬ 5 * 7 = 35κ°€ λ°˜ν™˜λ¨

 

μ΄λ ‡κ²Œ 말이져.

a와 b의 값에 따라 result 값이 λ‹¬λΌμ§€κ²Œ λ©λ‹ˆλ‹€.

 

즉, λ™μž‘ νŒŒλΌλ―Έν„°λž€ 

ν•¨μˆ˜λ‚˜ λ©”μ„œλ“œμ˜ 호좜 μ‹œ μ „λ‹¬λ˜λŠ” κ°’μœΌλ‘œ,

ν•΄λ‹Ή ν•¨μˆ˜ λ˜λŠ” λ©”μ†Œλ“œμ˜ λ™μž‘μ„ μ œμ–΄ν•˜κ±°λ‚˜ μ‘°μ ˆν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.

 

그러면 λ™μž‘ νŒŒλΌλ―Έν„°ν™”κ°€ 무엇인지, 

Java 8의 λ™μž‘ νŒŒλΌλ―Έν„°ν™”λ‘œ 무엇을 ν•  수 μžˆλŠ”μ§€!!

Modern Java In Action 책을 참고둜 정리해보렀고 ν•©λ‹ˆλ‹€


 

μš°λ¦¬κ°€ κ°œλ°œμ„ ν•  λ•Œ κ³ λ―Όν•˜λŠ” λ‚΄μš©μ€ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

 

πŸ— μœ μ§€λ³΄μˆ˜κ°€ μ‰¬μ›Œμ•Ό ν•˜κ³  μƒˆλ‘œμš΄ κΈ°λŠ₯이 좔가될 λ•Œ μ‰½κ²Œ κ΅¬ν˜„μ΄ λ˜μ–΄μ•Ό ν•œλ‹€.

πŸ— λ³€κ²½λ˜λŠ” μš”κ΅¬μ‚¬ν•­μ— 효과적으둜 λŒ€μ‘ν•΄μ•Ό ν•  방법이 ν•„μš”ν•˜λ‹€.

 

λ™μž‘ νŒŒλΌλ―Έν„°ν™”

아직 μ–΄λ–»κ²Œ 싀행될지 κ²°μ •λ˜μ§€ μ•Šμ€ μ½”λ“œ 블둝.

μ½”λ“œ 블둝에 따라 λ©”μ„œλ“œ λ™μž‘μ΄ νŒŒλΌλ―Έν„°ν™” λœλ‹€.

 

첫 번째 μ‹œλ„: 녹색 사과 필터링

public List<Apple> filterGreenApples(List<Apple> inventory) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if(Color.GREEN.equals(apple.getColor())) {
                    result.add(apple);
                }
            }
            return result;
        }

        public class Apple {
            private final Color color;
            Apple(Color color) {
                this.color = color;
            }
            public Color getColor() { return this.color;}
        }

 

녹색 사과λ₯Ό ν•„ν„°λ§ν•˜κ³  μ‹Άλ‹€~λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

νŒŒλΌλ―Έν„°λ‘œ 받은 inventoryμ—μ„œ κΊΌλ‚Έ μ‚¬κ³Όμ˜ 색상과 GREEN인지 비ꡐλ₯Ό ν•˜κ³  μžˆλ‹€.

λ§Œμ•½ λΉ¨κ°„ 사과λ₯Ό ν•„ν„°λ§ν•˜κ³  μ‹Άλ‹€λ©΄? 색상이 3000개 λŠ˜μ–΄λ‚œλ‹€λ©΄?

 

좔상화λ₯Ό ν•΄λ³΄μž

 

두 번째 μ‹œλ„: 색을 νŒŒλΌλ―Έν„°ν™”

        @Test
        @DisplayName("녹색 사과 필터링")
        void filterGreenApples() {
            List<Apple> inventory = new ArrayList<>();
            inventory.add(new Apple(Color.GREEN));
            inventory.add(new Apple(Color.GREEN));
            inventory.add(new Apple(Color.RED));
            inventory.add(new Apple(Color.RED));
            inventory.add(new Apple(Color.GREEN));
            inventory.add(new Apple(Color.GREEN));

            // 첫 번째 μ‹œλ„
            // assertEquals(4, filterGreenApples(inventory).size());
            
            // 두 번째 μ‹œλ„
            assertEquals(2, filterGreenApples(inventory, Color.RED).size());
        }

        public List<Apple> filterGreenApples(List<Apple> inventory, Color color) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if(color.equals(apple.getColor())) { // color.GREEN λŒ€μ‹  color νŒŒλΌλ―Έν„°μ™€ 비ꡐ!
                    result.add(apple);
                }
            }
            return result;
        }

        public class Apple {
            private final Color color;
            Apple(Color color) {
                this.color = color;
            }
            public Color getColor() { return this.color;}
        }

 

νŒŒλΌλ―Έν„°λ‘œ 색상을 받도둝 λ³€κ²½ν–ˆλ‹€

λ§Œμ•½ μƒ‰μƒλΏλ§Œ μ•„λ‹ˆλΌ μ‚¬κ³Όμ˜ λ¬΄κ²Œλ„ 필터링을 ν•˜κ³  μ‹Άλ‹€λ©΄?

 

public List<Apple> filterGreenApplesByWeight(List<Apple> inventory, int weight) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if(apple.getWeight() > weight) {
                    result.add(apple);
                }
            }
            return result;
        }

 

무게λ₯Ό ν•„ν„°λ§ν•˜λŠ” ν•¨μˆ˜κ°€ 좔가됐닀

inventoryμ—μ„œ 사과λ₯Ό κΊΌλ‚΄μ„œ, λ°˜λ³΅λ¬Έμ„ 돌며  색상을 ν•„ν„°λ§ν•˜λŠ” 뢀뢄이 μ€‘λ³΅λœλ‹€.

 

DRY (don't repeat yourself) 원칙을 μ–΄κΈ‹λ‚œ μ½”λ“œμž–μŠ΄~

 

 

μ„Έ 번째 μ‹œλ„: κ°€λŠ₯ν•œ λͺ¨λ“  경우의 수둜 ν•„ν„°λ§ν•œλ‹€

public List<Apple> filterApples(List<Apple> inventory, Color color, int weight, boolean flag) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if( (flag && apple.getColor().equals(color)) ||
                        (!flag && apple.getWeight() > weight)) {
                    result.add(apple);
                }
            }
            return result;
        }

 

flagκ°€ λ‚œλ°μ—†μ΄ λ“±μž₯

μ±…μ—μ„œλ„ ν˜•νŽΈμ—†λŠ” μ½”λ“œλΌκ³  ν•΄μ„œ 웃겼닀

 

μ–΄λ–€ κΈ°μ€€μœΌλ‘œ 사과λ₯Ό 필터링할 건지 효과적으둜 μ „λ‹¬ν•˜λŠ” 방법이 ν•„μš”ν•˜λ‹€

→ λ™μž‘ νŒŒλΌλ―Έν„°ν™”λ‘œ μœ μ—°μ„±μ„ μ–»μž.

 

public interface ApplePredicate {
            boolean test (Apple apple);
        }

 

μ–΄λ–€ 속성에 κΈ°μ΄ˆν•΄μ„œ μ°Έκ³Ό 거짓을 λ°˜ν™˜ν•˜λŠ” ν”„λ ˆλ””μΌ€μ΄νŠΈ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ„±ν–ˆλ‹€.

μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†λ°›μ•„μ„œ κΈ°λŠ₯을 뢄리해 보자.

 


PredicateλŠ” 주둜 쑰건을 κ²€μ‚¬ν•˜μ—¬ μ°Έ λ˜λŠ” 거짓을 νŒλ‹¨ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€.

 

 

        
        public class AppleHeavyWeightPredicate implements ApplePredicate {

            @Override
            public boolean test(Apple apple) {
                return false;
            }
        }
        public class AppleGreenColorPredicate implements ApplePredicate {

            @Override
            public boolean test(Apple apple) {
                return false;
            }
        }

 

μ „λž΅ λ””μžμΈ νŒ¨ν„΄μ„ μ‚¬μš©ν•œ μ˜ˆμ œμ΄λ‹€.

 

ApplePredicate이 → μ•Œκ³ λ¦¬μ¦˜ νŒ¨λ°€λ¦¬

상속받은 애듀이 →  μ „λž΅μ΄ λ˜λŠ” 것이닀.

 

μœ„μ—μ„œ λ³Έ ν•¨μˆ˜ filterApples() μ—μ„œ 객체λ₯Ό λ°›μ•„μ„œ 쑰건을 κ²€μ‚¬ν•˜λ„λ‘ λ°”κΎΈλ©΄ λ˜μž–μŠ΄~

λ©”μ„œλ“œκ°€ λ‹€μ–‘ν•œ λ™μž‘μ„ ν•˜κ²Œ λœλ‹€ → 이게 λ™μž‘ νŒŒλΌλ―Έν„°ν™”μž–μŠ΄~

 


μ „λž΅ λ””μžμΈ νŒ¨ν„΄

μΊ‘μŠν™”ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜ νŒ¨λ°€λ¦¬λ₯Ό μ •μ˜ν•˜κ³  λŸ°νƒ€μž„ μ‹œ μ•Œκ³ λ¦¬μ¦˜μ΄ 선택

 

 

λ„€ 번째 μ‹œλ„: 좔상적 쑰건으둜 필터링

public List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if(applePredicate.test(apple))
                    result.add(apple);
            }
            return result;
        }

 

Predicate 객체둜 사과 검사 쑰건을 μΊ‘μŠν™”ν•œ μ˜ˆμ œμ΄λ‹€.

μ „λ‹¬ν•œ ApplePredicate 객체에 μ˜ν•΄ ν•¨μˆ˜μ˜ λ™μž‘μ΄ κ²°μ •λœλ‹€.

λ©”μ„œλ“œμ˜ λ™μž‘μ΄ νŒŒλΌλ―Έν„°ν™” λ˜μ—ˆλ‹€λŠ” 것이닀.

...

근데 λ§Œμ•½ λΉ¨κ°„ 무거운 사과λ₯Ό ν•„ν„°λ§ν•˜κ³  μ‹Άλ‹€λ©΄?

 

public class AppleRedHeavyPredicate implements ApplePredicate {

            @Override
            public boolean test(Apple apple) {
                /**
                 * 둜직
                 */
                return false;
            }
        }

 

이런 ν΄λž˜μŠ€λ“€μ΄ κ³„μ†ν•΄μ„œ μΆ”κ°€κ°€ 될 것이닀.

쀑볡을 μ–΄λ–»κ²Œ 쀄이고 λ³΅μž‘ν•˜μ§€ μ•Šκ²Œ λ§Œλ“€ 수 μžˆμ„κΉŒ?λ₯Ό κ³ λ―Όν•΄μ•Ό ν•œλ‹€.

 


λ³΅μž‘ν•œ κ³Όμ • κ°„μ†Œν™”ν•΄ 보자

κ°„μ†Œν™”ν•˜κΈ° μœ„ν•œ λ°©λ²•μœΌλ‘œ 읡λͺ… ν΄λž˜μŠ€μ™€ λžŒλ‹€κ°€ μžˆλ‹€.

그쀑, 읡λͺ… ν΄λž˜μŠ€λŠ” μ•„λž˜μ™€ 같은 의미λ₯Ό 가진닀.


읡λͺ… 클래슀 ( Anonymous Class)

 

μ½”λ“œμ˜ 양을 쀄일 수 μžˆλ‹€

μžλ°”μ˜ 지역 ν΄λž˜μŠ€μ™€ λΉ„μŠ·ν•œ κ°œλ…μœΌλ‘œ, 이름이 μ—†λŠ” 읡λͺ…인 ν΄λž˜μŠ€λž€ λœ»μ΄λ‹€

클래슀 μ„ μ–Έκ³Ό μΈμŠ€ν„΄μŠ€ν™”λ₯Ό λ™μ‹œμ— ν•  수 μžˆλ‹€

 

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // λ²„νŠΌμ΄ ν΄λ¦­λ˜μ—ˆμ„ λ•Œ μˆ˜ν–‰λ˜λŠ” μ½”λ“œ
    }
});

 

λ²„νŠΌμ΄ λˆŒλ Έμ„ λ•Œ 싀행될 μ½”λ“œλ₯Ό μ •μ˜ν•˜μ˜€λ‹€.

λ³„λ„μ˜ 클래슀 이름은 갖지 μ•ŠλŠ”λ‹€.

 

 

λ‹€μ„― 번째 μ‹œλ„: 읡λͺ… 클래슀

 

읡λͺ… 클래슀λ₯Ό μ΄μš©ν•΄μ„œ λ‹€μ‹œ κ΅¬ν˜„

λ©”μ„œλ“œ λ™μž‘μ„ 읡λͺ… 클래슀둜 λ§Œλ“€μ–΄μ„œ 직접 νŒŒλΌλ―Έν„°ν™” ν–ˆλ‹€.

 

읡λͺ… 클래슀둜 μž‘μ„±ν•΄ μ£Όλ©΄, μΈν…”λ¦¬μ œμ΄μ—μ„œ λžŒλ‹€λ₯Ό μΆ”μ²œν•΄ μ€€λ‹€.

μΉœμ ˆν•΄ πŸŽ„

 

μ—¬μ„― 번째 μ‹œλ„: λžŒλ‹€ ν‘œν˜„μ‹

List<Apple> greenApples = filterApples(inventory, apple -> false);

 

맀우 κΉ”κΌΌ..

λžŒλ‹€λŠ” λ‹€μŒ κΈ€μ—μ„œ λ‹€μ‹œ μžμ„Ένžˆ 닀루겠닀.

 

일곱 번째 μ‹œλ„: 리슀트 ν˜•μ‹μœΌλ‘œ 좔상화

void filterList() {
            List<Apple> inventory = new ArrayList<>();
            List<Apple> redApples = filter(inventory, (Apple apple) -> Color.RED.equals(apple.getColor()));
        }
        
        public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
            List<T> result = new ArrayList<>();
            for (T t : list) {
                if(predicate.test(t)) {
                    result.add(t);
                }
            }
            return result;
        }

 

ν˜•μ‹ νŒŒλΌλ―Έν„° T둜 클래슀λ₯Ό 생성할 μˆ˜λ„ μžˆλ‹€.

ν•„ν„° λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬, μ‚¬κ³ΌλΏλ§Œ μ•„λ‹ˆλΌ λ°”λ‚˜λ‚˜, μ˜€λ Œμ§€ 등등에 μ‚¬μš©ν•  수 μžˆλ‹€.


μ‹€μ „ 예제

κ·Έλ ‡λ‹€λ©΄ λ™μž‘ νŒŒλΌλ―Έν„°ν™”λ₯Ό λ‹€λ£¨λŠ” μ‹€μ „ 예제λ₯Ό 봐보자.

 

1) Comparator둜 μ •λ ¬ν•˜κΈ°


μžλ°” 8은 List에 sort λ©”μ„œλ“œκ°€ ν¬ν•¨λ˜μ–΄ μžˆλ‹€

Collections.sortλŠ” λ¬Όλ‘  있음

 

 

java.util.Comparator 객체λ₯Ό μ΄μš©ν•΄μ„œ sort λ™μž‘μ„ νŒŒλΌλ―Έν„°ν™” ν•  수 μžˆλ‹€

 

List<Apple> inventory = new ArrayList<>();
            List<Apple> redApples = filter(inventory, (Apple apple) -> Color.RED.equals(apple.getColor()));

            inventory.sort(new Comparator<Apple>() {
                @Override
                public int compare(Apple o1, Apple o2) {
                    return o1.getWeight() > o2.getWeight() ? o1.getWeight() : o2.getWeight();
                }
            });

 

읡λͺ… 클래슀λ₯Ό μ‚¬μš©ν•œ μ˜ˆμ œμ΄λ‹€.

 

λ§Œμ•½ λ†λΆ€μ˜ μš”κ΅¬μ‚¬ν•­μ΄ λ°”λ€Œλ©΄

μƒˆλ‘œμš΄ Comparatorλ₯Ό λ§Œλ“€μ–΄μ„œ sort λ©”μ„œλ“œμ— 전달해 μ£Όλ©΄ λœλ‹€.

 

inventory.sort((o1, o2) -> o1.getWeight() > o2.getWeight() ? o1.getWeight() : o2.getWeight());

 

λžŒλ‹€λ‘œ λ°”κΎΈλ©΄ μ΄λ ‡κ²Œ ν•  수 μžˆλ‹€.

 

 

2) Runnable둜 μ½”λ“œ 블둝 μ‹€ν–‰ν•˜κΈ°

또 λ‹€λ₯Έ 예제λ₯Ό 봐보자.

 

μžλ°” μŠ€λ ˆλ“œλ₯Ό μ΄μš©ν•˜λ©΄ Runnable둜 μ½”λ“œ 블둝을 λ³‘λ ¬λ‘œ μ‹€ν–‰ν•  수 μžˆλ‹€.

μ—¬λŸ¬ μŠ€λ ˆλ“œκ°€ 각자 λ‹€λ₯Έ μ½”λ“œλ₯Ό μ‹€ν–‰ν•  수 μžˆλ‹€λŠ” λœ»μ΄λ‹€.

 

μžλ°” 8 μ΄μ „κΉŒμ§€λŠ” Thread μƒμ„±μžμ— κ°μ²΄λ§Œμ„ 전달할 수 μžˆμœΌλ―€λ‘œ

보톡 κ²°κ³Όλ₯Ό λ°˜ν™˜ μ•ˆ ν•˜λŠ” void run 같은 λ©”μ„œλ“œλ₯Ό ν¬ν•¨ν•˜λŠ” 읡λͺ… ν΄λž˜μŠ€κ°€

Runnable μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λ„λ‘ ν•˜λŠ” 게 μΌλ°˜μ μ΄μ—ˆλ‹€κ³  ν•œλ‹€.

 

void threadTest() {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("FilterGreenApples.run");
                }
            });
        }

 

λžŒλ‹€λ‘œ λ°”κΎΈλ©΄

void threadTest() {
            Thread thread = new Thread(() -> System.out.println("FilterGreenApples.run"));
        }

 

 

3) Callableλ₯Ό 결과둜 λ°˜ν™˜ν•˜κΈ°


ExecutorService (μžλ°” 5λΆ€ν„° 지원.)

ExecutorServiceλŠ” μŠ€λ ˆλ“œ 풀을 κ΄€λ¦¬ν•˜κ³  μŠ€λ ˆλ“œ μž‘μ—…μ„ μ‹€ν–‰ν•˜λŠ” 데 μ‚¬μš©λ˜λŠ” μΈν„°νŽ˜μ΄μŠ€λ‹€.

μŠ€λ ˆλ“œ 관리 및 μž‘μ—… μŠ€μΌ€μ€„λ§μ„ μΆ”μƒν™”ν•˜κ³  효과적으둜 λ‹€λ£° 수 μžˆλ„λ‘ λ„μ™€μ€Œ.

 

ExecutorServiceμ—λŠ” λͺ‡ 가지 좔상화 κ°œλ…μ΄ 있음.

 

1) μŠ€λ ˆλ“œ ν’€

ExecutorServiceλŠ” μŠ€λ ˆλ“œ 풀을 μ‚¬μš©ν•˜μ—¬ μž‘μ—…μ„ μ²˜λ¦¬ν•¨.

미리 μŠ€λ ˆλ“œ ν’€ 생성해 놓고, μž‘μ—…μ„ λΆ„λ°°ν•΄μ„œ μŠ€λ ˆλ“œλ₯Ό μž¬μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•΄μ€Œ.

μŠ€λ ˆλ“œμ˜ 생성 및 μ†Œλ©Έμ— λ”°λ₯Έ μ˜€λ²„ν—€λ“œλ₯Ό κ°μ†Œμ‹œν‚€κ³  νš¨μœ¨μ„±μ„ λ†’μ—¬μ€Œ.

 

2) μž‘μ—… 큐

μž‘μ—…μ„ 큐에 넣어두고 μŠ€λ ˆλ“œ 풀이 이λ₯Ό κΊΌλ‚΄μ–΄ 싀행함.

μž‘μ—… νλŠ” μŠ€λ ˆλ“œκ°€ μ‚¬μš© κ°€λŠ₯ν•  λ•Œλ§ˆλ‹€ μž‘μ—…μ„ κΊΌλ‚΄μ–΄ μ‹€ν–‰ν•˜κ²Œ 됨.

이러면 뭐가 μ’‹μœΌλƒ? μ—¬λŸ¬ μž‘μ—…μ„ λΉ„λ™κΈ°μ μœΌλ‘œ μ²˜λ¦¬ν•  수 μžˆλ‹€~

 

3) μž‘μ—… μ‹€ν–‰κ³Ό μ’…λ£Œ

ExecutorServiceλŠ” submit() λ©”μ„œλ“œλ₯Ό 톡해 μž‘μ—…μ„ μ œμΆœν•˜κ³ ,

μž‘μ—…μ΄ μ™„λ£Œλ  λ•ŒκΉŒμ§€ κΈ°λ‹€λ¦¬κ±°λ‚˜ κ²°κ³Όλ₯Ό 받을 수 μžˆλŠ” Future 객체λ₯Ό λ°˜ν™˜ν•¨.

μ’…λ£ŒλŠ” shutdown() λ˜λŠ” shutdownNow() λ©”μ„œλ“œλ‘œ ν•œλ‹€.

 

→ Runnable 방식과 λ‹€λ₯Έ 점이당

 

4) μŠ€λ ˆλ“œ 생λͺ…μ£ΌκΈ° 관리

ExecutorServiceλŠ” μŠ€λ ˆλ“œ 생λͺ…μ£ΌκΈ°λ₯Ό μžλ™μœΌλ‘œ κ΄€λ¦¬κΉŒμ§€ ν•œλ‹€;;

μŠ€λ ˆλ“œλ₯Ό μƒμ„±ν•˜κ³  μ’…λ£Œν•˜λ©°, μ˜ˆμ™Έκ°€ λ°œμƒν•œ κ²½μš°μ—λ„ 적절히 μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

void callableTest() {
            ExecutorService executorService = Executors.newCachedThreadPool();
            Future<String> future = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return Thread.currentThread().getName();
                }
            });
        }

 

ν‚Ήλ‹€λ‘œ λ°”κΏ”μ£Όλ©΄?

 

void callableTest() {
            ExecutorService executorService = Executors.newCachedThreadPool();
            Future<String> future = executorService.submit(() -> Thread.currentThread().getName());
        }

 

μ΄λŸ¬ν•˜λ‹€.


 

κ²°λ‘ 

 

λ™μž‘ νŒŒλΌλ―Έν„°ν™”μ—μ„œλŠ” μ½”λ“œλ₯Ό λ©”μ„œλ“œ 인수둜 μ „λ‹¬ν•œλ‹€.

λ©”μ„œλ“œ λ‚΄λΆ€μ μœΌλ‘œ λ‹€μ–‘ν•œ λ™μž‘μ„ μˆ˜ν–‰ν•  수 μžˆλ„λ‘ ν•˜κΈ° μœ„ν•΄μ„œμ΄λ‹€.

μš”κ΅¬μ‚¬ν•­μ— 잘 λŒ€μ‘ν•˜μ—¬ μ—”μ§€λ‹ˆμ–΄λ§ λΉ„μš© κ°μ†Œν•˜μž.

μžλ°” 8둜 였며, 읡λͺ… ν΄λž˜μŠ€μ™€ λžŒλ‹€ 등을 μ΄μš©ν•΄μ„œ μ½”λ“œλ₯Ό κΉ”λ”ν•˜κ²Œ λ°”κΏ€ 수 μžˆλ‹€!!

μ •λ ¬, μŠ€λ ˆλ“œ, GUI 처리 등을 λ‹€μ–‘ν•œ λ™μž‘μœΌλ‘œ νŒŒλΌλ―Έν„°ν™” ν•  수 μžˆλ‹€.