473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

sleep() Problem

14
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 :



Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3.  
  4.  
  5. public class MultiThread {
  6.  
  7.  
  8.  
  9.     public static void main (String []args) {
  10.  
  11.         Frame frame = new Frame("Frame");
  12.  
  13.         Frame frame2 = new Frame("Frame 2");
  14.  
  15.         Threads t1 = new Threads ();
  16.  
  17.         Threads t2 = new Threads ();
  18.  
  19.         frame.add(t1);
  20.  
  21.         frame.setVisible(true);
  22.  
  23.         frame.setSize(400,800);
  24.  
  25.         frame.setLocation(100, 0);
  26.  
  27.         frame2.add(t2);
  28.  
  29.         frame2.setVisible(true);
  30.  
  31.         frame2.setSize(400,800);
  32.  
  33.         frame2.setLocation(500, 0);
  34.  
  35.         new Thread(t1).start();
  36.  
  37.         new Thread(t1).start();
  38.         new Thread(t2).start();
  39.         new Thread(t1).start();
  40.         new Thread(t2).start();
  41.     }
  42.  
  43. }
  44.  
  45.  
  46. class Threads extends JComponent
  47. implements Runnable {
  48.  
  49.  
  50.  
  51.     int x,y;
  52.  
  53.     Threads( ) {}
  54.  
  55.  
  56.  
  57.     public void run () {
  58.  
  59.         while (x<100) {
  60.  
  61.             x++;
  62.             y++;
  63.  
  64.             repaint();
  65.  
  66.             try {
  67.  
  68.                 Thread.sleep(23);
  69.             }
  70.  
  71.             catch (Exception e){};
  72.         }
  73.         if (x>=100) x =0;
  74.     }
  75.  
  76.     public void paint (Graphics g) {
  77.  
  78.         g.drawLine(x,y,x+30,y+44);
  79.  
  80.         }
  81. }
  82.  
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
Mar 31 '07 #1
4 1730
JosAH
11,448 Expert 8TB
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 :



Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. [ ... ]
  4.         new Thread(t1).start();
  5.  
  6.         new Thread(t1).start();
  7.         new Thread(t2).start();
  8.         new Thread(t1).start();
  9.         new Thread(t2).start();
  10.  
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
Mar 31 '07 #2
cozsmin
14
THANX MAN , i was just wondering and now i understand, and seems so logical
THANKX AGAIN
Mar 31 '07 #3
cozsmin
14
i am shure that syncronizing it will solve the problem :D
Mar 31 '07 #4
JosAH
11,448 Expert 8TB
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
Mar 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Anks | last post by:
hi I am displaying a digital clock in an applet.I am using Thread.sleep (1000) for counting every second.This works fine some times but some times my clock just slows down or give an...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
7
by: lokb | last post by:
Hi, I am creating a detach thread as shown below and caling pthread_create in a while loop where the file names in the directory are fetched and is passed as a parmater to pthread create. The...
2
by: rbt | last post by:
I have a win32 service written in Python. It works well. It sends a report of the status of the machine via email periodically. The one problem I have is this... while trying to send an email, the...
5
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.