| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:一个关于多线程同步的问题。
只看楼主 加入收藏
jy_lance
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-8-23
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:4 
一个关于多线程同步的问题。
问题:
假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒买一张票。打印出售票过程,注意使用synchronized确保同一张票只能卖出一次。输出格式如下:
第4售票点卖出第100张票
第2售票点卖出第101张票 ……
我的解答是:
package ti2;

public class TicketsSell implements Runnable {

    static int tickets = 1;

    int num;

    public TicketsSell(int num) {
        this.num = num;
    }

    public synchronized void run() {
        for (; tickets <= 100; tickets++) {
            System.out.println("第" + num + "售票点卖出第" + tickets + "张票");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


package ti2;

public class Station {

    public static void main(String[] args) {
        
        Thread wicket1 = new Thread(new TicketsSell(1));
        Thread wicket2 = new Thread(new TicketsSell(2));
        Thread wicket3 = new Thread(new TicketsSell(3));
        Thread wicket4 = new Thread(new TicketsSell(4));
        Thread wicket5 = new Thread(new TicketsSell(5));
        Thread wicket6 = new Thread(new TicketsSell(6));
        Thread wicket7 = new Thread(new TicketsSell(7));
        Thread wicket8 = new Thread(new TicketsSell(8));
        Thread wicket9 = new Thread(new TicketsSell(9));
        Thread wicket10 = new Thread(new TicketsSell(10));
        wicket1.start();
        wicket2.start();
        wicket3.start();
        wicket4.start();
        wicket5.start();
        wicket6.start();
        wicket7.start();
        wicket8.start();
        wicket9.start();
        wicket10.start();

    }

}
运行结果就是不对!!其中老是出现不同售票点卖出同一张票!请各位高手指点指点!
搜索更多相关主题的帖子: 火车票 多线程 售票点 
2011-04-07 22:24
w123012306
Rank: 9Rank: 9Rank: 9
来 自:湖南
等 级:蜘蛛侠
威 望:4
帖 子:307
专家分:1180
注 册:2010-4-22
收藏
得分:10 
程序修改如下:我把2个类放在一个包中了,synchronized不应在run方法中!  
试下!

class TicketsSell implements Runnable {
         
   
         static int tickets=1;
    int num;

    public TicketsSell(int num) {
        this.num = num;
    }

    public  void run() {
       synchronized(this){
        for (int i=0; i< 100; i++) {
        
           if(tickets<101) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第" + num + "售票点卖出第" + (tickets++) + "张票");
            }
        }
        }
    }
}




public class Station {

    public static void main(String[] args) {
        
        Thread wicket1 = new Thread(new TicketsSell(1));
        Thread wicket2 = new Thread(new TicketsSell(2));
        Thread wicket3 = new Thread(new TicketsSell(3));
        Thread wicket4 = new Thread(new TicketsSell(4));
        Thread wicket5 = new Thread(new TicketsSell(5));
        Thread wicket6 = new Thread(new TicketsSell(6));
        Thread wicket7 = new Thread(new TicketsSell(7));
        Thread wicket8 = new Thread(new TicketsSell(8));
        Thread wicket9 = new Thread(new TicketsSell(9));
        Thread wicket10 = new Thread(new TicketsSell(10));
        wicket1.start();
        wicket2.start();
        wicket3.start();
        wicket4.start();
        wicket5.start();
        wicket6.start();
        wicket7.start();
        wicket8.start();
        wicket9.start();
        wicket10.start();

    }

}

楼上,楼下的一定要幸福开心哦!
2011-04-08 15:58
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:10 
程序代码:
public class TicketsShop extends JApplet {
    private AtomicInteger ticketNum = new AtomicInteger(0);
    private int ticketNum2 = 0;

    public int sell() {
        return ticketNum.getAndIncrement();
    }

    public synchronized int sell2() {
        ticketNum2++;
        return ticketNum2;
    }

    public void test2() {
        long start = System.currentTimeMillis();
        List<TicketsSell2> t2 = new ArrayList<TicketsSell2>();
        for (int i = 0; i < 10; i++) {
            TicketsSell2 t = new TicketsSell2(i+1);
            t2.add(t);
            t.start();
        }
        for(TicketsSell2 t : t2){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public void test1() {
        long start = System.currentTimeMillis();
        TicketsSell wicket1 = new TicketsSell(1);
        TicketsSell wicket2 = new TicketsSell(2);
        TicketsSell wicket3 = new TicketsSell(3);
        TicketsSell wicket4 = new TicketsSell(4);
        TicketsSell wicket5 = new TicketsSell(5);
        TicketsSell wicket6 = new TicketsSell(6);
        TicketsSell wicket7 = new TicketsSell(7);
        TicketsSell wicket8 = new TicketsSell(8);
        TicketsSell wicket9 = new TicketsSell(9);
        TicketsSell wicket10 = new TicketsSell(10);
        wicket1.start();
        wicket2.start();
        wicket3.start();
        wicket4.start();
        wicket5.start();
        wicket6.start();
        wicket7.start();
        wicket8.start();
        wicket9.start();
        wicket10.start();
        try {
            wicket1.join();
            wicket2.join();
            wicket3.join();
            wicket4.join();
            wicket5.join();
            wicket6.join();
            wicket7.join();
            wicket8.join();
            wicket9.join();
            wicket10.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();

        System.out.println(end - start);
    }

    public static void main(String[] args) {
        TicketsShop shop = new TicketsShop();
//        shop.test1();
        shop.test2();
    }

    class TicketsSell extends Thread {
        int num;
        int count = 0;

        public TicketsSell(int num) {
            this.num = num;
        }

        public void run() {
            while (count < 100) {
                int ticketNum = sell() + 1;
                System.out.println("第" + num + "售票点卖出第" + ticketNum + "张票");
                count++;
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    class TicketsSell2 extends Thread {
        int num;
        int count = 0;

        public TicketsSell2(int num) {
            this.num = num;
        }

        public void run() {
            while (count < 100) {
                System.out.println("第" + num + "售票点卖出第" + sell2() + "张票");
                count++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
2011-04-08 17:54
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
其实最好的方法是Lock来做
2011-04-08 18:00
jy_lance
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-8-23
收藏
得分:0 
回复 2楼 w123012306
首先谢谢你的解答。因为在实际卖票活动中,票号必须是从小到大地卖出的,所以你这个还是有点问题!
2011-04-09 11:11
快速回复:一个关于多线程同步的问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.062658 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved