?
Solved

Sleep?

Posted on 2003-03-20
12
Medium Priority
?
332 Views
Last Modified: 2008-03-17
Hi,

Please see this excerpt of code....

if ((onLight) && (greenLight == false)){
            greenLight = true;
            System.out.println("Green");
            repaint();
           
            try{
                System.out.println("Asleep");
                Thread.currentThread().sleep(5000);
            }
            catch(InterruptedException e){
            }
        }

What I want to know is why does it sleep before the repaint() method is called, even though the sleep method is after it in the code??

Thanks
0
Comment
Question by:cullenswood
[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
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8173870
This is a Swing application yeah?

Swing performs all of it's painting in  another thread, so it cannot start doing the painting, as you have the processor control...

try:

if ((onLight) && (greenLight == false)){
           greenLight = true;
           System.out.println("Green");
           repaint();
           Thread.yield() ;
           
           try{
               System.out.println("Asleep");
               Thread.currentThread().sleep(5000);
           }
           catch(InterruptedException e){
           }
       }
0
 
LVL 35

Expert Comment

by:girionis
ID: 8173873
 It is always a good practice to have the repaint() after the sleep so the component can immediately repaint after the thread "wakes up".
0
 

Author Comment

by:cullenswood
ID: 8173998
That Thread.yield() didn't actually make any difference to it.   I understand that it would probably be better to have the repaint after the sleep (probably was going to have that aswell), its just that I want the program to do a repaint before it goes to sleep.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:cullenswood
ID: 8174020
Oh, and yes it is a Swing Application
0
 
LVL 2

Expert Comment

by:Aleph
ID: 8174073
A call to repaint() does not necessarily result in a call to paint() immediately, it only tells the component that it should be repainted sometime soon. (Because of this several calls to repaint() can result in one call to paint().)

If the repaint() call in your code is made from inside some event driven part of you code, you will have the event handler thread, which I believe is involved in repainting aswell, sleeping. If that is the case repainting will be done after you have returned from the event triggered method, and thus after you have sleept for five seconds.
0
 

Author Comment

by:cullenswood
ID: 8174091
Fair enough, thanks Aleph.   You dont know anyway around that by any chance??!
0
 
LVL 2

Expert Comment

by:Aleph
ID: 8174162
I guess that depends on what you intend to do with your program. One possibility might be to have another thread call repaint(), but it is usually better to solve problems without introducing more threads since they can slow things done and have a tendency to cause rather complicated bugs. Perhaps you could tell me a little bit more about what all this should actually do?
0
 

Author Comment

by:cullenswood
ID: 8174415
Basically Aleph,

THe program has a line, with dots along it.   When greenLight is true, the number of dots is going to decrease, when it is red the number will increase.  

if ((onLight) && (greenLight == false)){
           greenLight = true;
           System.out.println("Green");
           repaint();
           
           try{
               System.out.println("Asleep");
               Thread.currentThread().sleep(5000);
           }
           catch(InterruptedException e){
           }
           number -= 5;
           repaint();
       }

What I wanted it to do was that the greenLight would change, it would sleep for 5 secs, then the dots would decrease by 5.   But what is happening is that the greenLight is changing, and the dots decreasing, at the same time, after it has slept.

0
 
LVL 2

Accepted Solution

by:
Aleph earned 320 total points
ID: 8174785
Ok. In that case you could try to use another thread as a timer. Basically what you could do is to create a small class that runs as a thread (extends Thread) and, when it is started, just waits for a while and then does whatevery you like. There is actually a Timer class in the Java API which does something like that (and a couple of other things, for that matter).

It's documentation is available on the web at: http://java.sun.com/j2se/1.4/docs/api/java/util/Timer.html
0
 

Author Comment

by:cullenswood
ID: 8174882
Thanks for the help Aleph.   Can I ask u one more little question.   Below basically is the code for the small program I have made so far.   As it is, when u click on the red dot/light it changes to green, and vice versa.   What I eventually want to be able to do is have a program that every so often changes the light from green to red (ie a certain probability (say 1/p) that it will change to red) for a set number of seconds.   When it is red, there will be a probability (1/n) that extra dots will be added to the line, and when it is green a certain amount of dots (m) will be taken off the line!!!  
Hope I explained it ok!!

What I want to know is, would I do this by using threads or how??   I know it will probably be one continuous loop, going from green to red to green etc, but how would I go about doing the bit inbetween.

Thanks for your previous help anyway, greatly appreciated.




import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TLight extends JFrame implements MouseListener,
                                                MouseMotionListener{

    static double length;
    static int points[][];
    private JLabel statusBar;
    int maxCars, number;
    static boolean greenLight;
    private boolean onLight, doubleClick;

    public TLight(){
        super("Traffic Light");

        statusBar = new JLabel();
        getContentPane().add(statusBar, BorderLayout.SOUTH);
        addMouseListener(this);
        addMouseMotionListener(this);

        maxCars = 13;
        number = 13;

        length = Math.sqrt(Math.pow((200 - 200), 2) + Math.pow((300 - 100), 2));

    }

    public void paint( Graphics g ) {
        g.clearRect( 0, 0, 400, 400);
        draw(g);
    }

    public void draw(Graphics g){

        points = new int [maxCars][2];
        int count = 0;
        points[0][0] = 300;
        points[0][1] = 200;
        for(int i = 1; i < number; i++){
            points[i][0] = (int)(300 - (length / maxCars) - count);
            points[i][1] = 200;
            count += length/maxCars;
        }
        if (number < 13){
            for (int i = number; i < 13; i++){
                points[i][0] = points[number-1][0];
                points[i][1] = points[number-1][1];
            }
        }

        g.drawLine( 100, 200, 300, 200);

        if (greenLight == false){
            g.setColor(Color.RED);
        }
        else{
            g.setColor(Color.GREEN);
        }
        g.fillOval( 300, 180, 20, 20);

        for (int i = 0; i < points.length; i++){
            int r = (int)(Math.random() * 256);
            int gg = (int)(Math.random() * 256);
            int b = (int)(Math.random() * 256);
            g.setColor(new Color( r, gg, b));
            g.fillOval( points[i][0] - 10, (points [i][1] - 10), 10, 10);

        g.setColor(Color.BLACK);
        }
    }
    public void onTLight( int mouseX, int mouseY ) {
        doubleClick = false;
        int x = 300 + 10;
        int y = 200 - 10;
        int xdiff = mouseX-x;
        int ydiff = mouseY-y;
        onLight = (xdiff*xdiff + ydiff*ydiff <= 100 /* 10*10(ie radius) */ );


        if ((onLight) && (greenLight == false)){
            greenLight = true;
            System.out.println("Green");
            repaint();

            number -= 4;

        }
        else if ((onLight) && (greenLight == true)){
            greenLight = false;
            System.out.println("Red");
            repaint();

            number += 3;

        }
    }

    public void mouseClicked(MouseEvent e){
       
    }

    public void mousePressed(MouseEvent e){
        onTLight(e.getX(), e.getY());

    }

    public void mouseReleased(MouseEvent e){
   
    }

    public void mouseEntered(MouseEvent e){
       
    }

    public void mouseExited(MouseEvent e){
       
    }

    public void mouseDragged(MouseEvent e){

    }

    public void mouseMoved(MouseEvent e){
       
    }

    public static void main( String args[] ) {
        TLight app = new TLight();
        app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        app.setSize( 400, 400 );
        app.setVisible( true );
    }
}
0
 
LVL 2

Expert Comment

by:Aleph
ID: 8175494
If you use the Timer class, one way to do it could be to simply add TimerTasks that the timer should repeat with a short period. Each time a TimeTask object you have created is executed, you could with a small probability change the light, or add or remove some dots.

Another way is to randomize a delay to the next time the light should change, and instruct the Timer to execute a TimerTask that changes the light after that delay.

As I understand, it is possible to add multiple TimerTasks to a Timer object, so you could create just one Timer object and use it for all timed things.
0
 

Author Comment

by:cullenswood
ID: 8175533
Cheers Aleph,

Thanks for the help and ideas.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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