class AThread implements Runnable
{
  private int x=0;
  public void run()
  {
   while(true)
   {
  
   for(int i=0;i<3;i++)
   {
   System.out.print("* ");
   }
   System.out.println();
   try{Thread.sleep(1000);}catch(Exception e){}
  
   }
   }
}
class BThread implements Runnable
{
public void run()
{
while(true)
{

System.out.println("& ");
try{Thread.sleep(100);}catch(Exception e){}

}
}
}
class CThread implements Runnable
{
public void run()
{
while(true)
{

System.out.println("$ ");
try{Thread.sleep(500);}catch(Exception e){}

}
}
}
class PrintThread
{
public static void main(String [] args)
{
//String str=new String("");
new Thread(new AThread()).start();
new Thread(new BThread()).start();
new Thread(new CThread()).start();
}
}
输出的结果为:
* * *






*&
* *
我想这应该是线程没有同步的问题.如果我想使用synchronized(str){}我应该在哪创建一个str对象呢.同一个对象?

解决方案 »

  1.   

    你这个是3个进程,对应3个对象。应该使用join()方法保证他们顺序执行。另一种方法是BThread 内同步对象 AThread ,我调试调试,过会答复你。
      

  2.   

    class Test {  private static final Pattern http_pattern = Pattern.compile("^/\\*dhtmlLine=\\d+\\*/$", Pattern.MULTILINE);  public static void main(String[] args) {
        Object lock = new Object();
        //String str=new String("");
        new Thread(new AThread(lock)).start();
        new Thread(new BThread(lock)).start();
        new Thread(new CThread(lock)).start();
      }
    }class AThread implements Runnable {
      
      private Object lock;
      public AThread(Object lock) {
        this.lock = lock;
      }
      
      private int x = 0;
      public void run() {
        while (true) {
          synchronized (lock) {
            for (int i = 0; i < 3; i++) {
              System.out.print("* ");
            }
            System.out.println();
            try {
              Thread.sleep(1000);
            }
            catch (Exception e) {}
          }
        }
      }
    }class BThread implements Runnable {
      private Object lock;
      public BThread(Object lock) {
        this.lock = lock;
      }  
      public void run() {
        while (true) {
          synchronized (lock) {
            System.out.println("& ");
            try {
              Thread.sleep(100);
            }
            catch (Exception e) {}
          }
        }
      }
    }class CThread implements Runnable {
      private Object lock;
      public CThread(Object lock) {
        this.lock = lock;
      }  
      public void run() {
        while (true) {
          synchronized (lock) {
            System.out.println("$ ");
            try {
              Thread.sleep(500);
            }
            catch (Exception e) {}
          }
        }
      }
    }
      

  3.   

    使用 join()同步的代码如下:class AThread implements Runnable{
      private int x=0;
      public void run() {
    for(int i=0;i<10;i++){
    try{Thread.sleep(50);}catch(Exception e){}
    System.out.print("* ");
    }
    System.out.println();
      }
    }
    class BThread implements Runnable{
    // AThread a  =  new AThread(); public void run() {
    //synchronized(AThread.class) {
    for(int i=0;i<10;i++){
    try{Thread.sleep(50);}catch(Exception e){}
    System.out.print("& ");
    }
    //}
    System.out.println();
    }
    }
    class CThread implements Runnable{
    public void run(){
    for(int i=0;i<10;i++){
    try{Thread.sleep(50);}catch(Exception e){}
    System.out.print("$ ");
    }
    System.out.println();
    }
    }
    class PrintThread{
    public static void main(String [] args) throws Exception {
    Thread a  = new Thread(new AThread());
    a.start();
    a.join();
    Thread b =  new Thread(new BThread());
    b.start();
    b.join();
    Thread c = new Thread(new CThread());
    c.start();
    c.join();
    }
    }
      

  4.   

    呵呵
    blackpark(暖咖啡) 你有没有测过你的程序亚
    运行结果:
    * * * * * * * * * * 
    & & & & & & & & & & 
    $ $ $ $ $ $ $ $ $ $ 
    程序结束