0
    private static int millis_per_tick = 1;
    public void run() 
    {   
      // While not stop do
      while(true) 
      {
        Thread t = Thread.currentThread();
        long startTime, timeTaken;          
        startTime = System.currentTimeMillis();
        act();
        timeTaken = System.currentTimeMillis() - startTime;
        if(timeTaken < millis_per_tick )
        {
            try 
            {
                Thread.sleep(millis_per_tick-timeTaken );
            }

            catch(InterruptedException ex)
            {}

        }

The thread.sleep method does not accept double values only float and integer. But i need a value under 1.

In Space

public void act() 
{
    ListIterator<Body> iterator = bodies.listIterator();
    while (iterator.hasNext())
    {
        Body body = iterator.next();
        body.wirkenKraefte();
        body.move();
    }
    spaceGUI.repaint();
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • i must better say that if i change millis per tick from 0 to 1 the act() -method(repaint and calculating position) are very slowly – Mr. Knister Oct 01 '11 at 09:58
  • i have just noticed that i have in my act method a while() - method i edit my question with the act method – Mr. Knister Oct 01 '11 at 10:00

3 Answers3

1

Check out the Thread.sleep(long,int) method in the API.

Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Yup, I've only just seen that myself and edited into my answer - but note that it depends on the precision of the system clock, and I would expect *most* systems to fail to sleep for sub-millisecond periods. – Jon Skeet Oct 01 '11 at 09:48
0

This is not possible. 1ms is even more than 1ms (15.6ms) and this is not guranteed.

-> Can I improve the resolution of Thread.Sleep?

Community
  • 1
  • 1
Tobias
  • 9,170
  • 3
  • 24
  • 30
0

You can't sleep for less than a millisecond using Thread.sleep(long) - and on many platforms, you'll find that even sleeping for one millisecond will actually sleep for longer (e.g. 15 milliseconds) depending on the granularity of the system clock.

In other words, you basically can't do what you're trying to do with Thread.sleep(long) - and trying to get a sub-millisecond precision time is troublesome in the first place. You can try using System.nanoTime() instead, which is only appropriate for elapsed times (so fine in this case) - and you might want to consider a spin-lock to wait until the right time, instead of sleeping.

In most cases it's a bad idea to even try to execute something at a fixed rate of 1000 calls per second though... what's the use case here? There may be a better approach.

EDIT: I've just noticed that there is Thread.sleep(millis, nanos) - you could try using this and sleep for (say) 0 milliseconds and 100,000 nanoseconds. You should be aware that on most platforms this won't actually do what you want though. You can try it for your particular use case, but I wouldn't personally hold out too much hope.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ok my use case is that i have an space with two planets. In my act() method it calculate his new position with an vector than it make a repaint. – Mr. Knister Oct 01 '11 at 09:49
  • 1
    @Mr.Knister: And why do you need to do that 1000 times per second? Surely you won't be able to display it that often anyway... – Jon Skeet Oct 01 '11 at 09:49
  • hmm.. you right but if i change millis per tick at 0 it works very well but i want change the speed with changing of the millis per tick but if i change it to 1 it stops/goes very slow – Mr. Knister Oct 01 '11 at 09:54
  • @Mr.Knister: That's basically because it's the difference between "not sleeping at all" and "sleeping between each tick". You need to work out what your *real* requirements are. You shouldn't really be blocking the UI thread like this anyway to be honest... – Jon Skeet Oct 01 '11 at 10:33
  • this program should be sleep on every stage that means calculating-->repaint-->sleep-->calculating... i hope i understand the question :) Sorry – Mr. Knister Oct 01 '11 at 10:50
  • @Mr.Knister: But you still shouldn't block the UI thread by making it sleep. Instead, you should schedule a timer for the UI thread to call into your recalculation task. Otherwise you're stopping all the other UI operations from occurring. – Jon Skeet Oct 01 '11 at 11:08
  • Could you please a simple programm code for a schedule timmer please? – Mr. Knister Oct 01 '11 at 11:09
  • @Mr.Knister: Not easily, especially as your question doesn't tell us *anything* about the context - is this in Android, Swing, etc? – Jon Skeet Oct 01 '11 at 11:11
  • I am programming with Eclipse and create the gui with eclipse plugin jiggloo (java.swing) is that what you mean? – Mr. Knister Oct 01 '11 at 11:15
  • @Mr.Knister: Okay, it sounds like a Swing UI then. (It would have been worth stating that and the programming language in the question.) Look at `javax.swing.Timer`. – Jon Skeet Oct 01 '11 at 11:29