开发者

How to send a string (that is in a loop) from client to server only one time per a minute?

I have a string that is in a while loop to be sent from client to server multiple times but I want it to be sent only one time per a minute. How can I do it, any ideas !!?

Here is the code:

    String str= " ";
    try{
        FileInputStream fstream = new FileInputStream("C:\\bluetooth.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

             while ((str = br.readLine()) != null)   {
            if ( str.equals.("F81EDF5B0CC7")) // "F81EDF5B0CC7" is a String that I have it in the text file 'bluetooth.txt'
                {
                  out.writeUTF(str);
             }
            } 
            in.close();
        }catch (Exception e){
          System开发者_StackOverflow.err.println("Error: " + e.getMessage());
        }

Please help and thanks in advance.


Use Timer.scheduleAtFixedRate(..)

int delay =9;   // delay for 0 sec - start immediatelly
int period = 60000;  // repeat every 60 sec.
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        // Task here ...
    }
}, delay, period);

Taken from here.


Have a look at the scheduled executor service ( http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html ), tutorial in ( http://download.oracle.com/javase/tutorial/essential/concurrency/executors.html )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