Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I do not understand how multi-thread part of program works

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question I do not understand how multi-thread part of program works

    I am working on this project in my java text book and I am unable to figure out how the multi thread part of the program works. The program that I am working on is a traffic light simulation program. I have a little understanding how multi thread works. I am particularly baffled about the waitForChange() method. I just cant figure out how it knows that the simulated light has changed. I understand that the method is syncronized so, when the wait() method is called, it waits until it is notified to start again. I understand how it works a little bit but, not enough to do the exercise in my text book. could someone please help me?

    Thanks,
    Truck35

    // An improved version of the traffic light simulation that 
    // stores the light delay in TrafficLightColor. 
     
    // An enumeration of the colors of a traffic light. 
    enum TrafficLightColor {  
      RED(12000), GREEN(10000), YELLOW(2000); 
     
      private int delay; 
     
      TrafficLightColor(int d) { 
        delay = d; 
      } 
     
      int getDelay() { return delay; } 
    } 
     
    // A computerized traffic light. 
    class TrafficLightSimulator implements Runnable { 
      private Thread thrd; // holds the thread that runs the simulation 
      private TrafficLightColor tlc; // holds the current traffic light color 
      boolean stop = false; // set to true to stop the simulation 
      boolean changed = false; // true when the light has changed
     
      TrafficLightSimulator(TrafficLightColor init) {  
        tlc = init; 
     
        thrd = new Thread(this); 
        thrd.start(); 
      } 
     
      TrafficLightSimulator() {  
        tlc = TrafficLightColor.RED; 
     
        thrd = new Thread(this); 
        thrd.start(); 
      } 
     
      // Start up the light. 
      public void run() { 
        while(!stop) { 
     
          // Notice how this code has been simplified! 
          try { 
            Thread.sleep(tlc.getDelay()); 
          } catch(InterruptedException exc) { 
            System.out.println(exc); 
          } 
     
          changeColor(); 
        }  
      } 
     
      // Change color. 
      synchronized void changeColor() { 
        switch(tlc) { 
          case RED: 
            tlc = TrafficLightColor.GREEN; 
            break; 
          case YELLOW: 
            tlc = TrafficLightColor.RED; 
            break; 
          case GREEN: 
           tlc = TrafficLightColor.YELLOW; 
        } 
     
        changed = true; 
        notify(); // signal that the light has changed 
      } 
     
      // Wait until a light change occurs. 
      synchronized void waitForChange() { 
        try { 
          while(!changed)
            wait(); // wait for light to change 
          changed = false;
        } catch(InterruptedException exc) { 
          System.out.println(exc); 
        } 
      } 
     
      // Return current color. 
      synchronized TrafficLightColor getColor() { 
        return tlc; 
      } 
     
      // Stop the traffic light. 
      synchronized void cancel() { 
        stop = true; 
      } 
    }  
     
    class TrafficLightDemo {  
      public static void main(String args[]) {  
        TrafficLightSimulator tl =
          new TrafficLightSimulator(TrafficLightColor.GREEN); 
     
        for(int i=0; i < 9; i++) { 
          System.out.println(tl.getColor()); 
          tl.waitForChange(); 
        } 
     
        tl.cancel(); 
      }  
    }
    


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,380
    Thanks
    54
    Thanked 2,636 Times in 2,586 Posts

    Default Re: I do not understand how multi-thread part of program works

    Try debugging the code by adding println statements that print messages as the code is executed so you can see when and where events happen.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Red face Re: I do not understand how multi-thread part of program works

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding println statements that print messages as the code is executed so you can see when and where events happen.
    Thanks for your reply.

    Truck35

  4. #4
    Junior Member
    Join Date
    Jul 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: I do not understand how multi-thread part of program works

    Hello, It's the same problem I'm facing, and thanks for sharing this question.

  5. #5
    Junior Member
    Join Date
    Jun 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I do not understand how multi-thread part of program works

    Typically, when a company is ready to purchase a software program, they have several choices. They can choose to work with an open source or closed source application development system, or they can select an on-premise or off-site online marketplace development. With on-site development, the product owner can monitor the progress of development and ensure that the software meets their specific needs before making any purchases. Many businesses also prefer to work with a dedicated software team instead of an on-site team because they can request features that are not currently available in a development environment and request them again at a later date. The business analysts responsible for the development of the application must meet a quality threshold in order to be considered for approval by the customer.
    Last edited by katespy; October 30th, 2021 at 10:15 AM.

Similar Threads

  1. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  2. 2 questions about GUI and one for my curiousity(multi thread)
    By derekxec in forum Java Theory & Questions
    Replies: 2
    Last Post: June 29th, 2011, 05:31 PM
  3. Multi-CPU Thread Execution
    By bgroenks96 in forum Threads
    Replies: 4
    Last Post: June 26th, 2011, 11:31 PM
  4. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 12:35 AM
  5. Converting Code to Multi Thread
    By cmill_xc in forum Threads
    Replies: 1
    Last Post: December 6th, 2010, 12:39 PM