package asmTest;public class TongOne extends Thread
{
    private String cha;
    
    static Object printer = new Object();
    
    public TongOne(String cha)
    {
        this.cha = cha;
    }
    
    void print()
    {
        for (int i = 1; i <= 3; i++)
            System.out.print(cha + " ");
        
    }
    
    public void run()
    {
        synchronized (printer)
        {
            for (int i = 1; i < 3; i++)
            {
                print();
                System.out.println();
                
            }
        }
        
    }
    
    public static void main(String args[])
    {
        
        TongOne test1 = new TongOne("线程A");
        TongOne test2 = new TongOne("线程B");
        TongOne test3 = new TongOne("线程C");
        
        test1.start();
        test2.start();
        test3.start();
        
    }
}不知道是不是你想要的结果

解决方案 »

  1.   


    package number;public class TongOne extends Thread{                     
    private String cha;                                     
    static Object printer = new Object();                   
    public TongOne(String cha){                             
    this.cha = cha;                                     
    }                                                       
    void print(){                  
    synchronized(printer){                               
    for(int i =1; i <=3;i++)                             
    System.out.print(cha+ " ");                       
    }                                                   
    }                                                        
    public void run(){                                       
                                                         
    for(int i =1;i <3;i++){                               
    print();                                           
    System.out.println();                               
                                                       
    }                                                   
                                                         
                                                         
    }                                                  
    public static void main(String args[]){                 
                                                         
    TongOne test1 = new TongOne("线程A");                 
    TongOne test2 = new TongOne("线程B");                 
    TongOne test3 = new TongOne("线程C");                 
                                                         
       
    try {
    test1.start();       
    test1.join();
    test2.start(); 
    test2.join();
    test3.start();  
    } catch (InterruptedException e) {
    }                                                  
    }                                                     
    }                         这样就可以了
    你运行下就好了   
      

  2.   

    public class TongOne extends Thread {
    private String cha;
    static Object printer = new Object(); public TongOne(String cha) {
    this.cha = cha;
    } void print() {
    synchronized (printer) {
    for (int i = 1; i <= 3; i++)
    System.out.print(cha + " ");
    }
    } public void run() { for (int i = 1; i < 3; i++) {
    print();
    System.out.println(); } } public static void main(String args[]) { TongOne test1 = new TongOne("线程A");
    TongOne test2 = new TongOne("线程B");
    TongOne test3 = new TongOne("线程C");
    try {
    test1.start();
    test1.join();
    test2.start();
    test2.join();
    test3.start(); } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
      

  3.   

        存在的问题就是run方法里面调用Print()方法后有一条输出语句。因为同步块是在Print方法里面的,一旦执行完毕后去执行输出语句的时候,该线程就释放了该资源的锁,其他等待的线程就可以去访问该资源了。
      

  4.   


    package asmTest;public class TongOne extends Thread
    {
        private String cha;
        
        static Object printer = new Object(); 
        
        public TongOne(String cha)
        {
            this.cha = cha;
        }
        
        void print()
        {
            for (int i = 1; i <= 3; i++)
                System.out.print(cha + " ");
            
        }
        
        public void run()
        {
            synchronized (printer)        {
                for (int i = 1; i < 3; i++)
                {
                    print();
                    System.out.println();
                    
                }
            }
            
        }
        
        public static void main(String args[])
        {
            
            TongOne test1 = new TongOne("线程A");
            TongOne test2 = new TongOne("线程B");
            TongOne test3 = new TongOne("线程C");
            
            test1.start();
            test2.start();
            test3.start();
            
        }
    }static Object printer = new Object();
    synchronized (printer)//这两句里printer的意义是什么,
    若不用printer,又有什么可代替的呢?
      

  5.   

    lz 在方法块中加入了 synchronized进行同步。并对此做了同一时只能有一个线程操作它的方法。
    public final void join()
                    throws InterruptedException等待该线程终止。 
      

  6.   

    整个run()才是一个操作,所以应该给run()方法加锁
    public class TongOne extends Thread{                      
    private String cha;                                      
    static Object printer = new Object();                    
    public TongOne(String cha){                              
    this.cha = cha;                                      
    }                                                        
    void print(){                                                                          
    for(int i =1; i <=3;i++)                              
    System.out.print(cha+ " ");                                                                           
    }                                                         
    public void run(){                                        
    synchronized(printer){                                                
    for(int i =1;i <3;i++){                                
    print();                                            
    System.out.println();                                
    }                                                  
    }                                                    
                                                          
                                                          
    }                                                   
    public static void main(String args[]){                  
                                                          
    TongOne test1 = new TongOne("线程A");                  
    TongOne test2 = new TongOne("线程B");                  
    TongOne test3 = new TongOne("线程C");                  
                                                          
    test1.start();                                        
    test2.start();                                        
    test3.start();                                        
                                                          
                                                          
    }                                                      
    }       
      

  7.   

    要保证执行顺序就要用join()方法等待前一个线程执行完
    public class TongOne extends Thread{                      
    private String cha;                                      
    static Object printer = new Object();                    
    public TongOne(String cha){                              
    this.cha = cha;                                      
    }                                                        
    void print(){                                                                          
    for(int i =1; i <=3;i++)                              
    System.out.print(cha+ " ");                                                                          
    }                                                        
    public void run(){                                                                                       
    for(int i =1;i <3;i++){                                
    print();                                            
    System.out.println();                                                                                 
    }                                                    
                                                          
                                                          
    }                                                  
    public static void main(String args[]){                  
                                                          
    TongOne test1 = new TongOne("线程A");                  
    TongOne test2 = new TongOne("线程B");                  
    TongOne test3 = new TongOne("线程C");                  
                                                          
    test1.start();
    test1.join();                                       
    test2.start();
    test2.join();                                       
    test3.start();                                        
                                                          
                                                          
    }                                                      
    }      
      

  8.   

    8楼是正确的。
    分析下原因吧。
    A执行完run下面的for的一次循环以后,A休息去了,B上来干活了。
    为什么啊,不是应该A做完了才能休息么。。不是啊,你的程序告诉我说,我只要执行print的时候不别人打断就行了,你又没说我在run中间不能被打断。
    8楼的修改就达到了这个目的。
      

  9.   

    对于有严格顺序的同步执行,为啥要用多线程呢?直接单线程来的不是很快,更方便?
    严重属于多线程滥用
    去掉那些同步关键字吧,去掉多线程吧。直接调用run执行TongOne test1 = new TongOne("线程A");                  
    TongOne test2 = new TongOne("线程B");                  
    TongOne test3 = new TongOne("线程C"); test1.run();
    test2.run();
    test3.run();