동시처리를 하기위한 문법
main()메서드→ 메인스레드 한개가 동작하고 있는겁니다.
Thread 클래스를 이용하는 방법
public class MyThread extends Thread{
@Override
public void run() {
//System.out.println("스레드가 동작합니다.");
for(int i=0; i<100; i++) {
System.out.println("MyThread : "+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadTest01 {
public static void main(String[] args) {
MyThread thread=new MyThread();
//스레드를 적용시키려면 run()직접호출하면 않됩니다.
//thread.run();//XXXXXX
//시분할기법 마치 동시에 처리하는 것처럼 동작한다.
thread.start(); //스레드가 동작합니다.
for(int i=0; i<100; i++) {
System.out.println("main : "+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Runnable인터페이스를 이용활용하는 방법