1.没办法改进,cpu的问题,把票数改大点就自然了
2.其实A不需要三个成员变量,一个就够了
package com.thread;import java.util.*;public class Test
{
    public static void main(String[] args)
    {
        A aa1 = new A();
        aa1.setName("Thread1");
        aa1.start();
         
        A aa2 = new A();
        aa2.setName("Thread2");
        aa2.start();
         
        A aa3 = new A();
        aa3.setName("Thread3");
        aa3.start();
    }   
}
 
class A extends Thread
{
    private static int ticket = 1000;
    static String str = new String("qqq");
    private int count = 0;
    
    public void run()
    {
        while(true)       
        {
             
            synchronized(str)
            {
                if(ticket > 0)
                {               
                    Date time = new Date();               
                    //System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n", 
                     //                Thread.currentThread().getName(), ticket, time);
                    ticket--;   
                    count++;
                }   
              else
                {
                    break;   
                }   
            }
             
        }
         
        if(0 == ticket)
        {
           // System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n", 
            //                   count_0, count_1, count_2);  
            System.out.print(Thread.currentThread().getName()+ " = "+ count + " ");
        }
    }   
}
有个不明白的就是同步这个str是什么意思,请楼主解答下

解决方案 »

  1.   

    谢谢楼上,str是指static String str = new String("qqq");中的str,我是在网上看视频看到这个程序,也可以任意写个字符串进去,比如synchronized("abc"),更具体地我也说不明白,因为我刚接触进程。
      

  2.   

    另外,我的utraledet好像不用加package com.thread;,加上会报异常。
      

  3.   

    请问gmh0421,“2.其实A不需要三个成员变量,一个就够了”这一步怎样实现呢?
      

  4.   

    synchronized(str)是为了同步各线程
    如果没有这句,可能出现多个线程卖同一张票的情况
      

  5.   


    就是在类里面只定义一个成员变量就可以了啊,你的三个实例各自修改自己的成员变量不就能实现你要的功能么我只会用接口Runnable,然后再试着用一个变量,但是会出现每个站点都出1000张票的情况,可能我没有理解到您的意思,请您再再我根据您的提示所做的修改,请再次改正。import java.util.*;
     
    public class Test_2
    {
        public static void main(String[] args)
        {
        
         A aa = new A();
        
         Thread t1 = new Thread(aa);
         t1.start();
        
         Thread t2 = new Thread(aa);
         t2.start();
        
         Thread t3 = new Thread(aa);
         t3.start();
        
        }   
    }
      
    class A implements Runnable
    {
        private static int ticket = 1000;
        String str = new String("qqq");
        private int count = 0;
         
        public void run()
        {
            while(true)       
            {
                  
                synchronized(str)
                {
                    if(ticket > 0)
                    {               
                        Date time = new Date();               
                        //System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n", 
                         //                Thread.currentThread().getName(), ticket, time);
                        ticket--;   
                        count++;
                    }   
                  else
                    {
                        break;   
                    }   
                }
                  
            }
              
            if(0 == ticket)
            {
               // System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n", 
                //                   count_0, count_1, count_2);  
                System.out.println(Thread.currentThread().getName()+ " = "+ count + " ");
            }
        }   
    }会出现都满票售出。
      

  6.   

    你只实例化了1个,你这样只是把一个类用三个线程来运行而已,要new出三个类来才行,那个str我改成static,这样才行,不然不能实现同步import java.util.*;
      
    public class Test
    {
        public static void main(String[] args)
        {
             
            A aa = new A();
            A bb = new A();
            A cc = new A();
            Thread t1 = new Thread(aa);
            t1.start();
             
            Thread t2 = new Thread(bb);
            t2.start();
             
            Thread t3 = new Thread(cc);
            t3.start();
             
        }   
    }
       
    class A implements Runnable
    {
        private static int ticket = 1000;
        static String str = new String("qqq");
        private int count = 0;
          
        public void run()
        {
            while(true)       
            {
                   
                synchronized(str)
                {
                    if(ticket > 0)
                    {               
                       Date time = new Date();               
                        //System.out.printf("%s is doing, and No.%d is sold at the time of %s.\n", 
                         //                Thread.currentThread().getName(), ticket, time);
                        ticket--;   
                        count++;
                    }   
                  else
                    {
                        break;   
                    }   
                }
                   
            }
               
           
               // System.out.printf("\ncount_0 = %d, count_1 = %d, count_2 = %d.\n", 
                //                   count_0, count_1, count_2);  
                System.out.println(Thread.currentThread().getName()+ " = "+ count + " ");
            System.out.println(ticket);
        }   
    }
      

  7.   

    但是您的程序并没有体现您说的“.其实A不需要三个成员变量,一个就够了”这一特性呀?是我没有理解对吗?因为您也new出来了三个A类的变量aa,bb,cc呀?
      

  8.   

    难道您说的是count吗,只要一个count就行,而不是像我那样写成三个,count_0,count_1,count_2.您说的是这个吗?还是我理解错了?
      

  9.   

    是我没说清楚,我说的是count