JAVA Thread 스레드

스레드Thread(멀티스레드)란 하나의 프로세스에서 여러가지 일을 동시에 하는것을 의미한다.

ex)
프로세스 - 채팅프로그램
스레드 - 채팅, 파일전송


스레드의 두개지 형태

객체 1개를 1개의 스레드가 공유하는 방식
객체 1개를 n개의 스레드가 공유하는 방식

ThreadTest_1 - 객체 1개를 1개의 스레드가 공유하는 방식
Interface 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 ThreadTest implements Runnable {
    @Override
    public void run() {
        // 스레드의 네임을 구한다.
        System.out.println(Thread.currentThread().getName()); // A print
        System.out.println("ThreadTest"); // 현재 클래스 네임
        
        for (int i =0; i<10; i++){
            System.out.println("i = " + i);
            // 예외처리
            try{
                Thread.sleep(1000); // 1초 sleep
            } catch ( Exception e) {}
        }
    }
}
//#################### mainclass
public class MainClass {
    public static void main(String args[]){
        
        ThreadTest threadTest = new ThreadTest();
        
        Thread thread = new Thread(threadTest, "A"); // runnable target, String name
        thread.start();
        System.out.println(Thread.currentThread().getName()); // main print
    }
}
cs

result

main
A
ThreadTest
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9


ThreadTest_2 - 객체 1개를 1개의 스레드가 공유하는 방식
extends 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
public class ThreadTest extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        for(int i = 0; i<10;i++) {
            System.out.println(i);
            try{
                Thread.sleep(500);
            } catch (Exception e) {}
        }
    }
}
//#################### mainclass
public class MainClass {
    public static void main(String args[]){
        
        ThreadTest threadTest = new ThreadTest();
        threadTest.setName("B");
        threadTest.start();
        System.out.println(Thread.currentThread().getName());
    }
}
cs

result

main
B
0
1
2
3
4
5
6
7
8
9

ThreadTset_3 - 객체 1개를 n개의 스레드가 공유하는 방식
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
public class ThreadTest implements Runnable{
 
    int testNum = 0;
 
    @Override
    public void run() {
        for(int i = 0; i<10;i++) {
            if (Thread.currentThread().getName().equals("A")) {
                System.out.println("==================");
                testNum++;
            }
            System.out.println("Thread Name : "+Thread.currentThread().getName() + ", testNum : " + testNum);
            try{
                Thread.sleep(500);
            } catch (Exception e) {}
        }
    }
}
 
//#################### mainclass
 
public class MainClass {
    public static void main(String args[]){
 
        ThreadTest threadTest = new ThreadTest();
 
        Thread thread0 = new Thread(threadTest, "A");
        Thread thread1 = new Thread(threadTest, "B");
 
        thread0.start();
        thread1.start();
 
        System.out.println(Thread.currentThread().getName());
    }
}
cs

result

main
Thread Name : B, testNum : 0
==================
Thread Name : A, testNum : 1
==================
Thread Name : B, testNum : 1
Thread Name : A, testNum : 2
==================
Thread Name : B, testNum : 2
Thread Name : A, testNum : 3
==================
Thread Name : B, testNum : 3
Thread Name : A, testNum : 4
==================
Thread Name : B, testNum : 4
Thread Name : A, testNum : 5
==================
Thread Name : B, testNum : 5
Thread Name : A, testNum : 6
==================
Thread Name : B, testNum : 6
Thread Name : A, testNum : 7
==================
Thread Name : B, testNum : 7
Thread Name : A, testNum : 8
==================
Thread Name : B, testNum : 8
Thread Name : A, testNum : 9
==================
Thread Name : B, testNum : 9
Thread Name : A, testNum : 10




ThreadTset_4 - 객체 1개를 n개의 스레드가 공유하는 방식
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
public class ThreadTest implements Runnable{
 
    int testNum = 0;
 
    @Override
    public synchronized void run() {
        for(int i = 0; i<10;i++) {
            if (Thread.currentThread().getName().equals("A")) {
                System.out.println("==================");
                testNum++;
            }
            System.out.println("Thread Name : "+Thread.currentThread().getName() + ", testNum : " + testNum);
            try{
                Thread.sleep(500);
            } catch (Exception e) {}
        }
    }
}
 
//#################### mainclass
 
public class MainClass {
    public static void main(String args[]){
        ThreadTest threadTest = new ThreadTest();
 
        Thread thread0 = new Thread(threadTest, "A");
        Thread thread1 = new Thread(threadTest, "B");
 
        thread0.start();
        thread1.start();
 
        System.out.println(Thread.currentThread().getName());
    }
}
cs

result

main
==================
Thread Name : A, testNum : 1

==================
Thread Name : A, testNum : 2

==================
Thread Name : A, testNum : 3

==================
Thread Name : A, testNum : 4

==================
Thread Name : A, testNum : 5

==================
Thread Name : A, testNum : 6

==================
Thread Name : A, testNum : 7

==================
Thread Name : A, testNum : 8

==================
Thread Name : A, testNum : 9

==================
Thread Name : A, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10
Thread Name : B, testNum : 10

댓글