Solved

Thread sleeping

Posted on 1997-05-05
5
247 Views
Last Modified: 2010-04-16
I wrote a scrolling text class that runs in a thread.  I control the speed at which the text scrolls by having the Thread sleep, but I found that there was a HUGE difference in speed between 4ms and 5 ms sleep times.  Is this just the way is it?  actually, everything from 100ms down to 5 ms looks to be the same speed, then 4 ms is super fast.

Here's the code incase it is some other problem:

import java.applet.Applet;
import java.awt.*;

public class menuapp extends Applet {
      vPanel myPanel;

      public void init() {
            myPanel = new vPanel();
            setLayout(null);
            add(myPanel);
      }

      public void paint(Graphics g) {
            myPanel.reshape(0,0,300,100);
      }
}

class vPanel extends Panel implements Runnable {

      Thread vThread;
      int xx,yy;

      public vPanel() {
            xx = 10;
            yy = 10;
            vThread = new Thread(this);
            vThread.start();
      }

      public void run() {
            try {
                  while(true) {
                        for(xx = 300;xx > 0;xx--) {
                              vThread.sleep(5);
                              repaint();
                        }
                  }
            }
            catch(InterruptedException e) {}
      }

      public void paint(Graphics g) {
            g.drawString("Sign up now!",xx,yy);
      }
}
0
Comment
Question by:purple26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220150
Is this for a Win32 plattform or UNIX?

If its a Windows platform there is an explanation, but no solution.
0
 

Author Comment

by:purple26
ID: 1220151
It is windows 95.  From your comment I think I have an idea as to what is going on, but I would like to hear what you have to say.

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220152
Here goes:

There are two problems that are affecting your code:

1.- The windows scheduler (both 95 and NT) is not very accurate when assigning priorities to threads. Priorities diferencec must be in the high hundreds to start making a diference. In your code, this translates to the JVM paying lots of attention to one given thread and suddenly hopping to another one, causing the sudden increments in speed.

2.- The java sleep method under Windows utilizes the PC's timers. These timers are contained in the UART, and do not use the CPU clock. They are therfore limited to 1 Msec resolution under DOS and 5 msec. resolution under windows (Due to the scheduler). This % msec resolution has an error range of as high as 50% and doesnt become accurate untill you reach the 60 msec resolution.

Your code is using 4 ms calls (which are under the win resolution) and 5ms calls which are totaly inacurate over windows.

The only feasable work arround I could see for windows is to use native methods that call anon interuptable timer. There is sample code on how to do this in Undocumented Windows (for Win 3.1) by Shulman et. al.

Of course, I believe this doesn't solve your problem so I'm answering with a comment.
0
 

Accepted Solution

by:
rickl032797 earned 50 total points
ID: 1220153
First, you should not call repaint() for animation type stuff. repaint() tells the system to paint asap but there is no guarantee when it will occur. For your code as it is, you can call paint() directly or for that matter put the drawtext right in your loop.
0
 

Author Comment

by:purple26
ID: 1220154
Thank you for your insight to optimizing my code.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question