Results 1 to 6 of 6
  1. #1
    jyotigupta6 is offline Member
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default execution of threads

    I want to execute one thread in background executing the query, every half second (500 micro seconds) or 1 second (1000 micro seconds) depending on my requirement and I want to keep updating a variable with the value. I am not able to understand how to write this logic. Could anyone suggest me on this???

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    class Timer and Timer Task - often the awt / swing version .... btw it is milliseconds, not microseconds.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    jyotigupta6 is offline Member
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default Re:

    But I am not using swing and awt. It is core java program.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If your program is a non-GUI Java prog then Nicholas Jordan's util Timer and TimerTask answers are the correct replies here.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Your original description of the problem leads directly to the correct solution. Have one thread doing whatever it is, and spawn a second thread that calls

    Java Code:
    Thread.sleep(500);  // to sleep for half a second
    periodically. Use Barriers to control communications.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Yep, per ftr and Fubarable but we need to insure query(n) completes before query(n+1) and in general is approached using Barrier class, but problem as stated can lead to .....

    It's somewhat potenetially complex, but for some reason is very easy and clearly obvious for me: If T-2 sleeps, then in general it cannot overrun it's own path of execution. If you try to drive the Thread with Timer and TimerTask you would have to examing the documentation for that class to insure that Timer will not fire TimerTask before previous invocation has completed.

    Challenge devolves on what it is exactly you are to accomplish. If there is no GUI, then is it Polling? Some critical programing is in fact done using polling but people will tell you that it is unreliable, which is close to truth because true polling absolutely has to have realistic os for what is the system and environment that needs polling. I only use Thread.sleep(int milliseconds) and Thread.yield(); to make some or another already low-priority Thread run as control Thread - IOW not interfere with worker thread or consume system resources but instead set a boolean to false in a loop condional in the worker thread.

    Polling, implemented correctly, will not run on consumer-grade operating systems nor consumer-grade hardware.

    Just will not happen, caveat emptor.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Threads execution stop
    By jyotigupta6 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-31-2008, 02:41 PM
  2. slow execution and flickery graphics. too many threads?
    By lost_in_java in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-23-2008, 03:26 AM
  3. Package/Execution woes...
    By Hiro ZA in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 08:42 AM
  4. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 10:51 AM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •