class SumThread implements Runnable{
int from, to;
long sum;
SumThread(int from, int to){
this.from=from;
this.to=to;
}
long getSum(){
return sum;
}
public void run(){
for(int i=from;i<=to;i++)
sum+=i;
}
}
public class Thread02{
public static void main(String[] args){
SumThread st=new SumThread(1, 1000);
Thread aa = new Thread(st); //1.- Ŭ·¡½º¸¦ °´Ã¼È ½ÃŲ º¯¼ö st¸¦ Thread(st)·Î
aa.start();
try{
ss.join();
}catch(Exception e){}
System.out.println(st.getSum()); //2.-- aa.getSum() ÀÌ ¾ÈµÇ´Â ÀÌÀ¯°¡ ±Ã±Ý!
System.out.println(aa.isAlive());
}
}
½º·¹µå¸¦ ÀÌ¿ëÇؼ 1ºÎÅÍ 1000±îÁöÀÇ ÇÕÀ» ±¸ÇÏ´Â ÄÚµåÀÔ´Ï´Ù.
1¹ø Ç¥½Ã¿¡¼ ½º·¹½º¿¡ ¸Å°³º¯¼ö·Î Ŭ·¢½º °´Ã¤º¯¼ö¸¦ ³Ö¾ú´Âµ¥(ÀûÈ®ÇÑ Ç¥ÇöÀÎÁö´Â À߸𸣰ڽÀ´Ï´Ù ¤Ð;)
°´Ã¤º¯¼ö¸¦ ³Ö¾ú±â ‹š¹®¿¡ ½º·¹µå °´Ã¼¸í aa.getSum()ÀÌ °¡´ÉÇÒÁÙ ¾Ë¾Ò´Âµ¥ Á¢±ÙÀÌ ¾ÈµÇ´Â°Ì´Ï´Ù.
½º·¹µå °´Ã¤¸¦ ¸¸µé¶§ À§¿¡¼ ¸¸µç Ŭ·¡½º °´Ã¤ÀÇ ¸Þ¼ÒµåµéÀº »ó¼Ó(?)ÀÌ ¾ÈµÇ°í ±»ÀÌ »ó¼Ó½Ãų·Á¸é µû·Î ±¸ÇöÇØÁà¾ßÇϴ°ǰ¡¿ä?..
* ±ÛÀ» µî·ÏÇϽǶ§ ²À ÇÊ¿äÇÑ °æ¿ì°¡ ¾Æ´Ï¸é °³ÀÎÁ¤º¸¸¦ ³²±âÁö ¸¶¼¼¿ä ^^ (¿¬¶ôó,À̸ÞÀÏÁÖ¼Ò,Áֹεî·Ï¹øÈ£ µî)