想让这个程序两个线程同时卖票,程序如下:
public class ThreadDemo
{
 public static void main(String  args[])
 {
  ThreadTest t=new ThreadTest();  new Thread(t).start();   try{Thread.sleep(1);}
   catch(Exception e){}   t.str=new String("method");   new Thread(t).start();
  }
}
class ThreadTest implements Runnable
{
  private int tickets=100;
  String str=new String(" ");
  public void run()
  {
     if(str.equals("method"))
       {
while(true)
{
  sale();
}
       }      else
      {
        synchronized(this)
{
  if(tickets>0)
   {
System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
   }
}
       }
    }
     public synchronized void sale()
       {
if(tickets>0)
{
System.out.print("Test: ");
System.out.println(Thread.currentThread().getName()+" is saling
ticket "+tickets--);
}
       }
}程序的运行结果(第一次):
Thread-0 is saling ticket 100
Test: Thread-1 is saling ticket 99
Test: Thread-1 is saling ticket 98
Test: Thread-1 is saling ticket 97
Test: Thread-1 is saling ticket 96
Test: Thread-1 is saling ticket 95
Test: Thread-1 is saling ticket 94
Test: Thread-1 is saling ticket 93
Test: Thread-1 is saling ticket 92
Test: Thread-1 is saling ticket 91
Test: Thread-1 is saling ticket 90
               。
               。
               。
Test: Thread-1 is saling ticket 10
Test: Thread-1 is saling ticket 9
Test: Thread-1 is saling ticket 8
Test: Thread-1 is saling ticket 7
Test: Thread-1 is saling ticket 6
Test: Thread-1 is saling ticket 5
Test: Thread-1 is saling ticket 4
Test: Thread-1 is saling ticket 3
Test: Thread-1 is saling ticket 2
Test: Thread-1 is saling ticket 1第二次运行结果:
Test: Thread-0 is saling ticket 10
Test: Thread-0 is saling ticket 99
Test: Thread-0 is saling ticket 98
Test: Thread-0 is saling ticket 97
Test: Thread-0 is saling ticket 96
Test: Thread-0 is saling ticket 95
Test: Thread-1 is saling ticket 94
Test: Thread-1 is saling ticket 93
Test: Thread-1 is saling ticket 92
Test: Thread-1 is saling ticket 91
                。
                。
                。
Test: Thread-1 is saling ticket 11
Test: Thread-1 is saling ticket 10
Test: Thread-1 is saling ticket 9
Test: Thread-0 is saling ticket 8
Test: Thread-0 is saling ticket 7
Test: Thread-0 is saling ticket 6
Test: Thread-0 is saling ticket 5
Test: Thread-0 is saling ticket 4
Test: Thread-0 is saling ticket 3
Test: Thread-0 is saling ticket 2
Test: Thread-0 is saling ticket 1                   
而我希望得到的结果是:
Thread-0 is saling ticket 100
Test: Thread-1 is saling ticket 99
Thread-0 is saling ticket 98
Test: Thread-1 is saling ticket 97
Thread-0 is saling ticket 96
Test: Thread-1 is saling ticket 95
        。
        。
        。
Thread-0 is saling ticket 2
Test: Thread-1 is saling ticket 1 我是看的张孝祥的那本培训教程里有这么一个例子,他书上的结果就像我期望的那样,两个线程交替执行,但我的运行结果跟他说的完全不一样,我不知道这个程序是哪里出了问题,为什么线程0就只调用了一次synchronized(this)甚至一次都没调用呢?
 哪位大侠帮忙分析一下吧,感激不尽!!

解决方案 »

  1.   

    没看那本书,自己改了改
    public class Thread01 {
    public static void main(String args[])
    {
    ThreadTest t=new ThreadTest(); new Thread(t).start(); //try{Thread.sleep(1);}
    //catch(Exception e){} t.str=new String("method"); new Thread(t).start();
    }
    }
    class ThreadTest implements Runnable
    {
    private int tickets=100;
    private static String threadName = "";
    String str=new String(" "); public void run() 
    {
    try
    {
    while (tickets>0)
    sale(); }
    catch(InterruptedException e)
    {
    System.out.println(e.getStackTrace()); 
    }
    catch(Exception e)
    {
    System.out.println(e.getStackTrace()); 
    }
    }

    public synchronized void sale() throws InterruptedException {
    if(tickets>0)
    {
    if (threadName.length() > 0)
    {
    if (threadName.equals(Thread.currentThread().getName()))
    {
    this.notifyAll();
    this.wait();
    }
    }
    threadName = Thread.currentThread().getName();
    System.out.print("Test: ");
    System.out.println(Thread.currentThread().getName()+" is saling ticket "+tickets--);
    }
    }
    }
      

  2.   

    谢谢你呀!!!
    我运行了你的程序,结果:
    Test: Thread-0 is saling        ticket 100
    Test: Thread-1 is saling        ticket 99
    Test: Thread-0 is saling        ticket 98
    Test: Thread-1 is saling        ticket 97
    Test: Thread-0 is saling        ticket 96
    Test: Thread-1 is saling        ticket 95
    Test: Thread-0 is saling        ticket 94
    Test: Thread-1 is saling        ticket 93
    Test: Thread-0 is saling        ticket 92
    Test: Thread-1 is saling        ticket 91
                       。
                       。
                       。
    Test: Thread-1 is saling        ticket 15
    Test: Thread-0 is saling        ticket 14
    Test: Thread-1 is saling        ticket 13
    Test: Thread-0 is saling        ticket 12
    Test: Thread-1 is saling        ticket 11
    Test: Thread-0 is saling        ticket 10
    Test: Thread-1 is saling        ticket 9
    Test: Thread-0 is saling        ticket 8
    Test: Thread-1 is saling        ticket 7
    Test: Thread-0 is saling        ticket 6
    Test: Thread-1 is saling        ticket 5
    Test: Thread-0 is saling        ticket 4
    Test: Thread-1 is saling        ticket 3
    Test: Thread-0 is saling        ticket 2
    Test: Thread-1 is saling        ticket 1前面都有Test:,也就是说,这个程序始终调用的都是sale()方法,而我是想让代码块synchronized (this){}和sale()方法能分别调用。 这要怎么做呢?
      

  3.   

    你在 System.out.print("Test: ")前面做个判断就是了,我的想法是只在sale()做同步,好控制.
    说实在的,你的代码还没怎么看懂.如果str不等于method,怎么就运行了一次,没用循环.而且等于method又是个死循环.所以干脆自己写了一个.反正只要得到结果就好了.
      

  4.   

    这例子明显不对啊,
    while(true)
    {
      sale();
    }
    一个线程就把票都卖光了
      

  5.   

    这例子明显不对啊,  
    while(true)  
    {  
       sale();  
    }  
    一个线程就把票都卖光了 -----------那应该怎么做呢?
      

  6.   

    public class ThreadDemo
    {
     public static void main(String  args[])
     {
     ThreadTest t=new ThreadTest();  new Thread(t).start();  try
     {
     Thread.sleep(1);
     }
     catch(Exception e){}     // t.str=new String("method");  new Thread(t).start();
     }
    }
    class ThreadTest implements Runnable
    {
      private int tickets=100;
      //String str=new String(" ");
      public void run()
      {
      while(tickets > 0)
      {
      sale();
      try{Thread.sleep(1);}
       catch(Exception e){}
      }
       }
      public synchronized void sale()
      {
      if(tickets>0)
      {
      System.out.print("Test: ");
      System.out.println(Thread.currentThread().getName()+" is saling ticket" + tickets--);
      }
      }
    }你真正要的大概是这样吧!
      

  7.   

    也就是说如果2个线程操作的是同一个对象或者reference,那么一个对象的变异会影响到另外一个,那么我们就要利用面向对象的消息发送机制
      

  8.   

    racewind() :能否大致的写一下?谢谢!!