관리 메뉴

moozi

Thread Test 본문

카테고리 없음

Thread Test

moozi 2015. 5. 28. 14:03

 


 class MyThread implements Runnable{
  ThreadTest t;
  
  MyThread(ThreadTest mt){
   t=mt;
  }
  public void run(){
   add();
  }
  
   public void add(){
   
   for(int i=0;i<20;i++){
    try{
     //notify();
     
     Thread.sleep(100); 
     
     t.add();
     System.out.println(Thread.currentThread().getName()+":"+t.n);
     //wait();
     
    }catch(Exception e){
     
    }; 
   } 
  }
 }

 

public class ThreadTest {
 int n=0;//공유데이터
 
 public void add(){
  n++;
 }
 
 public static void main(String[] args){
  ThreadTest tt1=new ThreadTest();
  
  
  MyThread mt1=new MyThread(tt1);
  Thread t1=new Thread(mt1,"쓰레드1");
  t1.start();
  Thread t2=new Thread(mt1,"쓰레드2");
  t2.start();  
 }
 
}

Comments