sleep() Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cozsmin
    New Member
    • Mar 2007
    • 14

    sleep() Problem

    Hy i`m new to java ,studin it for 3 months;
    I got a code here ; works fine , no errors , but has a weird behavior
    here is the code :



    Code:
     import java.awt.*; 
    import javax.swing.*;
     
     
    public class MultiThread {
     
     
     
    	public static void main (String []args) {
     
    		Frame frame = new Frame("Frame");
     
    		Frame frame2 = new Frame("Frame 2");
     
    		Threads t1 = new Threads ();
     
    		Threads t2 = new Threads ();
     
    		frame.add(t1);
     
    		frame.setVisible(true);
     
    		frame.setSize(400,800);
     
    		frame.setLocation(100, 0);
     
    		frame2.add(t2);
     
    		frame2.setVisible(true);
     
    		frame2.setSize(400,800);
     
    		frame2.setLocation(500, 0);
     
    		new Thread(t1).start();
     
    		new Thread(t1).start();
    		new Thread(t2).start();
    		new Thread(t1).start();
    		new Thread(t2).start();
    	}
     
    }
     
     
    class Threads extends JComponent
    implements Runnable {
     
     
     
    	int x,y;
     
    	Threads( ) {}
     
     
     
    	public void run () {
     
    		while (x<100) {
     
    			x++;
    			y++;
     
    			repaint();
     
    			try {
     
    				Thread.sleep(23);
    			}
     
    			catch (Exception e){};
    		}
    		if (x>=100) x =0;
    	}
     
    	public void paint (Graphics g) {
     
    		g.drawLine(x,y,x+30,y+44);
     
    		}
    }
    so far so good but just compile it and u will see that the lines will just have a bigger sleep time each time a Tread.strat() begins.
    S oif someone can tell me how to stop sleep to sleep more each time pls tell me

    Thank in advance
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by cozsmin
    Hy i`m new to java ,studin it for 3 months;
    I got a code here ; works fine , no errors , but has a weird behavior
    here is the code :



    Code:
     import java.awt.*; 
    import javax.swing.*;
    [ ... ]
    		new Thread(t1).start();
     
    		new Thread(t1).start();
    		new Thread(t2).start();
    		new Thread(t1).start();
    		new Thread(t2).start();
    You are starting a thread more than once on the same Components t1 and t2;
    a thread draws a little line and updates its coordinates; two threads can do
    more than one (when one thread is sleeing the other one advances that little
    line again); that's why the line is moving faster than you'd expected.

    Start just two threads, once for eact t1 and t2 and see what happens.

    kind regards,

    Jos

    Comment

    • cozsmin
      New Member
      • Mar 2007
      • 14

      #3
      THANX MAN , i was just wondering and now i understand, and seems so logical
      THANKX AGAIN

      Comment

      • cozsmin
        New Member
        • Mar 2007
        • 14

        #4
        i am shure that syncronizing it will solve the problem :D

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by cozsmin
          i am shure that syncronizing it will solve the problem :D
          If you just use one drawer thread per component you don't need any synchronization
          at all; the AWT dispatcher thread (the one that manages all actual drawing)
          handles it and your two Components don't interfere with each other.

          kind regards,

          Jos

          Comment

          Working...