티스토리 뷰

Java

[JAVA] Thread 반복, 람다식

Hwan'ss 2019. 7. 18. 16:55

Thread를 반복사용 예제

  • Spring의 3대 요소인 AOP 기법을 활용한 예제
  • AOP란? 핵심적인 기능에서 부가적인 기능을 분리해서 애스펙트라는 모듈로 만들어서 설계하고 개발하는 방법
  • 아래의 예제는 순차적으로 변형되어 진행되어 마지막에는 람다식으로 처리하는 예제 
  • 처음부터 순차적으로 어떻게 소스가 변경되었는지 이해하면서 공부를 하기 바람

 

1. Thread

  • 프로그램 내에서, 특히 프로세스 내에서 실행되는 흐름의 단위를 말함
  • Thread.sleep(2000)을 통해 2000만큼 실행을 제어하는 역할을 함
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Ex07 {
    public static void main(String[] args) {
        System.out.println(1);
        
        try {
            Thread.sleep(2000); // 잠시 block 코드, 이 문장은 try-catch와 사용해야 함
            System.out.println("호랑이");
        } catch (Exception e) {
 
        }
        System.out.println(2);
        
        System.out.println(1);
        
        try {
            Thread.sleep(2000);
            System.out.println("코끼리");
        } catch (Exception e) {
 
        }
        System.out.println(2);
        
        System.out.println(1);
        
        try {
            Thread.sleep(2000); // 잠시 block 코드, 이 문장은 try-catch와 사용해야 함
            System.out.println("독수리");
        } catch (Exception e) {
 
        }
        System.out.println(2);
    }
}
cs

2. try-chtch

  • 예외가 발생 했을 때 적절한 처리, 즉, 예외 처리를 하기 위해 사용하는 키워드
  • try-chtch문을 사용하면 예외가 발생하더라고 프로그램 실행을 계속 해서 할 수 있도록 함

3. throws

  • Exception을 발생시킴
  • throws 키워드를 사용하는 메서드를 호출한 상위 메서드에서 이러한 에러 처리에 대한 책임을 맡게 되는 것임 즉, 예외가 발생 했을 때 모든 책임은 그 메소드에 있다라고 해석하고 이해하면 좋음
  • 그렇기 때문에 아래와 같이 메소드에 throws Exception 처리를 해 주면 try-catch문을 사용하지 않아도 됨
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
interface T{
    void func01() throws Exception;
}
 
class Aa {
    void func02(T t) {
        System.out.println(1);
        try {
            t.func01();
        } catch (Exception e) {
        }
        System.out.println(2);
    }
    
    void func01() {
        func02(new T() {
            public void func01() throws Exception {
                Thread.sleep(2000); // 잠시 block 코드, 이 문장은 try-catch와 사용해야 함
                System.out.println("호랑이");
            }
        });
        func02(new T() {
            public void func01() throws Exception {
                Thread.sleep(2000); // 잠시 block 코드, 이 문장은 try-catch와 사용해야 함
                System.out.println("코끼리");
            }
        });
        func02(new T() {
            public void func01() throws Exception {
                Thread.sleep(2000); // 잠시 block 코드, 이 문장은 try-catch와 사용해야 함
                System.out.println("독수리");    
            }
        });
    }
}
 
public class Ex07 {
    public static void main(String[] args) {
        Aa a = new Aa();
        a.func01();
    }
}
cs

4. 람다식

  • 아래의 코드는 위 예제 func01() 메소드 안에 있는 func02() 메소드를 람다식으로 변환한 코드
  • 람다식에 대해서 이해가 가지 않는다면 이전 글인 람다식 글을 참고하길 바람

    https://hwan1001.tistory.com/3

 

[JAVA] 람다식-Lambda Expressions

1. 람다식(lambda expression) 나중에 실행될 목적으로 다른곳에 전달 될 수 있는 함수 이름이 없는 메소드라고 할 수 있음 즉, 람다식은 메소드를 객체로 취급할 수 있는 기능 가. 형식 (args1, args2...) -> { b..

hwan1001.tistory.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
interface T{
    void func01() throws Exception;
}
 
class Aa {
    void func02(T t) {
        System.out.println(1);
        try {
            t.func01();
        } catch (Exception e) {
        }
        System.out.println(2);
    }
    
    void func01() {
        func02(() -> {
                Thread.sleep(2000);
                System.out.println("호랑이");
        });
        func02( () -> {
                Thread.sleep(2000);
                System.out.println("코끼리");
        });
        func02( () -> {
                Thread.sleep(2000);
                System.out.println("독수리");
        });
    }
}
 
public class Ex07 {
    public static void main(String[] args) {
        Aa a = new Aa();
        a.func01();
    }
}
cs

 

2019.07.18(목) [Text05 Project]

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함