How to stop the java timer

I tried writing a program for a scheduler using java , compiled and ran in java 1.6 .The code is given below
import java.util.*;
public class TimerTaskEx2 {
     public static void main(String[] args) {
          Timer timer = new Timer();
          timer.scheduleAtFixedRate(new TimerTask(){
               public void run() {
                    System.out.println("Java");
          }, 5000, 1000);
output
Java
Java
Java
Java
Java
Java
Java
The output of this program starts after 5 milliseconds delay and keeps running infinitely becoz the program is written only in such a way .
but my actual requirement is the timer should start 5 milliseconds after i run the program and stop after 20 milliseconds .
Here I need to know how to stop it at a required time interval
I dont find any api regarding this in 1.6 spec .
please clarify how to do that .
Thanks
Jee

try this code:
import java.util.*;
public class TimerTaskEx2 {
     public static void main(String[] args) {
          Timer timer = new Timer();
          timer.scheduleAtFixedRate(new TimerTask() {
               int i = 0;               
               public void run() {
                    System.out.println("Java");
                    i++;
                    if (i > 3)
                         this.cancel();                    
          }, 5000, 1000);
Also, make sure to close the alive thread too.

Similar Messages

  • How to stop the sla time of a service ticket

    Hello Folks,
    Is there a way to stop or pause the sla counting time of a service ticket?
    I have a scenario, where I get a service ticket, and I have to stop the sla counting time, because I need a response from a third person, not involved at that time.
    I´m using webclient, and my guess, is, there something realted to status, sla and actions, but not sure about.
    Could somebody give me some advise or a clue about it?
    Answers will be rewarded.
    thanks in advance,
    Luiz David

    Hi Luiz,
    Status needs to be selected in the conditions. Follow the below steps
    1. Select the actions in conditons
    Goto   Spro -->  IMG --> CRM --> Action --> Actions in transactions --> Change actions and conditions  --> Define conditions
    and click on your action profile and open the action
    2. Go to the start conditions tab and click on the edit conditions
    3. Click on the condition definition space and expand the container in the change condition pop-up box
    4. Double click on the status table and choose the new user status in the constant field
    5. Select the operator as not equal to
    Hope it helps
    Regards,
    Madhu

  • How to stop the "Elapsed Time" counting without stop the program running?

    我使用Labview 8.2,配合USB DAQ來做測試治具。目的:
    當檢測到某個電壓(Analog Input)到達某個電壓值(5V)之後,開始啟動計時。若是在15秒之內,另一個電壓值降為0之後為正常​,此時停止計時(但是數值不能歸零);若是超過15秒還沒有降到0伏,則表示錯誤,此時亮起紅色 indicator警示,但是時間仍然繼續計時,直到電壓降到0為止才停(但是數值不能歸零)。
    我使用 Elapsed Time 模組來使用,可是問題是如何讓該模組停止計時同時不能將時間歸零?因為此模組只有 "Reset" 控制功能,一但將條件結果輸入到 Reset input 之後,電壓達到零之後就會將此 Elapsed Time 歸零了。有沒有解決辦法?

    Elapsed Time 確實會將時間reset,如果你想要保留時間的話其實有很多做法
    例如使用 tick count,他會顯示出相對時間數值,先放一個tick count在迴圈開始前
    記得用sequence以確保他會在回區尚未開始運作前先記錄時間
    然後再放一個tick count在迴圈內,再用case或select讓電壓等於零時記錄下tick count的值
    再將兩者相減即可
    Chris

  • HOW TO STOP THE PROMPTING MESSAGE OF MASTER PASSWORD EACH TIME I OPEN MOZILLA FIREFOX?

    HOW TO STOP THE PROMPTING MESSAGE OF MASTER PASSWORD EACH TIME I OPEN MOZILLA FIREFOX?

    Firefox will ask for the Master Password on startup if it is needed on a page.<br />
    This can be Sync or a web page that is (re)opened automatically and you have saved a name & password for this website.
    You didn't confirm whether you are using Sync or not, so I don't know which of the two would apply.

  • How to stop the new Photos.app opening every time I put in a new device?

    How to stop the new Photos.app opening every time I put in a new device?
    It's annoying as i use bridge to do all my photo organising.

    Insert or attach a device then let Photos load. Then uncheck the box indicated:

  • How to find the Latency time in java.

    How to find the Latency time in java.

    long start = System.currentTimeMillis();
    doTheWork();
    long durationInMillis = System.currentTimeMillis() - start;You might repeat doTheWork() several (100-1000 or more) times if it is fast and the granularity of your system clock is not good enough.

  • How to get the system time in a Swing application ?

    I know how to do it in JavaScript, but not java. Please help me, and also how to get the system time in an Applet. Thanks !!!

    Check this link, I hope it helps
    http://202.71.136.142:8080/globalleafs/Swing/View.jsp?slno=22&tbl=0

  • How to stop the waterflow?

    Hi,
    I need to make the water coming from the shower, when shower is clicked.
    And the water should stop after 5 seconds.
    The code is below.
    The water is flowing nicely. But I do not manage to stop it.
    I added a timer, which starts when the shower is clicked. And should stop the waterflow after 5 seconds have passed.
    With the lines
    if (stop = true)
         break;
         trace("break");
    it looks like the for loop is breaking right from the start, from the first second. But not completely, somehow 1 or 2 drops are flowing.
    Without these lines nothing is happening when the time is over (5 seconds from clicking the shower). Water is just flowing.
    Can someone help? How to stop the water flowing after 5 seconds and clean of the array and stage from the waterdrops?
    Many thanks in advance!
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    var WatertropArray: Array = new Array();
    var WaterArea01: MovieClip = new WaterArea();
    var WaterBorder01: MovieClip = new WaterBorder();
    var timer: Timer = new Timer(5000, 1);
    var stopp: Boolean;
    var i: uint;
    Shower.addEventListener(MouseEvent.CLICK, waterFlowStart);
    function waterFlowStart(event: MouseEvent): void
                    addChild(WaterArea01);
                    WaterArea01.x = WaterArea00.x;
                    WaterArea01.y = WaterArea00.y;
                    WaterArea01.alpha = 0;
                    addChild(WaterBorder01);
                    WaterBorder01.x = WaterBorder00.x;
                    WaterBorder01.y = WaterBorder00.y;
                    WaterBorder01.alpha = 0;
                    stopp = false;
                    timer.addEventListener(TimerEvent.TIMER, waterFlowEnd);
                    timer.start();
                    addWaterdrops();
                    Shower.removeEventListener(MouseEvent.CLICK, waterFlowStart);
    function addWaterdrops(): void
                    for(var i: uint = 0; i < 100; i++)
                                   var waterDrop: MovieClip = new Waterdrop();
                                   addChild(waterDrop);
                                   waterDrop.x = Math.round(Math.random() * stage.stageWidth / 1.5);
                                   waterDrop.y = Math.round(Math.random() * stage.stageHeight / 3);
                                   waterDrop.alpha = 0;
                                   waterDrop.rotation = -12;
                                   WatertropArray.push(waterDrop);
                                   trace("waterdrops added");
                                   moveWaterdrops();
    function moveWaterdrops(): void
                    waterDrop00.addEventListener(Event.ENTER_FRAME, waterFlow);
    function waterFlow(event: Event): void
                    for(var i: uint = 0; i < WatertropArray.length; i++)
                                   WatertropArray[i].y += 8;
                                   WatertropArray[i].x += 5;
                                   //trace(i);
                                   if(WatertropArray[i].hitTestObject(WaterArea01))
                                                   WatertropArray[i].alpha = Math.random();
                                                   WatertropArray[i].scaleX = Math.random();
                                                   WatertropArray[i].scaleY = WatertropArray[i].scaleX;
                                   if(WatertropArray[i].hitTestObject(WaterBorder01) || WatertropArray[i].x > stage.stageWidth || WatertropArray[i].y > stage.stageHeight / 2)
                                                   WatertropArray[i].x = Math.round(Math.random() * stage.stageWidth / 1.5);
                                                   WatertropArray[i].y = Math.round(Math.random() * stage.stageHeight / 3);
                                                   WatertropArray[i].alpha = 0;
                                   if(stopp = true)
                                                   break;
                                                   trace("break");
    function waterFlowEnd(event: TimerEvent): void
                    trace("TIME OVER");
                    stopp = true;
                    stoppTrue();
    function stoppTrue(): void
                    for(var i: uint = WatertropArray.length; i > WatertropArray.length; i--)
                                   remove(i);
    function remove(idx: int)
                    removeChild(WatertropArray[idx]);
                    WatertropArray.splice(idx, 1);
                    trace("REMOVED");
                    removeChild(waterDrop00);
                    trace(i);

    thanks again, kglad.
    changed the for-loop and it is reaching now the last functions as well.
    but there is still a but  ... an error message.
    function waterFlowEnd(event: TimerEvent): void
    trace("TIME OVER");
    stopp = true;
    stoppTrue();                                                       // line 106
    function stoppTrue(): void
    for(var i: uint = WatertropArray.length-1; i >= 0; i--)
    trace("stoppTrue");
    remove(i);                                                         // line 115                                                    
    function remove(idx: int)
    removeChild(WatertropArray[idx]);                   // line 123
    WatertropArray.splice(idx, 1);
    trace("REMOVED");
    //removeChild(waterDrop00);
    trace(i);
    and the output panel gives the following (tested with 5 water drops, 5 items in array):
    TIME OVER
    stoppTrue
    REMOVED
    0
    stoppTrue
    REMOVED
    0
    stoppTrue
    REMOVED
    0
    stoppTrue
    REMOVED
    0
    stoppTrue
    REMOVED
    0
    stoppTrue
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/removeChild()
    at TitavannisinglisekeelneAS3_fla::MainTimeline/remove()[TitavannisinglisekeelneAS3_fla.Main Timeline::frame1:123]
    at TitavannisinglisekeelneAS3_fla::MainTimeline/stoppTrue()[TitavannisinglisekeelneAS3_fla.M ainTimeline::frame1:115]
    at TitavannisinglisekeelneAS3_fla::MainTimeline/waterFlowEnd()[TitavannisinglisekeelneAS3_fl a.MainTimeline::frame1:106]
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    What is that error message trying to tell me?   

  • How to retrieve the java object in a proxy service in osb -- Plz help

    Hi all,
    I have a singleton java class which runs whenever the weblogic server gets started and store the output in its object. I need to access this java object from a proxy service in osb.
    We tried using java call out and retrieved that object but we couldn't know how to parse that object into XML.
    We are not sure of using the java call out in osb to solve this purpose because whenever we use a java callout, that particular java code will run which is not the case of singleton class.
    So kindly help us how to retrieve the java object which holds the output without running the java code every time because its already run and holding the output in its object.
    Regards
    Prabhu

    here the doc http://download.oracle.com/docs/cd/E13159_01/osb/docs10gr3/userguide/context.html#wp1106656
    but I guess you are already at the stage of getting a POJO in a first Java Callout and passing the POJO to a second Java Callout, which should then return it to OSB as a XMLObject.
    My recommendation is to write a Java function which returns a XMLObject and uses a XMLCursor to populate it with the values of the POJO.
    An XMLObject returned to the OSB is automatically transformed in a "XML" variable (which in reality is represented as a XMLObject in the Pipeline context)
    Here some code sample:
    http://www.javamonamour.org/2010/09/how-to-create-xmlobject-using-xmlcursor.html

  • How to stop the running infospoke

    Hi Experts,
    there is a infospoke is still running so long time, it's incorrect, i'll cancel it as kick off the dependenies, i have no idea to how to stop the running infospoke. Anybody could tell me how to do it. thanks in advance.

    hi denny,
       Go to SM37 , find the job , stop the process
    or
       To stop the job find the job with the help of the request name ( TC - SM37 ),then in the job Details find the PID .
    find the process in the process Overview (SM50 / SM51 ).
    Set the restart to NO and Cancel the process without core
    u can also see these threads that are already posted:
    stopping v3 run job LIS-BW-VB_APPLICATION_02_010
    how to cancel or change the background job which is scheduled
      In SM37, for the job , click on Step button. Then from the menu Goto > variant. You can see the process chain name here.
    sure it helps
    Thanks
    Varun CN

  • How to stop the timing out of web Discoverer Plus

    I was working in Plus, and it drove me crazy that I would loose my work because I kept my session open and it would time out.How to stop the timing out of web Discoverer Plus?
    Thank you,
    Olga

    Please check Discoverer configuration guide which tells you which parameter to change in preference file(pref.txt).I think you change the value.

  • How to stop the upload process?

    Hi,
    i had by mistake started the other upload than the required!
    How to stop the current upload process,( generally it takes lot of time to upload the data as data is huge )
    Thanks,
    Ravi

    Hi Ravi Kottur 
    Just follow the things like Sunil suggested check one more just kill the process
    manually and goto RSMO and in the Monitor QM status just Amke it red manually
    for the load which you want to stop and then go to that particular info provider
    Manage and delte the request.. 
    Hope itz clear a little atleast...!
    Thanks & Regards
    R M K
    ***Assigning pointz is the only way of saying thanx in SDN ***
    **Learning the thingz is never end process if u stop it will Be a devil if u continue it will be a divine***
    > Hi,
    >
    > i had by mistake started the other upload than the
    > required!
    >
    > How to stop the current upload process,( generally it
    > takes lot of time to upload the data as data is huge
    > )
    >
    >
    > Thanks,
    > Ravi

  • How to stop the "minority report" type of personalized ads triggered by one's google search and appearing in the firefox browser?

    Please advise how to stop the "minority report" type of personalized advertising apparently triggered by one's google search and appearing in the firefox browser.
    == This happened ==
    Every time Firefox opened
    == a few months ago

    If another uses the computer with their own user account, everything
    is kept separate in each users folder. Only if someone used your account
    would that be an issue.
    Sometimes a problem with Firefox may be a result of malware installed on your computer, that you may not be aware of.
    You can try these free programs to scan for malware, which work with your existing antivirus software:
    * [http://www.microsoft.com/security/scanner/default.aspx Microsoft Safety Scanner]
    * [http://www.malwarebytes.org/products/malwarebytes_free/ MalwareBytes' Anti-Malware]
    * [http://support.kaspersky.com/viruses/disinfection/5350 Anti-Rootkit Utility - TDSSKiller]
    * [http://general-changelog-team.fr/en/downloads/viewdownload/20-outils-de-xplode/2-adwcleaner AdwCleaner] (for more info, see this [http://www.bleepingcomputer.com/download/adwcleaner/ alternate AdwCleaner download page])
    * [http://www.surfright.nl/en/hitmanpro/ Hitman Pro]
    * [http://www.eset.com/us/online-scanner/ ESET Online Scanner]
    [http://windows.microsoft.com/MSE Microsoft Security Essentials] is a good permanent antivirus for Windows 7/Vista/XP if you don't already have one.
    Further information can be found in the [[Troubleshoot Firefox issues caused by malware]] article.
    Did this fix your problems? Please report back to us!

  • How to stop the process chain showing status as yellow but no process step

    Dear Friends,
    How to stop the process chain showing status as yellow but no process step is running in that process chain.
    We have manually triggered the process chain for sales it finished successfully till load data but the next step is create index and DB statistics. Both of this steps are in unscheduled position only and no background job for this.
    Please guide me.
    Thanking you in advance.
    Regards,
    Shubhangi

    Hi,
      At times even I have faced this situation.  During those times, running the Function module RSPC_PROCESS_FINISH by passing the right parameters came to my rescue.
    Regards,
    Raj

  • How to stop the thread?

    Hi,
    How to stop the thread in java. This is my program.
    import java.net.InetAddress;
    public class ThreadPing extends Thread {
         ThreadPing(String pingIP)
              super(pingIP);
              start();
         public void run()
              try
              String pingIP = Thread.currentThread().getName();
              InetAddress inet = InetAddress.getByName(pingIP);
              Boolean get=inet.isReachable(1500);          
              if(get==true)
                   System.out.println(inet.getHostName());               
              }catch(Exception e)
         public static void main(String args[])
              for(int i=1;i<=100;i++)
                   String pingIP = "192.168.1."+i;
                   ThreadPing tp = new ThreadPing(pingIP);
    Thanks in advance.

    The simplest way to stop all the thread is to make all thread daemons and exit the program when you want them to stop.

Maybe you are looking for