Solved

Thread sleeping

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now