하나의 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을 계속 작업을 하는 것을 알 수 있다.
'Programing > java' 카테고리의 다른 글
Java Serializable : 자바 직렬화 (0) | 2015.01.31 |
---|---|
thread이야기_02.run()과 start()의 차이 (0) | 2014.11.23 |
자바 시작할 때 썻던 자바 간단한 특징 (0) | 2014.06.29 |
자동으로 import 하는 단축키 (0) | 2014.06.29 |
integer클래스의 valueOf와 parsInt (0) | 2014.05.20 |