首页 > 代码库 > java的多线程初体验
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:59Ticket Seller NO.1, get ticket:41Ticket Seller NO.1, get ticket:67Ticket Seller NO.2, get ticket:63Ticket Seller NO.2, get ticket:87Ticket Seller NO.2, get ticket:23Ticket Seller NO.1, get ticket:80Ticket Seller NO.2, get ticket:36Ticket Seller NO.2, get ticket:20Ticket Seller NO.3, get ticket:9Ticket Seller NO.1, get ticket:6Ticket Seller NO.3, get ticket:81Ticket Seller NO.2, get ticket:82Ticket Seller NO.3, get ticket:38Ticket Seller NO.1, get ticket:19Ticket Seller NO.3, get ticket:26Ticket Seller NO.2, get ticket:1Ticket Seller NO.3, get ticket:84Ticket Seller NO.1, get ticket:51Ticket Seller NO.3, get ticket:89Ticket Seller NO.2, get ticket:31Ticket Seller NO.3, get ticket:2Ticket Seller NO.1, get ticket:34Ticket Seller NO.3, get ticket:78Ticket Seller NO.2, get ticket:11Ticket Seller NO.3, get ticket:83Ticket Seller NO.1, get ticket:73Ticket Seller NO.3, get ticket:61Ticket Seller NO.2, get ticket:56Ticket Seller NO.3, get ticket:68Ticket Seller NO.1, get ticket:16Ticket Seller NO.3, get ticket:48Ticket Seller NO.2, get ticket:18Ticket Seller NO.3, get ticket:4Ticket Seller NO.1, get ticket:49Ticket Seller NO.1, get ticket:53Ticket Seller NO.1, get ticket:85Ticket Seller NO.1, get ticket:13Ticket Seller NO.1, get ticket:57Ticket Seller NO.3, get ticket:93Ticket Seller NO.3, get ticket:42Ticket Seller NO.3, get ticket:46Ticket Seller NO.3, get ticket:44Ticket Seller NO.3, get ticket:58Ticket Seller NO.3, get ticket:70Ticket Seller NO.3, get ticket:33Ticket Seller NO.3, get ticket:76Ticket Seller NO.3, get ticket:47Ticket Seller NO.3, get ticket:92Ticket Seller NO.3, get ticket:55Ticket Seller NO.3, get ticket:75Ticket Seller NO.3, get ticket:96Ticket Seller NO.3, get ticket:77Ticket Seller NO.3, get ticket:45Ticket Seller NO.3, get ticket:28Ticket Seller NO.3, get ticket:88Ticket Seller NO.3, get ticket:72Ticket Seller NO.3, get ticket:8Ticket Seller NO.3, get ticket:10Ticket Seller NO.3, get ticket:62Ticket Seller NO.3, get ticket:24Ticket Seller NO.3, get ticket:86Ticket Seller NO.3, get ticket:97Ticket Seller NO.3, get ticket:5Ticket Seller NO.3, get ticket:21Ticket Seller NO.3, get ticket:0Ticket Seller NO.3, get ticket:7Ticket Seller NO.3, get ticket:22Ticket Seller NO.3, get ticket:54Ticket Seller NO.3, get ticket:98Ticket Seller NO.3, get ticket:95Ticket Seller NO.3, get ticket:52Ticket Seller NO.3, get ticket:66Ticket Seller NO.3, get ticket:3Ticket Seller NO.3, get ticket:50Ticket Seller NO.3, get ticket:27Ticket Seller NO.3, get ticket:40Ticket Seller NO.3, get ticket:74Ticket Seller NO.2, get ticket:25Ticket Seller NO.2, get ticket:91Ticket Seller NO.3, get ticket:94Ticket Seller NO.3, get ticket:71Ticket Seller NO.3, get ticket:65Ticket Seller NO.3, get ticket:60Ticket Seller NO.3, get ticket:29Ticket Seller NO.3, get ticket:35Ticket Seller NO.3, get ticket:90Ticket Seller NO.3, get ticket:43Ticket Seller NO.3, get ticket:39Ticket Seller NO.3, get ticket:69Ticket Seller NO.3, get ticket:37Ticket Seller NO.3, get ticket:14Ticket Seller NO.3, get ticket:99Ticket Seller NO.3, get ticket:32Ticket Seller NO.3, get ticket:12Ticket Seller NO.3, get ticket:64Ticket Seller NO.3, get ticket:30Ticket Seller NO.3, get ticket:79Ticket Seller NO.3, sold out.Ticket Seller NO.1, get ticket:15Ticket Seller NO.1, sold out.Ticket Seller NO.2, get ticket:17Ticket Seller NO.2, sold out.
参考文章:http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html
java的多线程初体验
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。