Solved

Thread sleeping

Posted on 1997-05-05
5
244 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
  • 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

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

756 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