【问题标题】:Check who wins the competition on 2 classes with using Thread使用 Thread 检查谁在 2 个班级的比赛中获胜
【发布时间】:2018-12-05 02:56:17
【问题描述】:

所以在尝试完成我的硬件时,还有一件事对我来说并不顺利。 在这个程序中,我使用了 2 个类,这是运行的压缩, 我只是想宣布赢得比赛的玩家的位置。

例如:我们有 4 个线程。 如果第二个线程最后达到100M,我想发布一个他排在第4位的MSG。

赛车手级别:

    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() );

            }
        }

    }
  }

轨道类别:

   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;
    }

}

当我们使用 Thread 时,它在我们第一课中的愚蠢程序。

【问题讨论】:

    标签: java multithreading oop


    【解决方案1】:

    我对您的程序进行了一些更改,并引入了 AtomicInteger 和 ExecutorService。 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() );
            }
        }
    } }
    

    【讨论】:

    【解决方案2】:

    这是主要的。 而且我们不能添加来改变那里的东西。

    package assig1_2;
    

    公共类 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();
    }
    

    }

    【讨论】:

    • 在您的 main 中,您传递给 Racer 构造函数的值被用作线程优先级。根据 Thread 的 API - 优先级较高的线程优先于较低优先级的线程执行。因此,在这种情况下,您的最高优先级赛车手的硬编码值将始终首先完成。
    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多