실행 중인 사용자 쓰레드가 하나도 없을 때 프로그램은 종료된다.
ex)
class Test {
static long startTime = 0;
public static void main(String args[])
{
ThreadTest1 t1 = new ThreadTest1();
ThreadTest2 t2 = new ThreadTest2();
t1.start();
t2.start();
startTime = System.currentTimeMillis();
try
{
t1.join(); //main쓰레드가 t1의 작업이 끝날 때 까지 기다린다.
t2.join(); //main쓰레드가 t2의 작업이 끝날 때 까지 기다린다.
} catch(InterruptedException e) {}
System.out.println("소요시간 :" +(System.currentTimeMillis() - startTime));
} //main
}
class ThreadTest1 extends Thread{
public void run(){
for(int i=0; i< 300; i++)
{
System.out.println(new String("-"));
}
}//run()
}
class ThreadTest2 extends Tread{
pyblic void run()
{
for(int i=0; i<300; i++)
{
System.out.println(new String("\"));
}
}//run()
}
class ThreadTest{
public static void main(String args[])
{
for(int i=0; i<300; i++)
{
System.out.println("-");
}
for(int i=0; i<300; i++)
{
System.out.println("\");
}
}//main
}
class ThreadTest{
public static void main(String args[])
{
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
t1.start();
t2.start();
}
}
class MyThread1 extends Thread{
public void run()
{
for(int i=0; i<300; i++)
{
System.out.println("-");
}
}// run()
}
class MyThread2 extends Thread{
public void run()
{
for(int i=0; i<300; i++
{
System.out.println("\");
}
}// run()
}
class ThreadTest{
public static void main(String args[])
{
String input = JOptionPane.showInputDialog("아무 값이나 입력하세요.");
System.out.println("입력하신 값은" + input + "입니다");
for(int i=10; i> 0; i--){
System.out.println(i);
try { Thread.sleep(1000); } catch (Exception e) {}
}
}// main
}
위의 코드는,
사용자로부터 입력을 기다리는 구간동안 아무런 일도 하지 않는다. (동기)
class ThreadTest2{
public void main(String args[])
{
Thread1 t1 = new Thread1();
t1.start();
String input = JOptionPane.showInputDialog("아무 값이나 입력하세요");
System.out.println("입력한 값은" + input + "입니다.");
}
}
class Thread1 extends Thread{
public void run()
{
for(int i=10; i>0 ; i--)
{
System.out.println(i);
try { sleep(1000); } catch(Exception e ) {}
}
} //run()
}
사용자로부터 입력을 기다리는 동안 다른 일을 수행하고 있는다. (비동기)