0
class BeeperControl {
    private final static ScheduledExecutorService scheduler = 
                                  Executors.newScheduledThreadPool(1);

    public static void main(String[] args){
        beepForAnHour();
    }

    public static void beepForAnHour() {
        final Runnable beeper = new Runnable() {
            public void run() {
                System.out.println(“beep”);
            }
        };
        final ScheduledFuture beeperHandle = 
             scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);

        scheduler.schedule(new Runnable() {
            public void run() {
                beeperHandle.cancel(true);
            }
        }, 60 * 60, SECONDS);
    }
}

This code beeps every 10 seconds for an hour. How do i make it beep for every 10minutes forever. as in it should keep beeping every 10mins forever(not just for an hour)

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • 1
    Wrap it inside `while(true) {}`? – 11684 Jul 25 '12 at 09:11
  • Step 1: Format your code readably. – T.J. Crowder Jul 25 '12 at 09:11
  • which is better .. schedulor or sleep ????????? – user1545477 Jul 25 '12 at 09:17
  • 1
    @MarkoTopolnik The reason I keep reformatting is because your formatting you keep using is frankly horrid, its very unreadable. – Jon Taylor Jul 25 '12 at 09:18
  • @MarkoTopolnik Well your code is completely bunched together, it may be ok in an IDE but for quick reading in a question such as this bunched up formatted code is not readable. At least with my code, yes it may take up a bit more room but it's a lot easier to read through. Besides I edited the post originally and there was nothing wrong with my edit, so why people feel the need to keep changing it is beyond me. – Jon Taylor Jul 25 '12 at 09:22
  • @MarkoTopolnik I'm not making any sort of argument, however this is again standard SO, again and again, question has been reformatted just fine (whether to your taste or not) and is now completely readable to the other users and yet again, another member comes along and changes the formatting to his liking instead. As I said I had reformatted the question so there was no need to keep reformatting. It even tells you while you're making an edit if another edit has been made, you should look at them and if its a non trivial change (which yours and the other guys was) then cancel your changes. – Jon Taylor Jul 25 '12 at 09:26
  • @JonTaylor I didn't "come along". As far as I'm concerned, it is you who came along. Also, I didn't rollback your edit, I made further changes to my formatting and pasted again. – Marko Topolnik Jul 25 '12 at 09:27
  • @MarkoTopolnik How do you figure that? look at the revision history, it's pretty obvious who made the edit first. – Jon Taylor Jul 25 '12 at 09:29
  • @JonTaylor Yes, it is indeed pretty obvious. Try and understand what I **really** meant by that. – Marko Topolnik Jul 25 '12 at 09:30
  • @MarkoTopolnik I don't particularly care what you meant by it to be quite honest. I am sick of the crappy attitude that a large percentage of higher ranking users have though, I see unnecessary edits all the time, stupid reasons for closing posts etc etc. I'm not saying your edit was unecessary, if I had not already made an edit it would be perfectly reasonable, but changing 1 or two indentations on someone elses edit is frankly idiotic. – Jon Taylor Jul 25 '12 at 09:33
  • @JonTaylor Will you please at the very least wrap the line that extends beyond the visible area? Or is that invisible part of the line still more readable than my version? – Marko Topolnik Jul 25 '12 at 09:34

3 Answers3

3

You explicitly schedule a cancel task. If you don't want to cancel, then don't schedule the cancelling task.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Just remove the 2nd schedule that does the cancel.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
0

I propose:

public void run() {
    while(true) {
        System.out.println(“beep”);
        Thread.sleep(10000);
    }
}

+'s;

  • Simpler
  • You can track if you're being Interrupted

-'s: - You have to catch the InterruptedException

If you want to stop:

  • Put code to stop/pause in the catch statement and say myThread.interrupt(); to stop, starting it again is a bit harder, your best option is to create a new Thread (that does exactly the same). Info here: How to start/stop/restart a thread in Java?
  • Put an if-statement in the while-loop, checking a public boolean isRunning, if it's false, say continue; an recheck again and again until it is true again, put your beeping code in the 'true-block'.
Community
  • 1
  • 1
11684
  • 7,356
  • 12
  • 48
  • 71
  • @Nandkumar haha, when I clicked edit it showed your edit on the edit-page, it was exactly what I was going to do! – 11684 Jul 25 '12 at 09:14
  • as I think OP wants to use ScheduledExecutorService I'm afraid that this isn't really an answer to his question... – fvu Jul 25 '12 at 09:15
  • Perhaps, but that is't stated in the question. – 11684 Jul 25 '12 at 09:16
  • Also as others have pointed out the only reason it stops is because the task is cancelled by the second scheduller. – Jon Taylor Jul 25 '12 at 09:16