?
Solved

Thread sleeping

Posted on 1997-05-05
5
Medium Priority
?
250 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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