Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

(newbie) Thread problem

Thread view: 
Vanessa Berni - 23 Feb 2006 08:11 GMT
Hi,
I'm trying to write a program that simulate traffic. I've got a class
"Scheduler" that simulate the movement of streets, cars ...
I've created an applet which calls a thread; in his method "run" I simply
call the method run of the Scheduler (which modifies cars position ...)
(action A) and then I repaint all the streets and cars (action B) then the
thread sleeps 30 millis (action C)

The thread start and so actions A,B,C, A,B,C, A,B,C, A,B,C ... are executed.
At a certain point while executing event B (repaint) the action A starts
again....
Why???

Can you please help me??

Thank you Vanessa

public class JSim extends JPanel implements Runnable{
   public Thread runner = null;
   public static Schedulatore scheduler = new Schedulatore();

   public SimulatoreCanvas() {
       super();
   }

   public void run() {
       startTime = System.currentTimeMillis();
       while (Thread.currentThread() == runner) {
           try {
                   //action A
                   this.scheduler.run();

                   //action B
                   repaint();

                   //action C
                   Thread.sleep(30);

           }
           catch (InterruptedException e) { ;
           }
       }
   }

    public void start() {
       if (runner == null) {
           runner = new Thread(this);
           runner.start();
       }
   }

   public void paintComponent(Graphics g){
       //... code that paints street objects
   }
}
Paul Hamaker - 23 Feb 2006 08:52 GMT
repaint is only a request to have the component painted asap. This is
done in a separate thread, so the fact is, that repaint does not result
in paintComponent being executed immediately.
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Vanessa Berni - 23 Feb 2006 09:11 GMT
Thank you
The problem is I CAN'T execute action A if action B is still running...

To try solving the problem I've added a boolean flag (canSim). If flag is
false action A won't be performed...

At the beginning of paintComponent I set it to false and at the end of the
method I set it to true. Is there another way??

Thanks Vanessa

   boolean canSim=true;

   public void run() {
       startTime = System.currentTimeMillis();
       while (Thread.currentThread() == runner) {
           try {
                   //**********************
                   //**********************
                   if(canSim){

                       //action A
                       this.scheduler.run();

                       //action B
                       repaint();

                       //action C
                       Thread.sleep(30);
                   }

           }
           catch (InterruptedException e)
           {
               ;
           }
       }
   }

> repaint is only a request to have the component painted asap. This is
> done in a separate thread, so the fact is, that repaint does not result
> in paintComponent being executed immediately.
> --------------------
> Paul Hamaker, SEMM, teaching ICT since 1987
> http://javalessons.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.