孙鑫java视频——多线程总结

//模拟火车票售票系统

class TicketsThread{

       public static void main(String args[])

       {

              SellThread mythread=new SellThread();

              new Thread(mythread).start();

              new Thread(mythread).start();

              new Thread(mythread).start();

              new Thread(mythread).start();

              //4个线程同时进行售票

       }

}

class SellThread implements Runnable{

       int tickets=100;//一共要卖100张票

       public void run()

       {

              while(true){

                     if(tickets>0){

                            try{

                                   Thread.sleep(10);//10毫秒用来处理一些数据或其他售票过程

                            }

                            catch(Exception e){

                                   e.printStackTrace();

                            }

                            System.out.println(Thread.currentThread().getName()+"sell ticket:"+tickets);

                            tickets--;

                     }

              }    

       }

}

 

问题1Runnable Thread有什么关系和区别?

答:实现多线程程序的两种方式:

    1)从Thread类继承;

    2)实现Runnable接口。

Thread类实现了Runnable接口。

如果多个线程访问同一个资源(比如变量),那么必须使用方式(2)。

除非要使用Thread中除run方法以外的其他方法,才用方式(1)。一般,只需用方法(2)。

问题2:如果运行上面这个程序,输出是:

.......

//把前面正常的输出省略了

Thread-3sell tickets 1

Thread-0sell tickets 0

Thread-1sell tickets -1

Thread-2sell tickets -2

很明显,结果是错误的,怎么可能卖出0-1-2这三张票??

关键就在Thread.sleep(10);这里。在这里,四个进程的时间片发生了轮换。

我们可以使用同步机制解决这个问题。这里不做解释,看代码:

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();

       }

}

 

class SellThread implements Runnable

{

       int tickets=100;

       Object obj=new Object();

       public void run()

       {

                     while(true)

                     {

                            synchronized(obj)

                            {

                                   try

                                   {

                                          Thread.sleep(10);

                                   }

                                   catch(Exception e)

                                   {

                                          e.printStackTrace();

                                   }

 

                                   if(tickets>0)

                                   {

                                                

                                          System.out.println("obj:"+Thread.currentThread().getName()+" sell tickets:"+tickets);

                                          tickets--;

                                   }

                                   //或者调用sell()                        

                            }

                     }

       }

}

       public synchronized void sell()

       {

              synchronized(obj)

              {

                     if(tickets>0)

                     {

                            try

                            {

                                   Thread.sleep(10);

                            }

                            catch(Exception e)

                            {

                                   e.printStackTrace();

                            }

                            System.out.println("sell():"+Thread.currentThread().getName()+

                                          " sell tickets:"+tickets);

                            tickets--;

                     }

              }

       }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值