Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Thread sleeping

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:
Suggested Courses

610 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