java的多线程初体验

这里以车站售票作为模拟场景,多个票务人员同时出售某一列高铁(G250?)的车票,售票的基本保证有2点:

  1、不能售出多于预定限额的车票。

  2、不能售出具有相同座位的车票。

初学java,有不当的地方各位看官请指教~

代码如下:

package threadjava;

public class ThreadTest4 {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //限定总票数为100张
        Seller seller = new Seller(100);
        //开放三个窗口售票
        Worker w1 = new Worker(seller);
        Worker w2 = new Worker(seller);
        Worker w3 = new Worker(seller);
    }

}

//打印车票的机器,作为服务端可连接多个客户端运行使用
class Seller {
    private int[] tickets;
    private int sold_line;

    public Seller(int ticket_total) {
        this.tickets = getTickets(ticket_total);
        this.sold_line = ticket_total;
    }

    //该方法是个独占性方法,即同一时刻只允许一个线程调用
    //也是java多线程进行同步操作的主要实现部分
    public synchronized int sell() {
        if(this.sold_line != 0){
            int ticket_pos = (int)(Math.random()*this.sold_line);
            int ticket = this.tickets[ticket_pos];
            this.sold_line = this.sold_line - 1;
            swap(this.tickets, ticket_pos, this.sold_line);
            return ticket;
        }else{
            return -1;
        }
    }

    private static void swap(int[] a, int x, int y){
        int temp = a[x];
        a[x] = a[y];
        a[y] = temp;
    }

    private static void shfulle(int[] a) {
        int x, y;
        for(x=0; x<a.length; x++){
            y = (int)(Math.random() * (x+1));
            swap(a, x, y);
        }
    }

    private static int[] getTickets(int total){
        int i;
        int[] a = new int[total];
        for(i=0; i<total; i++){
            a[i] = i;
        }
        shfulle(a);
        return a;
    }
}

//售票人员,被激活后就立即开始售票。哈哈,真是敬业~
class Worker extends Thread {
    private static int threadID = 0;
    private Seller tickor;

    public Worker(Seller seller) {
        super("Ticket Seller NO." + (++threadID));
        this.tickor = seller;
        this.start();
    }

    public void run() {
        while(true){
            int ticket = this.tickor.sell();
            if(ticket == -1){
                System.out.println(this.getName() + ", sold out.");
                break;
            }else{
                System.out.println(this.getName() + ", get ticket:" + ticket);
            }
        }
    }
}

出票结果:

Ticket Seller NO.1, get ticket:59
Ticket Seller NO.1, get ticket:41
Ticket Seller NO.1, get ticket:67
Ticket Seller NO.2, get ticket:63
Ticket Seller NO.2, get ticket:87
Ticket Seller NO.2, get ticket:23
Ticket Seller NO.1, get ticket:80
Ticket Seller NO.2, get ticket:36
Ticket Seller NO.2, get ticket:20
Ticket Seller NO.3, get ticket:9
Ticket Seller NO.1, get ticket:6
Ticket Seller NO.3, get ticket:81
Ticket Seller NO.2, get ticket:82
Ticket Seller NO.3, get ticket:38
Ticket Seller NO.1, get ticket:19
Ticket Seller NO.3, get ticket:26
Ticket Seller NO.2, get ticket:1
Ticket Seller NO.3, get ticket:84
Ticket Seller NO.1, get ticket:51
Ticket Seller NO.3, get ticket:89
Ticket Seller NO.2, get ticket:31
Ticket Seller NO.3, get ticket:2
Ticket Seller NO.1, get ticket:34
Ticket Seller NO.3, get ticket:78
Ticket Seller NO.2, get ticket:11
Ticket Seller NO.3, get ticket:83
Ticket Seller NO.1, get ticket:73
Ticket Seller NO.3, get ticket:61
Ticket Seller NO.2, get ticket:56
Ticket Seller NO.3, get ticket:68
Ticket Seller NO.1, get ticket:16
Ticket Seller NO.3, get ticket:48
Ticket Seller NO.2, get ticket:18
Ticket Seller NO.3, get ticket:4
Ticket Seller NO.1, get ticket:49
Ticket Seller NO.1, get ticket:53
Ticket Seller NO.1, get ticket:85
Ticket Seller NO.1, get ticket:13
Ticket Seller NO.1, get ticket:57
Ticket Seller NO.3, get ticket:93
Ticket Seller NO.3, get ticket:42
Ticket Seller NO.3, get ticket:46
Ticket Seller NO.3, get ticket:44
Ticket Seller NO.3, get ticket:58
Ticket Seller NO.3, get ticket:70
Ticket Seller NO.3, get ticket:33
Ticket Seller NO.3, get ticket:76
Ticket Seller NO.3, get ticket:47
Ticket Seller NO.3, get ticket:92
Ticket Seller NO.3, get ticket:55
Ticket Seller NO.3, get ticket:75
Ticket Seller NO.3, get ticket:96
Ticket Seller NO.3, get ticket:77
Ticket Seller NO.3, get ticket:45
Ticket Seller NO.3, get ticket:28
Ticket Seller NO.3, get ticket:88
Ticket Seller NO.3, get ticket:72
Ticket Seller NO.3, get ticket:8
Ticket Seller NO.3, get ticket:10
Ticket Seller NO.3, get ticket:62
Ticket Seller NO.3, get ticket:24
Ticket Seller NO.3, get ticket:86
Ticket Seller NO.3, get ticket:97
Ticket Seller NO.3, get ticket:5
Ticket Seller NO.3, get ticket:21
Ticket Seller NO.3, get ticket:0
Ticket Seller NO.3, get ticket:7
Ticket Seller NO.3, get ticket:22
Ticket Seller NO.3, get ticket:54
Ticket Seller NO.3, get ticket:98
Ticket Seller NO.3, get ticket:95
Ticket Seller NO.3, get ticket:52
Ticket Seller NO.3, get ticket:66
Ticket Seller NO.3, get ticket:3
Ticket Seller NO.3, get ticket:50
Ticket Seller NO.3, get ticket:27
Ticket Seller NO.3, get ticket:40
Ticket Seller NO.3, get ticket:74
Ticket Seller NO.2, get ticket:25
Ticket Seller NO.2, get ticket:91
Ticket Seller NO.3, get ticket:94
Ticket Seller NO.3, get ticket:71
Ticket Seller NO.3, get ticket:65
Ticket Seller NO.3, get ticket:60
Ticket Seller NO.3, get ticket:29
Ticket Seller NO.3, get ticket:35
Ticket Seller NO.3, get ticket:90
Ticket Seller NO.3, get ticket:43
Ticket Seller NO.3, get ticket:39
Ticket Seller NO.3, get ticket:69
Ticket Seller NO.3, get ticket:37
Ticket Seller NO.3, get ticket:14
Ticket Seller NO.3, get ticket:99
Ticket Seller NO.3, get ticket:32
Ticket Seller NO.3, get ticket:12
Ticket Seller NO.3, get ticket:64
Ticket Seller NO.3, get ticket:30
Ticket Seller NO.3, get ticket:79
Ticket Seller NO.3, sold out.
Ticket Seller NO.1, get ticket:15
Ticket Seller NO.1, sold out.
Ticket Seller NO.2, get ticket:17
Ticket Seller NO.2, sold out.

参考文章:http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html

上一篇:python多版本的pip共存问题解决办法


下一篇:Jmeter(二十六)_数据驱动测试