Java计时器不工作,无休止地运行

时间:2018-06-12 18:44:04

标签: java timer timertask

  • 我正在运行该程序5分钟。它没有显示任何错误,但在运行时剩余时间保持不变
  • 我正在开发一个时间驱动的单服务器或多服务器队列系统 以下规格:

  • 来电平均每分钟拨打20个电话。

  • 当呼叫到达时(随机呼叫ID),呼叫将被置于队列中。
  • 通话的服务时间从3秒到16秒不等(使用随机数和计算机时钟时间)
  • 如果呼叫需要的服务时间更长,则呼叫一次最多7秒 超过7秒,对于剩余的服务时间,呼叫被放回队列 (入队)再次

这是我的编码:

package callcenter;

import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class CallCenter extends TimerTask {

ArrayList<Caller> Server1 = new ArrayList<>();
ArrayList<Caller> Server2 = new ArrayList<>();
ArrayList<Caller> Queue = new ArrayList<>();
Random rand = new Random();

int count = 7,
count1 = 7,
time = rand.nextInt(301) + 300;

public static int firstattempt = 0;
public static int queued1 = 0;
public static int queued2 = 0;
public static int attempt = 0;
public static int callcounter = 0;
public static int Processed = 0;

public static void main(String[] args)  {
    TimerTask task = new CallCenter();
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(task,0,1000);
}

@Override
public void run() {


    if ((Server1.isEmpty() == false) || (Server2.isEmpty() == false)){
        ReceiveCall();
        callcounter++;

        if (count > 0){

            if(Server1.get(0).getServiceTime() > 1){

                Server1.get(0).setServiceTime();
                count--;

            }

            else{

                count = 7;
                attempt = Server1.get(0).getAttempt();
                Server1.remove(0);
                if(attempt == 1){
                    firstattempt++;
                }
                else if(attempt == 2){
                    queued1++;
                }
                else if(attempt == 3){
                    queued2++;
                }

                if(Queue.size() > 0){
                    Server1.add(Queue.get(0));
                    Queue.remove(0);
                    Processed++;
                }

            }

        }

        else{

            Server1.get(0).setAttempt();
            Queue.add(Server1.get(0));
            Server1.remove(0);
            if(Queue.size() > 0){

                Server1.add(Queue.get(0));
                Queue.remove(0);
                Processed++;
            }
            count = 7;

        }

        if (count1 > 0){

            if(Server2.get(0).getServiceTime() > 1){

                Server2.get(0).setServiceTime();
                count1--;

            }

            else{

                count1 = 7;
                attempt = Server2.get(0).getAttempt();
                Server2.remove(0);
                if(attempt == 1){
                    firstattempt++;
                }
                else if(attempt == 2){
                    queued1++;
                }
                else if(attempt == 3){
                    queued2++;
                }

                if(Queue.size() > 0){
                    Server2.add(Queue.get(0));
                    Queue.remove(0);
                    Processed++;
                }

            }

        }

        else{

            Server2.get(0).setAttempt();
            Queue.add(Server2.get(0));
            Server2.remove(0);
            if(Queue.size() > 0){
                Server2.add(Queue.get(0));
                Queue.remove(0);
                Processed++;
            }
            count1 = 7;

        }

    }

    else if (Queue.size() > 0) {

        if(Server1.isEmpty() == false) {

            Server2.add(Queue.get(0));
            Queue.remove(0);
            Processed++;
        }
        else{

            Server1.add(Queue.get(0));
            Queue.remove(0);
            Processed++;
        }
    }

    else {
        ReceiveCall();
        callcounter++;
    }

    Format();

}


public void ReceiveCall(){

    int SAssign;
    Caller c = new Caller();

    c.setCallerID();
    c.setRandomTime();
    c.setAttempt();


    SAssign = rand.nextInt(2) + 1;

    if (SAssign == 1){
        if(Server1.isEmpty() == false){
            Queue.add(c);
        }

        else {
            Server1.add(c);
            Processed++;
        }
    }

    else if(SAssign== 2){           
        if(Server2.isEmpty() == false){               
            Queue.add(c);             
        }

        else {              
            Server2.add(c);
            Processed++;
        }          
    }


}

public void CountDown(){

    if (this.time != 0) {
        --this.time;
    } else {
        System.exit(0);
    }

}


 public void Format (){

    int x = 0;
    String Service = null;
    String Time1= null;
    String Service2 = null;
    String Time2 = null;

    if (Server1.size() > 0){

        Service = Server1.get(0).toString();
        Time1 = Integer.toString(Server1.get(0).getServiceTime());

    }

    if (Server2.size() > 0){

        Service2 = Server2.get(0).toString();
        Time2 = Integer.toString(Server2.get(0).getServiceTime());

    }


    System.out.println("Time Remaining: " + time + " seconds");
    System.out.println("Server 1: \n" + Service + " | " + Time1);
    System.out.println("Server 2: \n" + Service2 + " | " + Time2);
    System.out.println("Queue: ");

    if (Queue.size() > 0){

        while(x < Queue.size()){

            System.out.println(Queue.get(x));
            x++;

        }
    }

    if (time == 0){
    System.out.println("Total number of calls processed are : "+ Processed);
    System.out.println("Average number of calls processed per minute is : "+ (double)(Processed/60.0));
    System.out.println("Average arrival rate per minute is : "+ (double)(callcounter/60.0));
    System.out.println("Number of calls processed in first attempt : "+ firstattempt);
    System.out.println("Number of calls had to be requeued once : "+ queued1);
    System.out.println("Number of calls had to be requeued twice : "+ queued2);
    }
}
}

Caller.java

package callcenter;
import java.util.Random;
public class Caller {
Random rand = new Random();

int CallerID;
int ServiceTime;
int Attempt = 0;
String Name;

public Caller() {
    this.CallerID = CallerID;
}

@Override
public String toString() {
    Name = Integer.toString(CallerID);
    return Name;
}
public void setCallerID() {
    this.CallerID = rand.nextInt(3000) + 2000;
}

public int getCallerID() {
    return CallerID;
}

public int getServiceTime() {
    return ServiceTime;
}

public void setRandomTime() {
    this.ServiceTime = rand.nextInt(14) + 3;
}

public void setServiceTime() {
    this.ServiceTime = ServiceTime - 1;
}

public void setAttempt() {
    this.Attempt++;
}

public int getAttempt() {
    return Attempt;
}

}

0 个答案:

没有答案