小弟跪拜求帮忙。银行排队信号量多线程问题求解决非常感谢,希望可以帮我改改。Java。

weixin_41191839 2018-05-16 02:49:12
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class Assignment2 {

public static void main(String args[]) {
CustomerThreads test = new CustomerThreads();

//newThread test2 = new ;

int Tellers=3, MeanAverageArrivalsTime=10, MeanAvergeServiceTime=30, LengthOfTime=70;
/*
Tellers=Integer.parseInt(args[0]);
MeanAverageArrivalsTime=Integer.parseInt(args[1]);//arrival time
MeanAvergeServiceTime=Integer.parseInt(args[2]);//service time
LengthOfTime=Integer.parseInt(args[3]);
*/
//ExecutorService executorService = new ThreadPoolExecutor(3,10,3000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>(50));
//final Semaphore sem= new Semaphore(Tellers);//how many teller we had.

new CustomerThreads(Tellers,MeanAverageArrivalsTime,MeanAvergeServiceTime,LengthOfTime);
Random_Int_Mean r=new Random_Int_Mean();
System.out.println("Mean inter-arrival time:"+MeanAverageArrivalsTime);
System.out.println("Mean service time::"+MeanAvergeServiceTime);
System.out.println("Number of tellers:"+Tellers);
System.out.println("Length of simulation: "+LengthOfTime );
final Semaphore semaphore = new Semaphore(Tellers);

newThread text2= new newThread(semaphore,MeanAverageArrivalsTime,MeanAvergeServiceTime,LengthOfTime);
//System.out.println(r.random_int(MeanAvergeServiceTime));
//System.out.println(r.random_int(MeanAverageArrivalsTime));
test.CustomerThread();
text2.run();
//test.CustomerThread();


}


}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


public class CustomerThreads{
int cn;
private int Tellers,MeanAverageArrivalsTime,MeanAvergeServiceTime,LengthOfTime;



public CustomerThreads() {
cn=0;
}
public synchronized int getNextCustomer() {
return cn=cn+1;
}

public CustomerThreads(int T, int MeanAT,int MeanAvT,int LT) {
this.Tellers=T;
this.MeanAverageArrivalsTime=MeanAT;
this.MeanAvergeServiceTime=MeanAvT;
this.LengthOfTime=LT;
}

public void CustomerThread() {

Random_Int_Mean rd= new Random_Int_Mean();
int random=rd.random_int(MeanAverageArrivalsTime);
final Semaphore semaphore = new Semaphore(Tellers);
newThread nt=new newThread(semaphore,random,MeanAvergeServiceTime,LengthOfTime);
ExecutorService es = Executors.newCachedThreadPool();

for (int i = 0; i < LengthOfTime; i++) {
es.submit(new Thread(nt));

}

es.shutdown();

semaphore.acquireUninterruptibly(Tellers);
System.out.println("Time Up");
semaphore.release(Tellers);

}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.concurrent.Semaphore;

public class newThread implements Runnable{

private int waittime=0;
CustomerThreads ct=new CustomerThreads();
private Semaphore Semaphore;
private int MeanAverageArrivalsTime,MeanAvergeServiceTime,LengthOfTime;
private Random_Int_Mean r=new Random_Int_Mean();

public newThread(Semaphore Semaphore, int MeanAverageArrivalsTime,int MeanAvergeServiceTime,int LengthOfTime) {
this.Semaphore=Semaphore;
this.MeanAverageArrivalsTime=MeanAverageArrivalsTime;
this.MeanAvergeServiceTime=MeanAvergeServiceTime;
this.LengthOfTime=LengthOfTime;
}


@Override
public void run() {
int Customer=ct.getNextCustomer();
int MeanAvergeServiceT=r.random_int(MeanAvergeServiceTime);

while(Semaphore.availablePermits() !=0){
if (Semaphore.availablePermits() > 0) {
System.out.println("At time " +MeanAvergeServiceTime+",customer "+ Customer + " arrives in line");
System.out.println("At time " +MeanAvergeServiceTime+",customer "+ Customer + " starts being served");
//System.out.println(Semaphore.availablePermits());
} else {
System.out.println("At time " +MeanAvergeServiceTime+",customer "+ Customer + " arrives in line");
waittime++;
}try {
Semaphore.acquire();
Thread.sleep(MeanAvergeServiceT*1000);
//r.random_int(MeanAvergeServiceTime);
System.out.println("At time " +MeanAvergeServiceTime+",customer "+ Customer + " leaves the bank");
Semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();

}

}


System.out.println(Semaphore.availablePermits());
waittime=waittime/LengthOfTime;
System.out.println("Simulation terminated after " +Customer+ " customers served");
System.out.println("Average waiting time = "+ waittime);

}




}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Random;

public final class Random_Int_Mean {

static int initialized = 0;
static Random r = new Random();

public static void init_random_int(int seed)
{
/*
* Initializes the random number generator. If seed is negative then the
* system clock is used to initialize the generator. A count is also kept
* of the number of times this routine has been called.
*/
if (seed < 0) seed = (int) (System.currentTimeMillis() / 1000);
r.setSeed(seed);
initialized++;
}

int random_int(int mean)
{
/*
* Computes a random integer from an exponetial distribution with a
* specified mean. If this routine is called and the generator has not
* yet been initialized, it initializes it using the system clock.
*/
if (initialized == 0) init_random_int(-1);

/*
* Find number from exponentially distribution with specified mean
* and round to an integer.
*/
int value = (int) (0.5 - mean * Math.log(r.nextDouble()));

if (value == 0)
value = 1;
else
if (value > 5 * mean) value = 5 * mean;

return value;
}

}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这个是我的输出:
Mean inter-arrival time:10
Mean service time::30
Number of tellers:3
Length of simulation: 70
Time Up
At time 30,customer 1 arrives in line
At time 30,customer 1 starts being served

------------------------------------------------------------------------------------------------------------------------------------------------
但是我想要的输出是这种:
Mean inter-arrival time: 10
Mean service time: 30
Number of tellers: 3
Length of simulation: 70
At time 30, customer 1 arrives in line
At time 30, customer 1 starts being served
At time 30, customer 2 arrives in line
At time 30, customer 2 starts being served
At time 30, customer 3 arrives in line
At time 30, customer 3 starts being served
At time 40, customer 1 leaves the bank
At time 50, customer 4 arrives in line
At time 50, customer 4 starts being served
At time 50, customer 4 leaves the bank
At time 50, customer 5 arrives in line
At time 50, customer 5 starts being served
At time 50, customer 6 arrives in line
At time 50, customer 7 arrives in line
At time 60, customer 3 leaves the bank
At time 60, customer 5 leaves the bank
At time 60, customer 6 starts being served
At time 60, customer 7 starts being served
At time 70, customer 7 leaves the bank
At time 70, customer 8 arrives in line
At time 70, customer 8 starts being served
At time 70, customer 8 leaves the bank
At time 100, customer 6 leaves the bank
At time 110, customer 2 leaves the bank
Simulation terminated after 8 customers served
Average waiting time = 2.50

------------------------------------------------------------------------
只要能帮我就好,可以要报酬。谢谢
...全文
1000 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,452

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