메뉴 건너뛰기

조회 수 104 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

  기본적인 쓰레드 사용


Thread를 상속받아 run() 메소드를 오버라이딩을 함으로써 간단한 Thread을 구현 해보겠습니다.

 

 
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
public class Thread1 extends Thread{
    int index ;
    public Thread1(int index)
    {
        this.index = index;
    }
    public void run()                            //실제로 Thread가 동작하는 내용을 구현합니다.
    {
        System.out.println(index + "번째 Thread Start");
        try
        {
            Thread.sleep(2000);         //단순히 2초동안 thread가 생성되었따가 종료됩니다.
        }catch(Exception e){}
        System.out.println(index + "번째 Thread End");
    }
}
public class Main {
    
    public static void main(String argsp[])
    {    
        Thread1 thread1 = new Thread1(1); //위에 구현한 클래스 객체 생성
        Thread1 thread2 = new Thread1(2);
        Thread1 thread3 = new Thread1(3);
        
        thread1.start();                           //start() 메서드를 통해 Thread 시작, run() 호출됨
        thread2.start();
        thread3.start();
    }
}
 

 

쓰레드 동작을 위해 Thread를 상속받는 Thread1 클래스를 구현합니다. thead의 인덱스를 넘겨받는 생성자와 쓰레드의 동작을 구현하는 run() 메서드를 오버라이딩 합니다.

Main 영역에서 Thread1 클래스의 객체를 생성하고 start() 메서드를 호출함으로써 쓰레드를 동작시킵니다.

 

 

■ join()을 통한 쓰레드 구현


위에 예제 실행 결과를 보면 첫 번째 쓰레드가 먼저 시작했음에도 불구하고 3번째 thread가 먼저 끝났다는 메세지가 먼저 뜹니다. 이것은 thread1 쓰레드를 실행하다가 실행흐름이 thread3로 옮겨지게 되면서 이런 경우가 발생합니다. 하지만 첫 번째 쓰레드가 끝나야지 2번쨰 쓰레드가 실행되고 2번째 쓰레드가 끝나야지 3번째 쓰레드를 실행시키고 싶을 경우가 있을 수 있습니다. 그럴때 사용하는데 join() 입니다.

 

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 Thread1 extends Thread{
    int index ;
    public Thread1(int index)
    {
        this.index = index;
    }
    public void run()
    {
        System.out.println(index + "번째 Thread Start");
        try
        {
            Thread.sleep(2000);
        }catch(Exception e){}
        System.out.println(index + "번째 Thread End");
    }
}
    
public class Main {
    
    public static void main(String argsp[]) throws InterruptedException
    {    
        Thread1 thread1 = new Thread1(1);
        Thread1 thread2 = new Thread1(2);
        Thread1 thread3 = new Thread1(3);
        
        thread1.start();
        thread1.join();     //thread1이 종료될때 까지 기다립니다.
        thread2.start();
        thread2.join();    //thread2가 종료될때까지 기다립니다.
        thread3.start();
    }
}
 

 

start() 호출 후에 join() 메서드를 호출하는 구조입니다. join은 해당 쓰레드가 종료 될 때까지 대기상태에 있도록 해줍니다.

 

 

■ Runnable 인터페이스


Thread 클래스를 상속받아 thread를 구현하는 방법도 있지만 Runnable 인터페이스를 구현하도록 하는 방법도 있습니다. 위에 예제를 Runnable 인터페이스를 구현하는 방법으로 수정 해보겠습니다.

  

 
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
public class Thread1 implements Runnable{
    int index ;
    public Thread1(int index)
    {
        this.index = index;
    }
    public void run()
    {
        System.out.println(index + "번째 Thread Start");
        try
        {
            Thread.sleep(2000);
        }catch(Exception e){}
        System.out.println(index + "번째 Thread End");
    }
}
public class Main {
    
    public static void main(String argsp[]) throws InterruptedException
    {    
        Thread thread1 = new Thread(new Thread1(1));
        Thread thread2 = new Thread(new Thread1(2));
        Thread thread3 = new Thread(new Thread1(3));
        
        thread1.start();
        thread2.start();
        thread3.start();
        
    }
}

Thread 상속받는 대신 Runnable 인터페이스를 구현함으로써 thread를 사용하는 방식입니다.

Main에서 Thread 생성 정보로 구현한 클래스의 객체를 넘겨줌으로써 객체를 생성하는 방식입니다.

 

■ Synchronized 키워드를 활용한 동기화


동기화란 여러 개의 쓰레드가 한 개의 자원을 사용하고자 할 때 해당 쓰레드를 제외한 나머지 쓰레드는 접근을 못하도록 막는 것을 의미합니다.

 

예제는 각 쓰레드가 공통 자원을 사용하는데 있어서 순차적으로 한번씩 사용해야 하는 자원에 대해 접근한다고 가정을 하였습니다.

 

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
43
44
public class Main {
    
