首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何实现Java计时器无休止地运行?

如何实现Java计时器无休止地运行?
EN

Stack Overflow用户
提问于 2018-07-31 06:54:05
回答 1查看 0关注 0票数 0

以下是我的代码:

代码语言:txt
复制
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

代码语言:txt
复制
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;
}

}
EN

回答 1

Stack Overflow用户

发布于 2018-07-31 16:36:02

我从上面找不到任何错误,可能它在另一边有错误,比如计时器或计时器任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档