A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© pengbin 中级黑马   /  2015-7-21 09:59  /  557 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


/*
* 需求:简单的卖票程序。
* 多个窗口买票
*
* 创建线程的第二种方式:实现Runnable接口
*
* 步骤:
* 1.定义类实现Runnable接口
* 2.覆盖Runnable接口中的run方法
*                 将线程要运行的代码放在该run方法中
*
* 3.通过Thread类建立线程对象
* 4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数
*                 为什么要将Runable接口的子类对象传递给Thread的构造函数。
*                 因为,自定义的run方法所属的对象是Runnable接口的子类对象
*                 所以要让线程去执行指定对象的run方法。就必须明确该run方法所属对象。
*
* 5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法
*
*
*
* 实现方式和继承方式有什么区别呢?
*
* 实现方式的好处:避免了单继承的局限性。
*
* java对于多线程的安全问题提供了专门的解决方式。
* 同步代码块:
*
* synchronized(对象)
* {
*                 需要被同步的代码
* }
*
* 同步的前提:
* 1.必须要有两个或者两个以上的线程。
* 2.必须是多个线程使用同一个锁。
*
* 好处:解决了多线程安全问题
* 弊端:比较消耗资源
*/


//方法1:不适用
/*
class Ticket extends Thread
{
        private static int tick=100;//必须定义静态,不然每个线程都有100张票
        public void run()
        {
                while(true)
                {
                        if(tick>0)
                        {
                                System.out.println(this.getName()+"....sale:"+tick--);
                        }
                }
        }
}

public class ThreadDemo2
{
        public static void main(String[] args)
        {
                Ticket t1 = new Ticket();
                Ticket t2 = new Ticket();
                Ticket t3 = new Ticket();
                Ticket t4 = new Ticket();
               
                t1.start();
                t2.start();
                t3.start();
                t4.start();
               
        }
}
*/

class Ticket implements Runnable
{
        private int tick=TicketSealCenter.num;
        Object obj = new Object();
        public void run()
        {
                while(true)
                {
                        synchronized(obj)
                        {
                                if(tick>0)
                                {
                                        String name = Thread.currentThread().getName();
                                        int windows = Integer.parseInt(name.substring(7, name.length()));
                                        try{Thread.sleep(100);}catch(Exception e){}
                                        System.out.println("第"+(windows+1)+"窗口卖出了第"+tick--+"张票");
                                }
                        }
                       
                }
        }
}
//售票窗口通过for循环控制窗口数量
class SealWindow
{
        public static void addWindow()
        {
                Ticket t = new Ticket();
                for(int x=1;x<=4;x++)
                {
                        Thread t1 = new Thread(t);
                        t1.start();
                }
        }
}

//售票中心控制票数
class TicketSealCenter
{
        public static int num=100;
}

public class ThreadDemo2
{
        public static void main(String[] args)
        {
                SealWindow.addWindow();
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马