开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推
这个该如何做?请教高手

解决方案 »

  1.   

    import java.lang.*;public class ThreadABC
    {

    public static void main(String[] args)
    {
    new Thread(new ABC(0)).start();
    new Thread(new ABC(1)).start();
    new Thread(new ABC(2)).start();
    }

    }class ABC implements Runnable
    {

    char a = 'A';

    ABC(int i)
    {
    a = (char)(a + i);
    }

    public void run()
    {

    for(int i=0; i<3; i++)
    {
    System.out.println(a);
    try{
    Thread.sleep(100);
    }
    catch(InterruptedException e)
    {
    }
    }

    }

    }
    这一种方法一般可以实现你要的功能,但是这个方法是危险,请楼主注意。
    在某些情况,这个方法打印出来的可能也不是你要的顺序,但是一般是正确的了。
      

  2.   

    1.5以后有个先进的办法创建线程.class ABC implements Runnable
    {
        
        char a = 'A';
        
        ABC(int i)
        {
            a = (char)(a + i);
        }
        
        public void run()
        {
            
            for(int i=0; i<10; i++)
            {
                System.out.println(a);
                try{
                    Thread.sleep(100);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            
        }
    public class ThreadTest
    {
        public static void main(String[] args)
        {
            ExecutorService executorService = Executors.newFixedThreadPool(3);
            executorService.execute(new ABC(0));
            executorService.execute(new ABC(1));
            executorService.execute(new ABC(2));
            //记得运行完以后把线程都删掉,要不然程序会一直运行而不会停止
             executor.shutdown();
        }
    }  
    }
    记得该导入的包要导入就可以了
      

  3.   


    public class ThreadABC
    {
    public static char last = 'C';
    public static byte[] key = new byte[0];
        
        public static void main(String[] args)
        {
            new Thread(new ABC(0,key)).start();
            new Thread(new ABC(1,key)).start();
            new Thread(new ABC(2,key)).start();
        }
        
    }class ABC implements Runnable
    {
        char a = 'A';
        byte[] key;
        
        ABC(int i,byte[] key)
        {
            this.key = key;
         a = (char)(a + i);
        }
        
        public void run()
        {
         while(true){
         synchronized(key){
         while(ThreadABC.last!=(a-63)%3+65)
    try {
    key.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
            System.out.println(a);
            ThreadABC.last = a;
            key.notifyAll();
                
         }
         try{
                    Thread.sleep(100);
                }
                catch(InterruptedException e)
                {
                }
        
            }
            
        }
        
    }
    总算搞定了,累死我也……………………
      

  4.   

    解释下7#的代码……用一个static函数last记录上一次打印,线程运行时判断上一个打印的是不是自己的前一个,是自己的前一个就打印,不是的话就等待,拆开来写就是下面这样
    synchronized(key){
         if(a=='A'){
         while(ThreadABC.last != 'C')
    try {
    key.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
         }else
         while(ThreadABC.last != a-1)
    try {
    key.wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
        
            System.out.println(a);
            ThreadABC.last = a;
            key.notifyAll();
                
         }