Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中调度两个独立的计时器并更改一个计时器的周期_Java_Timer_Timertask - Fatal编程技术网

在Java中调度两个独立的计时器并更改一个计时器的周期

在Java中调度两个独立的计时器并更改一个计时器的周期,java,timer,timertask,Java,Timer,Timertask,我有两个独立调度的java计时器。两个计时器都有不同的任务 定时器1增加一个数字,定时器2改变定时器1的周期。这是我使用两个定时器的代码 public class Receiver { public static int totalBufferCapacity = 1024; public static int totalPacketsDropped = 0; public static

我有两个独立调度的java计时器。两个计时器都有不同的任务

定时器1增加一个数字,定时器2改变定时器1的周期。这是我使用两个定时器的代码

public class Receiver 
{ 
     public static int               totalBufferCapacity     = 1024; 
     public static int               totalPacketsDropped     = 0; 
     public static int               totalPacketsServiced    = 0; 
     public static int               totalPacketsReceived    = 0; 
     public static int               timesBufferGetsFull     = 0; 
     public static int               timesIntervelChanged    = 0; 

     public static Socket            clientSocket; 
     public static BufferedReader    br; 
     public static ArrayList<String>   buffer; 

     public static String            START                   = "Start"; 
     public static String            STOP                    = "Stop"; 
     public static String            token                   = "1"; 
     public static boolean           flag; 

     public static Timer             timer; 

     public static int               Max                     = 80; 
     public static int               Min                     = 40; 

     public static int               rand; 
     public static PrintStream       ps; 
     public static String            packet; 
     public static Timer             timer_2; 
     public static consumeArrayItems task; 


     public static void main(String[] args) 
    { 
        flag = true; 
        try
        { 
           init(args[0], args[1]); 

           while (flag) 
           { 
              storePacketInArray(); 
           } 

        } catch (Exception e) 
        { 
             e.printStackTrace(); 
        } 
    } 


    public static void init(String localHost, String portNumber) 
    { 
        try
        { 

            // inet address which is local host in this case 
            InetAddress acceptorHost = InetAddress.getByName(localHost); 

            // port number at which the sender wants to communicate 
            int serverPortNum = Integer.parseInt(portNumber); 

            clientSocket = new Socket(acceptorHost, serverPortNum); 

         } catch (IOException e) 
         { 
             e.printStackTrace(); 

         } 
    } 



     public static void storePacketInArray() 
     { 
          try
          { 
             if (br == null) 
             { 
                  br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
             } 

           packet = new String(br.readLine()); 

           if (packet.compareToIgnoreCase("Start") == 0) 
           { 
                 token = START; 
                 buffer = new ArrayList<String>(totalBufferCapacity); 
           } else if (packet.compareToIgnoreCase("Stop") == 0) 
           { 
                 stopVaryingTimeSchedular(); 
                 stopSchedular(); 
           } else
           { 
                 totalPacketsReceived += 1; 
                 buffer.add(packet); 
           } 


           computeToken(); 

        } catch (IOException e) 
        { 
            e.printStackTrace(); 

        } 
     } 


    public static void computeToken() 
    { 
        int bufferSize = buffer.size(); 

        if (bufferSize > 0 && bufferSize < totalBufferCapacity) 
        { 
             float queueOccupancy = (bufferSize * 100 / totalBufferCapacity); 


        } else if (bufferSize == totalBufferCapacity) 
        { 
             token = "10"; 
             timesBufferGetsFull += 1; 
        } else if (token.compareToIgnoreCase("Start") == 0) 
        { 
             token = START; 
             startSchedular(); 
             startVaryingTimeSchedular(); 

         } else
        { 
              totalPacketsDropped += 1; 
              token = "15"; 

        } 


        sendAcknowledgment(); 

     } 



     public static void sendAcknowledgment() 
    { 

         try
         { 
             if (ps == null) 
             { 
                 ps = new PrintStream(clientSocket.getOutputStream()); 
             } 

             String tokenAck = token; 

             if (packet.compareToIgnoreCase("Stop") != 0) 
             { 
                 ps.println(tokenAck); 
                 ps.flush(); 
             } 
             if (!flag) 
             { 
                 clientSocket.close(); 
             } 

         } catch (IOException e) 
         { 
              e.printStackTrace(); 
         } 

    } 


     public static void startSchedular() 
     { 

          rand = (int) (Math.random() * (Max - Min)); 
          timer = new Timer(); 
          task = new consumeArrayItems(true); 
          timer.scheduleAtFixedRate(task, 1, rand); 
     } 

     public static void stopSchedular() 
     { 
         timer.cancel(); 
         timer.purge(); 
         flag = false; 

     } 

      // After every 500 ms service time of packets will vary between Max and Min 
     public static void startVaryingTimeSchedular() 
     { 
         timer_2 = new Timer(); 
         timer_2.scheduleAtFixedRate(new varyServiceTime(), 0, 500); 
     } 

     public static void stopVaryingTimeSchedular() 
     { 

         timer_2.cancel(); 
         timer_2.purge(); 
     } 

  } 


 class consumeArrayItems extends TimerTask 
 { 


       public synchronized void run() 
       { 
          if (Receiver.buffer.size() > 0) 
          { 
              Receiver.totalPacketsServiced += 1; 
              Receiver.buffer.remove(Receiver.buffer.size() - 1); 
          } 
       } 
  } 

  class varyServiceTime extends TimerTask 
  { 

        public synchronized void run() 
        { 
              Receiver.timer.cancel(); 
              Receiver.timer = null; 
              Receiver.rand = (int) (Math.random() * (Receiver.Max - Receiver.Min)); 
              Receiver.timer = new Timer(); 
              Receiver.timer.scheduleAtFixedRate(new consumeArrayItems(), 0,Receiver.rand); 

              Receiver.timesIntervelChanged += 1; 
        } 
  } 
公共类接收器
{ 
公共静态int totalBufferCapacity=1024;
公共静态int totalPacketsDropped=0;
公共静态int TotalPacketsService=0;
公共静态int totalPacketsReceived=0;
公共静态int-timesBufferGetsFull=0;
公共静态int timeintervelchanged=0;
公共静态套接字clientSocket;
公共静态缓冲读取器br;
公共静态数组列表缓冲区;
公共静态字符串START=“START”;
公共静态字符串STOP=“STOP”;
公共静态字符串标记=“1”;
公共静态布尔标志;
公共静态定时器;
公共静态int Max=80;
公共静态int Min=40;
公共静态整数;
公共静态打印流;
公共静态字符串包;
公共静态定时器2;
公共静态ConsumerArrayItems任务;
公共静态void main(字符串[]args)
{ 
flag=true;
尝试
{ 
init(args[0],args[1]);
while(旗帜)
{ 
storePacketInArray();
} 
}捕获(例外e)
{ 
e、 printStackTrace();
} 
} 
公共静态void init(字符串localHost,字符串portNumber)
{ 
尝试
{ 
//本例中为本地主机的inet地址
InetAddress acceptorHost=InetAddress.getByName(本地主机);
//发送方希望通信的端口号
int serverPortNum=Integer.parseInt(端口号);
clientSocket=新套接字(acceptorHost,serverPortNum);
}捕获(IOE异常)
{ 
e、 printStackTrace();
} 
} 
公共静态void storePacketInArray()
{ 
尝试
{ 
如果(br==null)
{ 
br=新的BufferedReader(新的InputStreamReader(clientSocket.getInputStream());
} 
数据包=新字符串(br.readLine());
if(packet.compareTignoreCase(“开始”)==0)
{ 
令牌=开始;
buffer=新的ArrayList(totalBufferCapacity);
}else if(packet.compareTignoreCase(“停止”)==0)
{ 
stopVaryingTimeSchedular();
stopSchedular();
}否则
{ 
接收的总数据包数+=1;
buffer.add(数据包);
} 
computeToken();
}捕获(IOE异常)
{ 
e、 printStackTrace();
} 
} 
公共静态void computeToken()
{ 
int bufferSize=buffer.size();
if(bufferSize>0&&bufferSize0)
{ 
Receiver.TotalPacketsService+=1;
Receiver.buffer.remove(Receiver.buffer.size()-1);
} 
} 
} 
类varyServiceTime扩展TimerTask
{ 
公共同步的无效运行()
{ 
Receiver.timer.cancel();
Receiver.timer=null;
Receiver.rand=(int)(Math.random()*(Receiver.Max-Receiver.Min));
Receiver.timer=新计时器();
Receiver.timer.scheduleAtFixedRate(新的ConsumerArrayItems(),