Use Timer Task to run a loop at a certain time

import java.util.*;
class Run30Sec {
public static void main( String args[] )
int numberOfMillisecondsInTheFuture = 10000; // 10 sec
Date timeToRun = new Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// Task here ...
for (; ;;) {
System.out.println("Run 10 secs");
}, timeToRun);
}

It starts after ten seconds delay and runs for five seconds.
import java.util.*;
class Run5Sec {
  static boolean go = true;
  public static void main( String args[] ){
    int numberOfMillisecondsInTheFuture = 10000; // 10 sec
    // you could use schedule(TimerTask task, long delay)
    // instead of Date
    Date timeToRun
      = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);
    Timer timer = new Timer();
    timer.schedule(new TimerTask(){
      public void run(){
        Timer ttimer = new Timer();
        ttimer.schedule(new TimerTask(){
          public void run(){
            go = false;
        }, 5000); // exit the timer after five seconds
        while (go) {
          System.out.println("I will run only for 5 secs.");
        System.exit(0);
    }, timeToRun);
}

Similar Messages

  • I am running Itunes in Windows 7. Every time I exit, it restarts in a few seconds. If I restart my compter and forec close it, it does not restart, but If I use it at all the whole loop starts again.

    I am running Itunes in Windows 7. Every time I exit, it restarts in a few seconds. If I restart my compter and forec close it, it does not restart, but If I use it at all the whole loop starts again.

    Sorry, just to confirm, I DID NOT ACTUALLY ANSWER MY OWN QUESTION!
    I replied to the previous message from my iphone and I managed to hit the link to "This has answered my question" by accident!
    I AM STILL LOOKING FOR A RESOLUTION TO MY ORIGINAL PROBLEM. CAN SOMEONE ALSO NIW TELL ME HOW TO GET RID OF THE GREEN TEXT TGAT SAYS TGAT MY QUESTION IS ANSWERED? That is a pain! It should at least ask you to confirm that your question is actually answered and you didn't hit it by mistake, like I did!

  • Firefox is running in the background, but the process can't be ended using the task manager?

    The program wouldn't open because it was apparently still running in the background, so I restarted a few times which didn't help. Then I tried using the task manager to end the process but that didn't work either. Then opened the command prompt and used taskkill, then pskill (using the PID number) but neither worked. The problem wasn't with how I was using these commands as they could close any other program I had running, just not firefox.exe*32. Any ideas?

    Try:
    *http://kb.mozillazine.org/Firefox.exe_always_open

  • Need help on Task Scheduling  using Timer task

    Hi all,
    Does any body know how to schedule a task at any given time using Timertask. Say i want to execute a task at 4:00 AM. but the timer task is executing the task at 4:00 AM and as well as at 4:00 PM also. But i don't want to execute the task at 4:00 PM. So how to specify the start time exactly as 4:00 AM only. How to specify the exact start time for timer task . please refer the following code how i have implemented
    Looking for any kind of help or hints or suggestions.
    Thanks in advance
    Date startTime = getExecutionTime(4,0);
    timer=new java.util.Timer();
    ImportingTask task = new ImportingTask();
    timer.scheduleAtFixedRate(task,startTime,interval);
    private Date getExecutionTime(int hours,int minutes ){
    GregorianCalendar present_Cal = new GregorianCalendar();
    GregorianCalendar result = new GregorianCalendar(
    present_Cal.get(Calendar.YEAR),
    present_Cal.get(Calendar.MONTH),
    present_Cal.get(Calendar.DATE),
    hours,
    minutes );
    return result ;
    class ImportingTask extends TimerTask{
    public void run(){
    executeScheduledJob();
    }

    I also have a similar problem.
    I used timer.scheduledAtFixedRate(MyTimerTask, start date, period). The start date is today 6:00AM and the period is 24 hours. It works fine. My problem is that I would like the user to enter a specific number of minutes or hours before it should start again.
    The current program starts initially at 6:00AM then it stops after 24 hours (which is 6:00AM the next day) and runs again.
    What I would like is that after 24 hours, the user can input, say, 5 mins......thus, the program should run at 6:05AM. Then it should run again at 6:10AM the next day and so forth.
    Can scheduledAtFixedRate handle this? Thanks in advance!

  • Using Time Capsule with a multiuser configured Mac Mini running 10.8.6

    I have an Admin user and 3 standard users in my Mac Mini.
    If I get a Time Capsule or a Thunderbolt compatiable external hard drive and back up using Time Machine, will 'it' do the entire drive or just user specific data.
    In other words do I have to set up the back up within each user and will the back up take place without user login?
    Thanks

    TM is designed to backup the disk and all user info.. there is no need to have each user login and run TM.. but it might be best if the setup is the Admin user.

  • I'm using a macbook pro, running snow leopard. Yesterday I got around to emptying my trash for the first time in quite awhile and was surprised by how much there seemed to be in there. Immediately after a question mark symbols appeared on all my hard disk

    I'm using a macbook pro, running snow leopard. Yesterday I got around to emptying my trash for the first time in quite awhile and was surprised by how much there seemed to be in there. Immediately after a question mark symbol appeared on all my hard disk folders. If I click on these question marks I get a message saying ' the item can't be found'.

    mpagan47 wrote: a question mark symbol appeared on all my hard disk folders.
    Not sure where these hard disk folders are located that you are referring to?  Finder, Finder tool bar, Finder side bar, Desktop, Dock? 
    More then likely they are  an alias that not longer point to the orginals,  thus the question mark.
    I would reboot.  Delete icons if an alias and just recreate by dragging from the source. Try right click (control) click to remove.

  • HT1338 Purchased a used macbook pro with Mountain Lion. My old Mac runs Snow Leopard is backed up to Time machine. How do I register the operating system to me and how do I use Time Machine to move my files to the new used computer?

    Purchased a used macbook pro with Mountain Lion. My old Mac runs Snow Leopard is backed up to Time machine. How do I register the operating system to me and how do I use Time Machine to move my files to the new used computer?

    If you look at the User Tips tab, you will find a write up on just this subject:
    https://discussions.apple.com/docs/DOC-4053
    The subject of buying/selling a Mac is quite complicated.  Here is a guide to the steps involved. It is from the Seller's point of view, but easily read the other way too:
    SELLING A MAC A
    Internet Recovery, and Transferability of OS & iLife Apps
    Selling an Old Mac:
    • When selling an old Mac, the only OS that is legally transferable is the one that came preinstalled when the Mac was new. Selling a Mac with an upgraded OS isn't doing the new owner any favors. Attempting to do so will only result in headaches since the upgraded OS can't be registered by the new owner. If a clean install becomes necessary, they won't be able to do so and will be forced to install the original OS via Internet Recovery. Best to simply erase the drive and revert back to the original OS prior to selling any Mac.
    • Additionally, upgrading the OS on a Mac you intend to sell means that you are leaving personally identifiable information on the Mac since the only way to upgrade the OS involves using your own AppleID to download the upgrade from the App Store. So there will be traces of your info and user account left behind. Again, best to erase the drive and revert to the original OS via Internet Recovery.
    Internet Recovery:
    • In the event that the OS has been upgraded to a newer version (i.e. Lion to Mountain Lion), Internet Recovery will offer the version of the OS that originally came with the Mac. So while booting to the Recovery Disk will show Mountain Lion as available for reinstall since that is the current version running, Internet Recovery, on the other hand, will only show Lion available since that was the OS shipped with that particular Mac.
    • Though the Mac came with a particular version of Mac OS X, it appears that, when Internet Recovery is invoked, the most recent update of that version may be applied. (i.e. if the Mac originally came with 10.7.3, Internet Recovery may install a more recent update like 10.7.5)
    iLife Apps:
    • When the App Store is launched for the first time it will report that the iLife apps are available for the user to Accept under the Purchases section. The user will be required to enter their AppleID during the Acceptance process. From that point on the iLife apps will be tied to the AppleID used to Accept them. The user will be allowed to download the apps to other Macs they own if they wish using the same AppleID used to Accept them.
    • Once Accepted on the new Mac, the iLife apps can not be transferred to any future owner when the Mac is sold. Attempting to use an AppleID after the apps have already been accepted using a different AppleID will result in the App Store reporting "These apps were already assigned to another Apple ID".
    • It appears, however, that the iLife Apps do not automatically go to the first owner of the Mac. It's quite possible that the original owner, either by choice or neglect, never Accepted the iLife apps in the App Store. As a result, a future owner of the Mac may be able to successfully Accept the apps and retain them for themselves using their own AppleID. Bottom Line: Whoever Accepts the iLife apps first gets to keep them.
    SELLING A MAC B
    Follow these instructions step by step to prepare a Mac for sale:
    Step One - Back up your data:
    A. If you have any Virtual PCs shut them down. They cannot be in their "fast saved" state. They must be shut down from inside Windows.
    B. Clone to an external drive using using Carbon Copy Cloner.
    1. Open Carbon Copy Cloner.
    2. Select the Source volume from the Select a source drop down menu on the left side.
    3. Select the Destination volume from the Select a destination drop down menu on the right
    side.
    4. Click on the Clone button. If you are prompted about creating a clone of the Recovery HD be
    sure to opt for that.
    Destination means a freshly erased external backup drive. Source means the internal
    startup drive. 
    Step Two - Prepare the machine for the new buyer:
    1. De-authorize the computer in iTunes! De-authorize both iTunes and Audible accounts.
    2, Remove any Open Firmware passwords or Firmware passwords.
    3. Turn the brightness full up and volume nearly so.
    4. Turn off File Vault, if enabled.
    5. Disable iCloud, if enabled: See.What to do with iCloud before selling your computer
    Step Three - Install a fresh OS:
    A. Snow Leopard and earlier versions of OS X
    1. Insert the original OS X install CD/DVD that came with your computer.
    2. Restart the computer while holding down the C key to boot from the CD/DVD.
    3. Select Disk Utility from the Utilities menu; repartition and reformat the internal hard drive.
    Optionally, click on the Security button and set the Zero Data option to one-pass.
    4. Install OS X.
    5. Upon completion DO NOT restart the computer.
    6. Shutdown the computer.
    B. Lion and Mountain Lion (if pre-installed on the computer at purchase*)
    Note: You will need an active Internet connection. I suggest using Ethernet if possible because
    it is three times faster than wireless.
    1. Restart the computer while holding down the COMMAND and R keys until the Mac OS X
    Utilities window appears.
    2. Select Disk Utility from the Mac OS X Utilities window and click on the Continue button. 
    3. After DU loads select your startup volume (usually Macintosh HD) from the left side list. Click
    on the Erase tab in the DU main window.
    4. Set the format type to Mac OS Extended (Journaled.) Optionally, click on the Security button
    and set the Zero Data option to one-pass.
    5. Click on the Erase button and wait until the process has completed.
    6. Quit DU and return to the Mac OS X Utilities window.
    7. Select Reinstall Lion/Mountain Lion and click on the Install button.
    8. Upon completion shutdown the computer.
    *If your computer came with Lion or Mountain Lion pre-installed then you are entitled to transfer your license once. If you purchased Lion or Mountain Lion from the App Store then you cannot transfer your license to another party. In the case of the latter you should install the original version of OS X that came with your computer. You need to repartition the hard drive as well as reformat it; this will assure that the Recovery HD partition is removed. See Step Three above. You may verify these requirements by reviewing your OS X Software License.

  • I have a Macbook which is running Snow Leopard. I back up using time machine to an external drive. If I purchase a new Apple Macbook pro running Lion if I use the backup to this will I be able to use Leopard on the new computer

    I have a Macbook running Snow Leopard which I back up to an external drive using time machine.  If I purchase a new MacBook pro and transfer backup from time maching to MacBook Pro will it also transfer Snow Leopard as its is my preferred O.S.

    Welcome to Apple Support Communities.
    If the new computer did not originally ship with Snow Leopard installed, no, you will not be able to go back from Lion. Snow Leopard lacks the necessary drivers required for newer hardware.

  • HT1178 Can the time capsule be used with a pc running Windows Vista , 7 or 8 as a backup drive

    Can the time capsule be used with a pc running Windows Vista , 7 or 8 as a backup drive. If so what software do I need to buy?

    Can the time capsule be used with a pc running Windows Vista , 7 or 8 as a backup drive.
    Yes, on XP, Vista and 7. Have not tried 8.
    If so what software do I need to buy?
    Genie Timeline Pro does a very nice job...and it is similar to Apple's Time Machine.  It's not cheap though.  Check with the Genie folks for more info and compatibility with Windows 8.
    Genie Timeline Pro | Overview | Time Machine Backup Software for ...
    Other users may have some other suggestions.

  • I just bought an iMac now running OSX 10.8.4.  I have a G5 running OXS 10.5.8 - External HD connected to the G5 been using Time Machine to back up. Need to get my files, photos etc. but not applications etc. onto the iMac. How do I do it?

    I just bought an iMac now running OSX 10.8.4.  I have a G5 running OXS 10.5.8 - External HD connected to the G5 been using Time Machine to back up. Need to get my files, photos etc. but not applications etc. onto the iMac. How do I do it?

    David JHS21 wrote:
    Can I just plug the external hard drive into the iMac and copy the "Documents" and "Pictures" from the hard drive to the iMac?
    Not those whole folders, but their contents, maybe.
    If the user account now on the Mac has read rights to the user account(s) on the backups, yes.  If not, you may have to do some finagling. 
    Try the Browse Other Backup Disks option, per Time Machine - Frequently Asked Question #17.  If you find an account that can open those folders, restore what you want to an alternate location (#16 in the FAQ).
    If not try creating one or more accounts temporarily.

  • I have a new macbook air, and want to transfer all my data using time machine from my old macbook running leopard, is this possible and easy

    I have a new macbook air, and want to transfer all my data using time machine from my old macbook running leopard, is this possible and easy and will it transfer all my musc and photos?

    The Setup Assistant can restore your data from a Time Machine backup. You'll be asked if you want to do this the first time you turn the computer on.
    (60797)

  • An old macbook running 10.6.8 was restored using Time Machine then Snow Leopard was reinstalled and updated to 10.6.8. Now the machine is slow and repair disc permissions results in a cancelled by user message.

    Our old macbook was getting a gray screen with flashing ? on bootup. Eventually I restored it using Time Machine then decided to reinstall Snow Leopard since it would not boot. Machine now boots but is slow. Ran repair disc and tried to run repair disc permissions but got a message that it was cancelled by user.
    This machine is at least 6 years old and I just want to limp it along until I can afford to replace it in the spring.

    You may be smelling it because with the new OS the processor is having to work harder causing it to get hotter then it would with the other OS.  I can't say there is nothing wrong, but what could be happening is just that, it's getting hotter so the fan is spinning faster and moving more air so you are getting more of the smell then you would before.  I would still continue to back it up, and take it to the Apple Store at a Genius Bar and have them look at it to make sure.  Was the computer ever in a smoky environment??

  • Can i use pci-7352 to run 4 or 6 axis (motors) if i am not using them same time

    can i use pci-7352 to run 4 or 6 axis (motors) if i am not using them same time. This is a stepper motor

    This is a duplicate posting. I have answered this question here.
    Jochen Klier
    National Instruments Germany

  • About 2/3 of the time I leave Firefox, it is evidently retained in memory so I can't restart it without using the task manager. I don't use Norton or Skype programs. How do I keep this from happening?

    About 2/3 of the time I leave Firefox, it is evidently retained in memory so I can't restart it without using the task manager. I don't use Norton or Skype programs. How do I keep this from happening?
    == After I have used Firefox and "closed" it.

    See this:
    https://support.mozilla.com/en-US/kb/Firefox+hangs#Hang_at_exit

  • Can I use 3rd party instruments/plug-ins/loops with Garageband '11 running OS X 10.6.8? and how do I install?

    Can I use 3rd party instruments/plug-ins/loops with Garageband '11 running OS X 10.6.8? and how do I install?

    10.6.8 won't run on a PowerPC Mac. 
    10.6.8 also won't run the latest Java.  Here's what version of Java you can run:
    https://discussions.apple.com/docs/DOC-5532

Maybe you are looking for

  • Connecting to a database on a network

    Is it possible to connect to a database on a network drive?

  • Create new iTunes Library

    My music is on an external HD, is there any way to create a Library from the HD to my laptop without moving all the music over. I just need to create a new Library on the laptop and keep the music on the external HD. Can anyone help me with this prob

  • CS3 Extended RAW file plug-in

    I use CS3 Extended on a Windows 7 computer. I have a Nikon D610 camera producing NEF RAW files. I have downloaded and installed the Adobe RAW plug-in and also the DNG file converter. Photoshop will still not recognise either the NEF files or the DNG

  • Smartform barcode problem

    I have designed a smartform prints barcode. On the left hand side there is barcode window and right hand side another window that holds some text for printing. Both windows are same level ( same line horizontally) . When i try to print, barcode print

  • System errors on N91 8GB

    The warranty on my N91 8GB has recently expired and since it has my phone has started acting wierd, it gives me system errors when frequently, sometimes it drops the network and will not detect any if i do a manual search, the music players acting fu