quilt code

[고급자바] Thread (1) 본문

daily/고급자바

[고급자바] Thread (1)

김뱅쇼 2023. 2. 6. 20:30

1. 싱글 스레드 프로그램

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package kr.or.ddit.basic;
 
public class T01ThreadTest {
    public static void main(String[] args) {
        
        // 싱글 스레드 프로그램
        for(int i=1; i<=200; i++) {
            System.out.print("*");
        }
        System.out.println();
        
        for(int i=1; i<=200; i++) {
            System.out.print("$");
        }
        
    }
 
}
 
cs

 

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
package kr.or.ddit.basic;
 
public class T02ThreadTest {
    public static void main(String[] args) {
        // 멀티 스레드 프로그램 방식
        
        // 방법1 : Thread 클래스를 상속한 클래스의 인스턴스를 생성한 후 이 인스턴스의 start() 메소드를 호출한다.
        MyThread1 th1 = new MyThread1();  // thread로 사용할 객체 생성
        th1.run();
        
        // 방법2 : Runnable인터페이스를 구현한 클래스의 인스턴스를 생성한 후 이 인스턴스를 Thread객체를 생성할 때의 생성자의 파라미터로 넘겨준다.
        //        이 때 생성된 Thread 객체의 start() 메소드를 호출한다.
        Runnable r = new MyRunnable();
        Thread th2 = new Thread(r);
        th2.run();
        
        // 방법3 : 익명클래스를 이용하는 방법
        //        Runnable 인터페이스를 구현한 익명클래스를 Thread 인스턴스를 생성할 때 파라미터로 넘겨준다.
        Thread th3 = new Thread(new Runnable() {
            
            @Override
            public void run() {
                for(int i=1; i<=200; i++) {
                    System.out.print("@");
                    
                    try {
                        // Thread.sleep(시간) => 주어진 시간동안 잠시 멈춘다.
                        // 시간은 밀리세컨드 단위를 사용한다. 즉 1000은 1초를 의미한다.
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }            
                    
                }
                
            }    
        });
        th3.run();
    
   }
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MyThread1 extends Thread {
    
    @Override
    public void run() {
        for(int i=1; i<=200; i++) {
            System.out.print("*");
            
            try {
                // Thread.sleep(시간) => 주어진 시간동안 잠시 멈춘다.
                // 시간은 밀리세컨드 단위를 사용한다. 즉 1000은 1초를 의미한다.
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }            
            
        }
 
    }
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyRunnable implements Runnable {
 
    @Override
    public void run() {
        for(int i=1; i<=200; i++) {
            System.out.print("$");
            
            try {
                // Thread.sleep(시간) => 주어진 시간동안 잠시 멈춘다.
                // 시간은 밀리세컨드 단위를 사용한다. 즉 1000은 1초를 의미한다.
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }            
            
        }
        
    }
    
}
cs

'daily > 고급자바' 카테고리의 다른 글

[고급자바] Thread(3)  (0) 2023.02.07
[고급자바] Thread (2)  (0) 2023.02.07
[고급자바] Annotation  (0) 2023.02.06
[고급자바] 호텔 과제  (0) 2023.02.03
[고급자바] 로또 과제  (0) 2023.02.03