i구야 2015. 3. 31. 12:16

 

synchronized 미적용시

 

 

 

 

synchronized 적고 나면

 

 

class ManyClient{
 StringBuffer str=new StringBuffer("ABCDE");
 public static void main(String args[]) throws InterruptedException{
 ManyClient mc=new ManyClient();
 mc.makeThread("가나다라마");
mc.makeThread("abcde");
 Thread.sleep(3000);
 }
 public void makeThread(final String s){
 Thread t=new Thread(){
 public void run(){
 addStr(s);
 }
 };
 t.start();
 }
   public synchronized void addStr(String other){
 for(int i=0; i<other.length(); i++){
 try{
 str.append(other.charAt(i));
 System.out.println("Str : "+str);
 Thread.sleep(100);
 }catch(Exception e){}
 }
 }
}