HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Multithreaded horse race simulation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
horsesimulationracemultithreaded

Problem

I'm just starting to get along with threads in Java and I need some code review,
about interfaces, classes, managing threads, correct writing, secure threads, exposed objects etc.

Horse-Race Implementation:

package HorseRace;

public interface FinishingLine {

    public void arrive(Horse h);
}


package HorseRace;

import java.util.Vector;

public class FinishingLineImpl implements FinishingLine {

    Vector Ranking = new Vector(10);

    public FinishingLineImpl(){}
    @Override
    public void arrive(Horse h) {
        // TODO Auto-generated method stub
        Ranking.add(h.getId());
    }

    public void print()
    {
        for (int i = 0; i < 10 ; i++)
        {
            System.out.println("Place " + (i + 1)+ " is : " + Ranking.get(i));
        }
    }
}


package HorseRace;

public interface Horse {

    public void run();
    public String getId();
}

package HorseRace;
import java.util.Random;

public class HorseImpl implements Horse,Runnable {

    int _distance;
    String _id;
    FinishingLine _f;

    public HorseImpl(String id, FinishingLine f)
    {
        _id = id;
        _distance = 0;
        _f = f;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        Random rand = new Random();

        int n = rand.nextInt(10) + 1;

        while (_distance < 100)
        {
            try
            {
                Thread.sleep(n);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            _distance += 1;
        }

            _f.arrive(this);
    }

    @Override
    public String getId() {
        // TODO Auto-generated method stud
        return _id;
    }
}


```
package HorseRace;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main {

public static void main(String[] args) throws InterruptedException {
// TODO Auto

Solution

`// package name should be lower case
package HorseRace;

public interface FinishingLine {

public void arrive(Horse h);
}

package HorseRace;

import java.util.Vector;

public class FinishingLineImpl implements FinishingLine {

// Vector is discouraged, unless you are using an ancient version of java
// use Collections.synchronizedList(new ArrayList()) instead
// variable should start with a lowercase letter, i.e. camelCase
Vector Ranking = new Vector(10);

public FinishingLineImpl(){}
@Override
public void arrive(Horse h) {
// TODO Auto-generated method stub
Ranking.add(h.getId());
}

// override toString instead
public void print()
{
// magic numbers are evil; use ranking.size() instead
for (int i = 0; i

Context

StackExchange Code Review Q#36806, answer score: 2

Revisions (0)

No revisions yet.