#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    16
    Rep Power
    0

    Question How to write output to a file repeatly last 10 seconds?


    I have several class write output to a file. I want class write1 write output to a file every 1 second and last 10 seconds, it means, write output 10 times for every 1 second.

    Timer can repeatedly run it every 1 second, but what is the condition to cancel the Timer after 10 seconds, or what is the way to do it?


    Code:
    import java.io.*;
    import java.util.*;
     
     
    public class WriteOut {
    	
    public void WriteOut() throws Exception {
    	File outfile = new File("sample.txt");
    	FileOutputStream fos = new FileOutputStream(outfile);
    	PrintWriter out = new PrintWriter(fos);
     
    	write1 w1 = new write1 ();
    	write2 w2 = new write2 ();
    	write3 w3 = new write3 ();
     
    	try{
    	w1.write1(out);
    	w2.write2(out);
    	w3.write3(out);
    	out.close();
    	}catch(Exception e){e.printStackTrace();}
    	}
    		
    	
    public static void main(String[] args) throws Exception {
    	WriteOut wo = new WriteOut();
    	wo.WriteOut();
    }
    }
     
     
    class write1 {
    	
    public synchronized static void write1(PrintWriter pw) {
    	pw.println("Line1");
    	pw.flush();
    }
    }
     
    class write2 {
    public synchronized static void write2(PrintWriter pw) {
        pw.println("Line2");
        pw.flush();
    }
    }
     
    class write3 {
    public  synchronized static void write3(PrintWriter pw) {
        pw.println("Line3");
        pw.flush();
    }
    }
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    but what is the condition to cancel the Timer after 10 seconds
    When ten seconds have passed, call cancel() on the timer. You can keep track of the passing time in a variety of ways.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    16
    Rep Power
    0
    I will have a few seconds break among the write out of each classes. So I must use loop for every ten seconds and break time.

    If use cancel(), it will cancel all the future tasks befure finish runing.

    I must cancel or stop each task after every 10 seconds, and then have a break then run task again and break again and again.

    So a simple cancel() can not do it. Any further idea? Thanks.


    Originally Posted by Yawmark
    When ten seconds have passed, call cancel() on the timer. You can keep track of the passing time in a variety of ways.
  6. #4
  7. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Any further idea?
    Sorry, no. Your requirements aren't clear to me, and it sounds like you're unfamiliar with the Timer class, which can schedule an event to happen periodically (e.g., every ten seconds). You might want to have a look at the following:

    Using the Timer and TimerTask Classes

    Good luck!
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo