donaricano-btn

Future, Callable, Executor를 이용한 Thread


1. ExecutorService

-  자바5에서 멀티스레드와 콜백을 간편하게 사용하기 위하여 ExecutorService를 사용한다.

1) 사용법

- 작업 대상의 Callable 객체를 만들고 ExecutorService에 등록한 다음 Future 객체를 반환 받는다.

- 작업 결과는 Future를 통해 얻는다.


2. 예제

1) FindMax.java

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
import java.util.concurrent.Callable;
 
public class FindMax implements Callable<Integer>{
     
    private int[] data;
    private int start;
    private int end;
     
    FindMax(int[] data, int start, int end){
        this.data = data;
        this.start = start;
        this.end = end;
    }
     
    public Integer call() {
        int max = 0;
        for(int i = start; i<end; i++) {
            if(data[i] > max) {
                max = data[i];
            }
        }
         
        return max;
    }
}

- 값을 비교하여 max값을 반환 한다.

- Callable 인터페이스는 어떤 값도 반환하는 call() 를 제공한다.


2) MultiThreadMaxFinder.java

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
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
 
public class MultiThreadMaxFinder {
     
    public static int max(int[] data) throws InterruptedException, ExecutionException {
         
        if(data.length == 1) {
            return data[0];
        }else if(data.length == 0) {
            throw new IllegalArgumentException();
        }
         
        //작업 분할
        FindMax task1 = new FindMax(data, 0, data.length/2);
        FindMax task2 = new FindMax(data, data.length/2, data.length);
         
        //두 개의 스레드 생
        ExecutorService service = Executors.newFixedThreadPool(2);
         
        Future<Integer> future1 = service.submit(task1);
        Future<Integer> future2 = service.submit(task2);
         
        return Math.max(future1.get(), future2.get());
         
    }
     
    public static void main(String[] args) throws InterruptedException, ExecutionException {
         
        int[] values = {1,4,5,7,10,2,77,100,55,9,11,23};
         
        System.out.println(MultiThreadMaxFinder.max(values));
    }
}

- Callable 객체를 Executor 에 추가하여 사용한다.

- Executor는 각 객체의 스레드 실행


3. 장점

- 멀티 코어 환경에서 두배의 가까운 성능을 보인다. 

- 비동기 문제를 해결 할 수 있다.


블로그 이미지

리딩리드

,
donaricano-btn

Thread에서 데이터 반환(폴링, 콜백) - Receive data by using thread polling, callback


1. thread의 데이터 반환

- run(), start() 는 어떤 값도 반환하지 않는다. 

- 특정한 방법이 필요하다


2. 일반 적인 데이터 반환

1) ThreadTest.java

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
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ThreadTest implements Runnable{
     
    private String fileName;
    private String result;
     
    public ThreadTest(String fileName) {
        this.fileName = fileName;
    }
     
    @Override
    public void run() {
         
        FileOutputStream out = null;
        String filePath = "/Users/jeonghui/eclipse-workspace/JavaTest/src/";
        String content = "Hello";
         
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
                //결과 셋
                result = fileName;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
    }
     
    public String getResult() {
        return result;
    }
     
}

- getResult() 를 이용하여 파일 쓰기가 끝나면 파일 이름을 반환한다.

2) ThreadInterface.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.IOException;
 
public class ThreadInterface {
     
    public static void main(String[] args) throws IOException {
         
        String[] files = {"abc0.txt","abc1.txt","abc2.txt"};
         
        for(String filename : files) {
            ThreadTest tt = new ThreadTest(filename);
            Thread t = new Thread(tt);
            t.start();
             
            //결과 출력
            System.out.println(tt.getResult());
        }
         
    }
}

- getResult() 를 호출하여 확인한다.

3) 문제

- 그러나 콘솔에는 null 값이 출력되거나 운 좋다면 파일 명이 나올 수 있다.

- 스레드의 처리 속도와 main() 의 속도가 불일치(경쟁조건) 하다.


