how to log my program every 10 seconds

Viewed 640

hi I have a program that prints infinite random texts from server in a while loop and wont stop , how can I log the data every 10 seconds in a text file ?

public class MyServer {
public static String str="";
private volatile boolean running=true;
public synchronized void  socketreceive(){
    try{
        ServerSocket ss=new ServerSocket(6666);
        Socket s=ss.accept();//establishes connection
        DataInputStream dis=new DataInputStream(s.getInputStream());
        str=(String)dis.readUTF();


       //this  continuously goes on  and I need to make a log from it
        System.out.println("message= "+str);


        ss.close();
    }catch(Exception e){}
}

public void receiver(){
 final Thread receive=new Thread(new Runnable() {
    @Override
    public void run() {
        while (running){
            socketreceive();
    }
    }

});
3 Answers
Related