• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a thread sleep?

 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want a cerain operation to be performed for every stipulated amount of time until the user stops.This time is set by the user.For eg.letus consider some 10 seconds.
Suppose I use
Thread.sleep(600);
the thread waits for not even a second.
When I used 1800 instead of 600 the thread slept for 3 seconds then 2 seconds then 3 & so on.
What is the problem .Plz help me as soon as possible.
Happy middloing with java.
Netharam.
 
Robert Ziel
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
same answere as in other thread...
the sleep sleeps for x mili seconds
so sleep(500) sleeps for at least half a second..
Robert
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I understand that, but how many milliseconds is equal to a second?
Happy middling with java.
Netharam. :roll:
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think Robert has already answered that.
1000 milliseconds = 1 second.
500 milliseconds = half a second.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by netharam ram:
Hi I want a cerain operation to be performed for every stipulated amount of time until the user stops.

If you are using JDK 1.3 or later, you can also use the java.util.Timer class to do this.
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why is this frustration here that delays for 2 seconds then 3 then 2 again.Why is this so?
Happy middling with java.
Netharam. :roll:
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cant force the processor to run this thread when you want it.
you can tell the thread to sleep for at least half a second as Robert mentioned.
after then it returns to the ready state where it waits for the cpu to run it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't guarantee much with threads, but in my experience you should be able to get pretty fast and consistent response with a well-written program. If you're seeing as much variation as 2-3 seconds for a sleep(1800), I'm guessing you've got some other threads running that are keeping the JVM too busy to act in a timely manner. Or perhaps there are other processes on the system, outside the JVM. For the former, try using the setPriority() method in Thread to make your sleeping thread higer priority - this should help it wake more promptly right after 1800 msec have elapsed. If that doesn't help, look at other processes on your machine using Windows Task Manager, or "top" (Unix), or some other utility. Good luck.
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jim let me try with setPriority().But I have to create this sleeping threads for every client connected to my system,will that owrk fine if I use setPriority()?
Please reply .
Happy middling with java.
Netharam. :roll:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to say. It depends on what exactly is tying up the system. If the delay is caused by some other threads, then setting the priority of your timer thread will increase the chances that it will time correctly. But if the delay is caused by the timer threads themselves (and if there's going to be a lot of them, this is certainly possible) then increasing timer thread priority won't help. It's worth a try though to see what happens.
I reread the problem, and a few more things occurred to me. What is this task you're trying to execute every x seconds? How long does the task itself take to execute? Thread.sleep(1800) will sleep at least 1.8 seconds in between task executions - but if the task takes 1 sec to execute, you'll see it execute every 2.8 seconds. So that could be the cause of your perceived delay right there.
Scheduling a repeated task is most easily accomplished using java.util.Timer - check it out. In particular look at scheduleAtFixedRate() vs. schedule().
Does it work OK when there's a single user, but not when there are multiple users? If so, you'll want to look at what synchronized methods are being called. These can force one thread to wait for another, creating delays. Typically you want to minimize the need for different threads to call synchronized code on the same instance. How you do this will depend on what your program does though - it's hard to say without more details.
 
netharam ram
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm capturing the screen of the specified Client after every 'n' seconds & getting the Bufferedimage from the clients.So u have one thread for one client & there will be 'n' threads running for 'n' clients in the server.Now how to optimize my server application.Are the details suficient?
Happy middling with java.
Netharam.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you need to look into a JRTOS or Java RealTime OS. A preemptive multitasking OS will not guarantee any process any amount of time, but it will make a valiant effort to give those priority that claim they need it.
How many threads are you running? How are you timing the frequency of your methods execution?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!