Results 1 to 3 of 3

Thread: thered sync

  1. #1
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default thered sync

    im trying to create a code that will simulate tow car in a race ,
    after the race start only one car can enter to the pit -stop and the other one need to wait

    this is my code
    package exericse;

    public abstract class Cars implements Runnable {

    private String carname;
    private int timeInThePit = 0;



    public Cars(String carname) {
    super();
    this.carname = carname;
    }


    public synchronized void startRace() {
    try {
    wait(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.notifyAll();

    }

    public synchronized int pitTime() throws InterruptedException {

    timeInThePit = (int) (Math.random() * 2000);
    Thread.sleep(timeInThePit);
    return timeInThePit;

    }

    @Override
    public void run() {
    try {
    this.startRace();
    System.out.println("the race is on");

    this.pitTime();
    System.out.println("the car : " + getCarname() + " is in the pit" +" for "+ getTimeInThePit());

    } catch (Exception e) {
    System.out.println("run method eror");

    }

    }

    public String getCarname() {
    return carname;
    }

    public void setCarname(String carname) {
    this.carname = carname;
    }

    public int getTimeInThePit() {
    return timeInThePit;
    }

    public void setTimeInThePit(int timeInThePit) {
    this.timeInThePit = timeInThePit;
    }
    }



    package exericse;

    public class UseCars {


    /**
    * @param args
    */
    public static void main(String[] args) {

    Cars car1 = new Cars("bbbb") {
    };
    Cars car2 = new Cars("aaaa") {
    };
    car1.run();
    car2.run();


    }

    }


    any suggestion how to improve it
    how to make the cars enter randomly to the pit?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    But your code doesn't even compile yet, does it?

  3. #3
    miko5054 is offline Member
    Join Date
    Mar 2010
    Posts
    50
    Rep Power
    0

    Default

    im just running it on eclipse ,
    and follow to console....

    it`s an exercise for school :)

Similar Threads

  1. vertical sync
    By afraidofdark in forum Java 2D
    Replies: 0
    Last Post: 03-25-2010, 09:54 PM
  2. Thread sync problems!
    By barusk in forum Threads and Synchronization
    Replies: 9
    Last Post: 04-30-2009, 06:08 PM
  3. project is out of sync with file system?
    By anthrax in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2009, 06:19 PM
  4. JTable view/model sync problem
    By edcincy in forum AWT / Swing
    Replies: 0
    Last Post: 09-12-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •