资源简介
并发多数据与多核编程课程实践作业:
1.1代码清单:
(1).TicketingDS.java文件,TicketingDS类是实现并发数据结构的类。
1⃣️方法:TicketingDS(int routenum, int coachnum, int seatnum, int stationnum, int threadnum),初始化方法,用来初始化并发数据结构和变量。
2⃣️方法:public Ticket buyTicket(String passenger, int route, int departure, int arrival),购票方法,乘客passenger购买route车次从department站到arrival站的车票一张。若购票成功,返回有效的Ticket对象,即return ticket;若购票失败,返回无效的Ticket对象,即return null。
3⃣️方法: public int inquiry(int route, int departure, int arrival),查询余票的方法,查询route车次从department站到arrival站的余票数。
4⃣️方法:public boolean refundTicket(Ticket ticket),退票方法,对有效的Ticket对象返回true,对无效的Ticket对象返回false。
(2).TicketingSystem.java文件,是一个接口类。
(3).Trace.java文件,主函数类,用来调用实现的各个方法。
(4).test.java文件,测试类,在各个threadnum和testnum情况下,计算吞吐量。
(5).Verify.java文件,快照类,显示每一个车次所有座位的预定情况,给开发者以参考,验证正确性。
代码片段和文件信息
package ticketingsystem;
import java.util.ArrayList;
import java.util.Random;
public class TdsThread extends Thread {
private TicketingDS tds;
// private int interval;
// private int start;
private int testnum;
long starttime;
long endtime;
volatile int i;
// Time time;
volatile long buytime;
volatile long inqtime;
volatile long reftime;
int buycount;
int inqcount;
int refcount;
public TdsThread(TicketingDS tds int testnum) {
this.tds = tds;
this.testnum = testnum; // �������
}
// public class Time{
// ArrayList time = new ArrayList();
// public void time(long i){};
// public void addTid(long i){
// time.add(i);
// }
// public void removeTid(long i){
// time.remove(i);
// }
// public boolean containsTid(long i){
// return time.contains(i);
// }
// }
public void run() {
starttime=System.currentTimeMillis();
int THREAD_EXE_NUM = this.testnum;
Random rand = new Random();
ArrayList soldTicket = new ArrayList();
Ticket ticket = new Ticket();
for ( i = 0; i < this.testnum; i++) {
//System.out.println(Thread.currentThread().getName() + “ “ + i);
String passenger = “cwg“+Thread.currentThread().getId()+i;
int route = rand.nextInt(5)+1;
int departure = rand.nextInt(9)+1;
int arrival = rand.nextInt(10-departure)+1 +departure;
int num = 0;
int departure2 = rand.nextInt(9)+1;
int arrival2 = rand.nextInt(10-departure2)+1 +departure2;
boolean refund = false;
Random ra = new Random();
int t = ra.nextInt(THREAD_EXE_NUM)%10;
System.out.println(“t is “ + t);
if(t==3|t==6|t==9){
long buystarttime;
long buyendtime;
buystarttime = System.currentTimeMillis();
System.out.println(“buy route is “ + route);
ticket = tds.buyTicket(passenger route departure arrival);
System.out.println(“after buy route is “ + route);
buyendtime = System.currentTimeMillis();
buytime=buytime+(buyendtime-buystarttime);
buycount ++;
if(ticket != null) {
//soldTicket.add(ticket);
continue;
}
// System.out.println(System.nanoTime()+“ ����Ʊ��Ϊ “ + ticket.tid + “ �� “+ ticket.passenger +
// “ �˿����� “+ticket.route +“ ���� “+ticket.coach+“ ���� “ +ticket.seat +“ ��λ�Ĵӵ� “ +
// ticket.departure +“ վ���� “ +ticket.arrival + “ վ�ij�Ʊһ��.“);
}
else if(t==1 | t==2 | t==4|t==5|t==7|t==8){
long inqstarttime;
long inqendtime;
inqstarttime = System.currentTimeMillis();
System.out.println(“inquary route is “ + route);
num = tds.inquiry(route departure2 arrival2);
inqendtime = System.currentTimeMillis();
inqtime=inqtime+(inqendtime-inqstarttime);
inqcount ++;
continue;
// System.out.println( System.nanoTime()+“ ��ѯ������Ϊ “ + route + “ �Ĵӵ� “ + departure2 +
// “ վ���� “ + arrival2 + “ վ����ƱΪ “ + num +“ ��.“);
}
else if(t==0)
//
if(ticket != null) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-18 11:27 ticketingsystem\
文件 470 2017-12-18 16:24 ticketingsystem\Test2.java
目录 0 2018-09-21 22:25 __MACOSX\
目录 0 2018-09-21 22:25 __MACOSX\ticketingsystem\
文件 188 2017-12-18 16:24 __MACOSX\ticketingsystem\._Test2.java
文件 4332 2017-12-18 10:54 ticketingsystem\Test.java
文件 188 2017-12-18 10:54 __MACOSX\ticketingsystem\._Test.java
文件 2721 2017-12-18 16:44 ticketingsystem\Test1.java
文件 188 2017-12-18 16:44 __MACOSX\ticketingsystem\._Test1.java
文件 343 2017-11-22 20:54 ticketingsystem\TicketingSystem.java
文件 188 2017-11-22 20:54 __MACOSX\ticketingsystem\._TicketingSystem.java
文件 3901 2017-12-20 17:34 ticketingsystem\TdsThread.java
文件 188 2017-12-20 17:34 __MACOSX\ticketingsystem\._TdsThread.java
文件 4561 2017-11-22 20:54 ticketingsystem\Trace.java
文件 188 2017-11-22 20:54 __MACOSX\ticketingsystem\._Trace.java
文件 6199 2017-12-21 20:27 ticketingsystem\TicketingDS.java
文件 244 2017-12-21 20:27 __MACOSX\ticketingsystem\._TicketingDS.java
文件 188 2017-12-18 11:27 __MACOSX\._ticketingsystem
- 上一篇:Servlet外文翻译
- 下一篇:Java记事本源码和课程设计报告
评论
共有 条评论