    public static void main(String argsp[]) throws InterruptedException
    {    
        Resource resource = new Resource();
        Thread thread1 = new Thread(new Thread1(resource,"1번쓰레드"));
        Thread thread2 = new Thread(new Thread1(resource,"2번쓰레드"));
        Thread thread3 = new Thread(new Thread1(resource,"3번쓰레드"));
        
        thread1.start(); //차례대로 쓰레드를 실행
        thread2.start();
        thread3.start();
    }
}
 
public class Thread1 implements Runnable{
    private Resource resource;
    private String name;
    public Thread1(Resource resource,String name)
    {
        this.resource = resource;
        this.name = name;
    }
    public void run()
    {
        try
        {
            for(int i=0;i<3;i++)
            {
                resource.getResource(name); //0번 index부터 차례대로 자원을 사용
            }
        }catch(Exception e){}
 
    }
}
 
public class Resource {
    private int Resource_index = 0;
    public void getResource(String name)
    {
        System.out.println(name +"=" +Resource_index + "번째 resource 사용"); //자원을 사용했다고 가정
        Resource_index ++;                                                //다음 자원의 index
    }
}

 

프로그램은 thread1,thread2,thread3가 Resoucr 객체의 Resource_index를 0번부터 차례대로 호출해서 사용할려고 합니다. 한번 사용하고나면 Resource_index를 증가시키게 됩니다. 동기화 되지 않은 상태이며 결과를 먼저 보겠습니다.

 

 

0번째 자원은 여러번 사용 되었을 뿐만 아니라 순차적으로 자원을 사용하지도 못하고 있습니다.

 

 
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
43
44
public class Main {
    
    public static void main(String argsp[]) throws InterruptedException
    {    
        Resource resource = new Resource();
        Thread thread1 = new Thread(new Thread1(resource,"1번쓰레드"));
        Thread thread2 = new Thread(new Thread1(resource,"2번쓰레드"));
        Thread thread3 = new Thread(new Thread1(resource,"3번쓰레드"));
        
        thread1.start(); //차례대로 쓰레드를 실행
        thread2.start();
        thread3.start();
    }
}
 
public class Thread1 implements Runnable{
    private Resource resource;
    private String name;
    public Thread1(Resource resource,String name)
    {
        this.resource = resource;
        this.name = name;
    }
    public void run()
    {
        try
        {
            for(int i=0;i<3;i++)
            {
                resource.getResource(name); //0번 index부터 차례대로 자원을 사용
            }
        }catch(Exception e){}
 
    }
}
 
public class Resource {
    private int Resource_index = 0;
    public synchronized void getResource(String name) //동기화
    {
        System.out.println(name +"=" +Resource_index + "번째 resource 사용");
        Resource_index ++;
    }
}

 

이번엔 synchronized 키워드를 사용하여 getResource()를 동기화 하였습니다.

 

 

 

이번에는 한번 사용된 자원은 재사용되지 않았고 순차적으로 잘 사용하고 있습니다.


List of Articles
번호 제목 날짜 조회 수
151 A java Runtime Environment(JRE) or Java Development Kit(JDK) must be ~~~~ 하면서 이클립스가 실행안될때. file 2019.03.05 789
150 Apache Commons HttpClient 3.x 로 Http 서버에 파일 전송하기 file 2019.01.08 1136
149 BigDecimal타입의 사칙연산 2016.12.22 3713
148 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
147 Database Connections 생성하기 (오라클) file 2016.08.29 4072
146 eclipse 콘솔(로그)에 디버그(Debug) 모드에서 실행된 쿼리문을 보여주자. - 전자정부프레임워크 오라클 file 2016.08.29 4172
145 eclipse 콘솔(로그)에 디버그(Debug) 모드에서 실행된 쿼리문을 보여주자. - 전자정부프레임워크 오라클 file 2016.08.29 6001
144 Eclipse에서 import문 자동으로 작성하는 기능 file 2016.09.19 4811
143 enum 2016.09.13 3404
142 File 클래스 file 2016.09.13 3603
141 for-each문 file 2016.09.13 3120
140 GET 파라메타 쿼리문자열 컨트롤 : getQueryString 2016.12.09 4705
139 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 256
138 HashMap 사용하기 file 2021.03.31 134
137 iBATIS 동적으로 맵핑하기 2016.12.09 3949
136 Jadclipse 플러그인 설치 file 2016.09.19 3512
135 JAR 파일 2016.09.19 3713
134 Java : JSOUP 를 이용, html에서 소스, 링크경로 추출후 절대 경로로 바꾸기 2019.01.08 1166
133 JAVA CentOS JDK 설치 및 환경변수 설정 file 2018.07.09 1821
132 JAVA HashMap의 Key값 출력하기 2018.07.09 1079
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved