写了一个简单的Java多线程的例子模拟火车售票,但线程同步后,只有一个线程在卖票了,不知道哪儿出问题了,请高手指出来?谢谢!//线程类
class SellThread implements Runnable {
int tickets = 100;
public void run() {
synchronized (this) {
while (tickets > 0) {
System.out.println(Thread.currentThread().getName()
+ " sell ticket: " + tickets);
tickets--;
}
}
}
}//测试类
public class TicketsSystem {
public static void main(String[] args) {
SellThread st = new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}
}运行的结果是:只有第一个线程在卖票

解决方案 »

  1.   

    //线程类 
    class SellThread implements Runnable { 
    int tickets = 100; 
    public void run() {
    while(true){ 
    if(tickets>0){
    count();
    }else{
    break;
    }
    }
    }
    public void count(){
    synchronized(this)
    {
    System.out.println(Thread.currentThread().getName() 
    + " sell ticket: " + tickets); 
    tickets--; 
    }

    } //测试类 
    public class TicketsSystem { 
    public static void main(String[] args) { 
    SellThread st = new SellThread(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 


      

  2.   

    同步块 要去掉
    //线程类 
    class SellThread implements Runnable { 
    int tickets = 100; 
    public void run() {
    while(true){ 
    if(tickets>0){
    count();
    }else{
    break;
    }
    }
    }
    public void count(){
    System.out.println(Thread.currentThread().getName() 
    + " sell ticket: " + tickets); 
    tickets--; 

    } //测试类 
    public class TicketsSystem { 
    public static void main(String[] args) { 
    SellThread st = new SellThread(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 


      

  3.   


    public void count() {
    synchronized (this) {
    {
    System.out.println(Thread.currentThread().getName()
    + " sell ticket: " + tickets);
    tickets--;
    }
    try {
    Thread.sleep(10);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    加个sleep看的更加的清楚一些,楼主把 run方法的方法体给同步了 以后在里面循环没完 其他的线程肯定是没法执行的啊
      

  4.   

    是啊。。有-1要做控制//线程类 
    class SellThread implements Runnable { 
    int tickets = 100; 
    public void run() {
    while(true){ 
    if(tickets>0){
    count();
    }else{
    break;
    }
    }
    }
    public void count(){
    synchronized(this)
    {
    if(tickets>0){
    System.out.println(Thread.currentThread().getName() 
    + " sell ticket: " + tickets); 
    tickets--; 
    }else{
    System.out.println("票卖完了!!");
    }
    }

    } //测试类 
    public class TicketsSystem { 
    public static void main(String[] args) { 
    SellThread st = new SellThread(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 
    new Thread(st).start(); 


      

  5.   

    synchronized (this) { 
    while (tickets > 0) { 
    交换一下~~~~~~
      

  6.   

    你把run方法体给同步了,一个线程进去就锁住了,这个线程不出来,别的线程就不能运用run方法体了,当然 就一个线程在跑。
      

  7.   

    你的while(tickets>0)  
      一旦一个线程run起来后他一直占有对象锁直到运行到tickets<1释放对象锁
      其他线程就算得到对象锁这时tickets>0已经不满足,当然不打印语句了
      

  8.   

    按LZ的程序,只有Thread-0把tickets减完才能放弃锁,因为你的while(tickets > 0) 在synchronized里面。把synchronized块设置在while循环里面就可以了public void run() {
    while (tickets > 0) {
    synchronized (this) {
    System.out.println(Thread.currentThread().getName()
    + " sell ticket: " + tickets);
    tickets--;
    }
    }

    }
      

  9.   


    class Demo1 implements Runnable
    {
    private int ticket = 10 ;
    public void run()
    {
    while(this.ticket>0)
    {
    System.out.println("卖票:"+this.ticket--) ;
    }
    }
    };
    public class Test6
    {
    public static void main(String args[])
    {
    // 四个售票点应该控制同一个资源:10
    Demo d = new Demo() ; Thread t1 = new Thread(d) ;
    Thread t2 = new Thread(d) ;
    Thread t3 = new Thread(d) ;
    Thread t4 = new Thread(d) ;
    t1.start() ;
    t2.start() ;
    t3.start() ;
    t4.start() ;
    }
    };