-1

so in try to finish my h.w, and one thing in not go well to me yet. in this program i using 2 classes, this is a compaction of running, i just want to declare a place of the player who win the compation.

for example: we have 4 Thread. if the second Thread reach 100M last, i want to post a MSG that he came in 4th place.

class of Racer:

    package assig1_2;
    public class Racer extends Thread{
    public static int globalid = 1;
    private int id;
    private int speed;
    private Track track;
    public Racer(int Speed,Track track) {
        this.speed = Speed;
        this.track = track;
        this.id = this.globalid;
        this.globalid++;
    }
    public void run() {
        go ();
    }
    void go () {
        this.track.setPriority(this.speed);
        for(int i=1; i<=10; i++) {

            System.out.println("runner " + this.id + " run " + i + " meters " );
            if (i == 10) {
                this.track.setPlace(id);
                System.out.println("Runner " + this.id + " finished " + track.getPlaceNumber() + track.getPlaceName() );

            }
        }

    }
  }

class of Track:

   package assig1_2;

public class Track extends Thread {
    private int finishedRacers; 
    public String place;

    public void setPlace(int numOfPlace) {

        if (numOfPlace == 1) {
            this.finishedRacers = numOfPlace;
            this.place = "st";

        } else if (numOfPlace == 2) {
            this.finishedRacers = numOfPlace;
            this.place = "nd";

        } else if (numOfPlace == 3) {
            this.finishedRacers = numOfPlace;
            this.place = "rd";

        } else {
            this.finishedRacers = numOfPlace;
            this.place = "th";
        }


    }

    public int getPlaceNumber() {
        return this.finishedRacers;
    }

    public String getPlaceName() {
        return this.place;
    }

}

its silly program in our first lesson when we use Thread.

2 Answers2

1

I did few changes on your program and have introduced AtomicInteger and ExecutorService. No changes was made on Track Class.

public class Racer extends Thread {

private static AtomicInteger atomicInteger = new AtomicInteger(0);
private int id;
private int speed;
private Track track;

private Racer(){}

public Racer(int Speed, Track track) {
    this.speed = Speed;
    this.track = track;
}

public int getPosition(){
    return atomicInteger.incrementAndGet();
}

public static void main(String[] args) throws InterruptedException {
    ExecutorService service = null;
    service = Executors.newFixedThreadPool(4);
    for (int i = 1; i <= 4; i++) {
        service.execute(() -> {
            new Racer(1, new Track()).go();
        });
    }
    service.awaitTermination(3, TimeUnit.SECONDS);
    if (service != null) service.shutdown();
}

void go() {
    this.track.setPriority(this.speed);
    for (int i = 1; i <= 10; i++) {

        System.out.println("runner " + Thread.currentThread().getName().substring(7,15) + " run " + i + " meters ");
        if (i == 10) {
            this.track.setPlace(getPosition());
            System.out.println("Runner " + Thread.currentThread().getName().substring(7,15) + " finished " +track.getPlaceNumber() +" "+ track.getPlaceName() );
        }
    }
} }
Gaurav Pathak
  • 2,576
  • 1
  • 10
  • 20
0

this is the main. and we cannot adding to change something in there.

package assig1_2;

public class assig1_2_main {

public static void main(String[] args){
    Track track = new Track();
    Racer racer1 = new Racer(10, track);
    Racer racer2 = new Racer(2, track);
    Racer racer3 = new Racer(3, track);
    Racer racer4 = new Racer(7, track);
    Thread t1 = new Thread(racer1);
    Thread t2 = new Thread(racer2);
    Thread t3 = new Thread(racer3);
    Thread t4 = new Thread(racer4);
    t1.start();
    t2.start();
    t3.start();
    t4.start();
}

}

  • In your main the values you are passing to the Racer constructor are getting used as a Thread Priority. As per the API of Thread - Threads with higher priority are executed in preference to threads with lower priority. So in that cases your hard coded value of racer with highest priority will always finish first. – Gaurav Pathak Dec 05 '18 at 16:32