3.  폴링(polling)을 이용한 경쟁조건 해결

1) 폴링(polling) 이란?

-  동기화를 목적으로 상태를 주기적으로 검사하여 일정 조건이 만족하면 처리하는 방법

2) 위의 문제 해결

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
import java.io.IOException;
 
public class ThreadInterface {
     
    public static void main(String[] args) throws IOException {
         
        String[] files = {"abc0.txt","abc1.txt","abc2.txt"};
        ThreadTest[] tt = new ThreadTest[files.length];
         
        for(int i = 0; i<files.length; i++) {
            tt[i] = new ThreadTest(files[i]);
            Thread t = new Thread(tt[i]);
            t.start();
        }
         
        for(int i = 0; i<files.length; i++) {
            //결과 출력
            while(true) {
                if(tt[i].getResult() != null) {
                    System.out.println(tt[i].getResult());
                    break;
                }
            }
             
        }
         
    }
}

- 반복적으로 null 을 체크하여 아니라면 출력한다.

3) 문제

- 체크하기 위하여 너무 많은 일을 한다.

- 메인 스레드가 작업종료 상태를 계속 확인해야 하기 때문에 다른 스레드가 일할 시간 없다(성능저하)


4. 콜백(callback) 으로 문제 해결

1) 콜백(callback) 이란?

- 단일 스레드의 작업이 끝났을 때 자신을 생성한 클래스를 호출하여  메인 스레드에게 결과를 알려준다.

- 폴링의 무한 루프로 결과값을 체크하는 문제 해결

1) ThreadTest.java

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
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ThreadTest implements Runnable{
     
    private String fileName;
    private String result;
     
    public ThreadTest(String fileName) {
        this.fileName = fileName;
    }
     
    @Override
    public void run() {
         
        FileOutputStream out = null;
        String filePath = "/Users/jeonghui/eclipse-workspace/JavaTest/src/";
        String content = "Hello";
         
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
                //결과 셋
                ThreadInterface.ThreadResult(fileName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
    }
     
    public String getResult() {
        return result;
    }
     
}

- 정적 메소드를 호출하여 결과값을 전달한다.

2) ThreadInterface.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.IOException;
 
public class ThreadInterface {
     
    public static void ThreadResult(String name) {
        System.out.println(name);
    }
     
    public static void main(String[] args) throws IOException {
         
        String[] files = {"abc0.txt","abc1.txt","abc2.txt"};
        ThreadTest[] tt = new ThreadTest[files.length];
         
        for(int i = 0; i<files.length; i++) {
            tt[i] = new ThreadTest(files[i]);
            Thread t = new Thread(tt[i]);
            t.start();
        }
    }
}

- 반환 값을 받기위한 정적메소드 

3) 개선

- 정적메소드를 호출하는 것보다 인스턴스를 생성하여 반환값을 받는 것이 더 좋다.

4) 문제

- 스레드의 순서가 없다. 먼저 종료되는 스레드의 결과를 먼저 받는다.

- 순서가 필요한 결과 값이라면 다른 방법이 필요하다.(Future, callable, Executor)




블로그 이미지

리딩리드

,
donaricano-btn

Thread 만들기 -  To create Threads


1. Thread 생성

1)  Thread클래스를 서브 클래싱해서 run()를 오버라이드 하는 방법

2) Runnable 인터페이스 구현하여 Runnable 객체를 Thread 생성자로 전달하는 방법


2. Thread의 run()

- 스레드의 시작과 끝이다.

- 단일 스레드는 main()가 반환될 때 종료되고 멀티 스레드는 run()메소드가 반환될 때 종료된다.


3. Thread클래스 서브 클래싱 하는법

- FileOutputStream 을 이용하여 여러 파일을 생성하는 스레드

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
45
46
47
48
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ThreadTest extends Thread{
     
    private String fileName;
     
    public ThreadTest(String fileName) {
        this.fileName = fileName;
    }
     
    @Override
    public void run() {
         
        FileOutputStream out = null;
        String filePath = "/Users/jeonghui/eclipse-workspace/JavaTest/src/";
        String content = "Hello";
         
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
    }
     
    public static void main(String[] args) throws IOException {
         
        String[] files = {"abc0.txt","abc1.txt","abc2.txt"};
         
        for(String filename : files) {
            Thread t = new ThreadTest(filename);
            t.start();
        }
         
    }
 
 
}

- main() 에서 파일 명의 갯수 만큼 thread를 생성한다.

- 생성자를 이용하여 스레드에 정보를 전달 했다.

1) 서브 클래싱 주의

- Thread의 서브 클래스를 만들었다면, run() 이 외에는 아무것도 오버라이드 해선 안된다.

- 다른 메소드(start(), interrupt()...)  특별한 목적이나 가상머신과의 통신에 사용된다.


4. 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ThreadTest implements Runnable{
     
    private String fileName;
     
    public ThreadTest(String fileName) {
        this.fileName = fileName;
    }
     
    @Override
    public void run() {
         
        FileOutputStream out = null;
        String filePath = "/Users/jeonghui/eclipse-workspace/JavaTest/src/";
        String content = "Hello";
         
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
         
    }
     
    public static void main(String[] args) throws IOException {
         
        String[] files = {"abc0.txt","abc1.txt","abc2.txt"};
         
        for(String filename : files) {
            ThreadTest tt = new ThreadTest(filename);
            Thread t = new Thread(tt);
            t.start();
        }
         
    }
}

- 서브 클래싱과 비슷하지만 객체를 전달받아 thread를 생성하는 방법만 다르다

블로그 이미지

리딩리드

,
donaricano-btn

InputStream  - 입력스트림


1. java.io.InputStream

- 자바에서 제공하는 기본 입력 클래스이다.

- 바이트 데이터를 읽는데 사용한다.

- 서브 클래스로 인스턴스를 작성해도 InputStream으로 반환된다(다형성)

1) FileInputStream : 파일로부터 데이터를 읽는데 사용

2) TelnetInputStream : 네트워크 연결로 부터 읽음

3) ByteArrayInputstream : 바이트 배열로 부터 읽음


2. read() 

- InputStream의 기본 메소드

- 단일바이트를 읽으며 0~255 사이의 정수 타입을 반환한다.

- 스트림이 끝에 도달하면 -1을 반환 한다.

- 추상 메소드로 선언되어 있어 서브클래스(ByteArrayInput....등)에서 수정할 필요가 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package selenium;
 
import java.io.FileInputStream;
import java.io.IOException;
 
public class InputStream {
     
    public static void main(String[]args){
        try{
            FileInputStream in = new FileInputStream("D:\\input.txt");
            byte[] input = new byte[10];
             
            for(int i = 0; i<input.length; i++){
                int b = in.read();
                if(b == -1){
                    break;
                }
                input[i] = (byte) b;
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

- int 타입을 반환한다.

- 1byte 씩 읽는 것은 매우 비효율적이다 그래서 배열로 한번에 반환하는 read() 함수가 있다.


3. read(byte[] input), read(byte[] input, int offset, int length)

- read(byte[] input) : 배열의 크기만큼 읽는다.

- read(byte[] input, int offset, int length) : offset 부터 length까지 읽는다.

- 스트림의 끝에 도달하면 -1을 반환한다.

1
2
3
4
5
6
int bytesRead = 0;
int byteToRead = 1024;
byte[] input = new byte[byteToRead];
while(bytesRead < byteToRead){
    bytesRead += in.read(input, bytesRead, bytesToRead-bytesRead);
}

- 위 소스는 네트워크 스트림에서 자주사용된다.(실제 읽고자 하는 바이트의 크기가 보장되어야 하는 상황에서 사용)



블로그 이미지

리딩리드

,