하나의 process 내부에도 엄연한 작업 단위가 있다. 이를 thread라고 하고 요즘엔 당연 multi-threading 방식의 programming을 한다.

물론 여러 이슈에 따라서 single-threading방식이 더 유리한 경우도 있다는 거 알아두자!

(예를 들면 Android와 Node-js는 single-thread기반이다.)

 

 runInterface class

1
2
3
4
5
6
7
8
9
10
11
12
public class runInterface implements Runnable{
    @Override
    public void run(){
        Thread mThread=Thread.currentThread();
        System.out.printf("%s-시작\n",mThread.getName());
        for(int i=0;i<50;i++){
            System.out.printf("%s-%d\n",mThread.getName(),i);
        }
        System.out.printf("%s-종료\n",mThread.getName());
    }
}
 

 example class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class example{
    public static void main(String[] args){
        //currentThread() 메서드는 static 메서드이다.
        Thread mThread=Thread.currentThread();
        System.out.printf("%s-시작\n",mThread.getName());
        
        runInterface run=new runInterface();
        
        Thread thread1=new Thread(run);
        Thread thread2=new Thread(run);
        
        thread1.start();
        thread2.start();
        
        System.out.printf("%s-종료\n",mThread.getName());
    }
}
 

 실행해보자!

process가 돌때 main thread가 만들어지고 Thread-0과 Thread-1을 돌린다. 그리고 main thread는 바로 종료된다.

거의 main thread는 Thread-0과 Thread-1을 만들고 start 시켜준뒤 바로 종료 되었지만 Thread-0과 Thread-1을 계속 작업을 하는 것을 알 수 있다.

 

Posted by duehd88
,