0

hello i was trying to understand about threads and i was asked to simulate a competence between to elements like a race between to objects but i need to use java threads and compare thread vs runnable. I implemented the following:

public class lamborgini extends Thread {

     public void run() {
     int distance = 1000;
     int steps = 0;
     int velocity = 45;
     int acelerationTime = 800;
     while (steps < distance) {
     System.out.println("Lamborgini running");
     steps+=velocity;
     Thread.sleep(acelerationTime);
    }
    }
    }

    public class ferrari implements Runnable {
    @Override
     public void run() {
     int distance = 1000;
     int steps = 0;
     int velocity = 130;
     int acelerationTime = 950;
     while (steps < distance) {
     System.out.println("Lamborgini running");
     steps+=velocity;
     Thread.sleep(acelerationTime);
    }
    }
    }

    public class RaceMain {
     public static void main(String[] args){
     lamborgini a = new lamborgini();
     lamborgini.start();

     ferrari b = new ferrari();
     ferrari.run();
    }
    }

But is this the right way? why use run and why use start? and how can I know which of the threads come first?

MPelletier
  • 16,256
  • 15
  • 86
  • 137

7 Answers7

3

You are using ferrari the wrong way. You would do

Thread ferrariThread = new Thread(new ferrari());
ferrariThread.start();

Both threads need to be start()ed. In your current use, run() is not actually running in a different thread. The difference between extending Thread and implementing Runnable is mainly a matter of use case, but the primary difference is that this inside the Thread refers to the running thread, whereas for the Runnable it doesn't.

Community
  • 1
  • 1
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
  • Based on the info in the link you provide, extending Thread vs. implementing Runnable is NOT a matter of taste. The overwhelming verdict was that implementing Runnable is preferred. – user949300 Mar 06 '13 at 19:26
  • That's true. I often use `Thread` though when I want to be able to directly interrupt a running method from separate thread. Otherwise, you have to keep track of the enclosing thread or use a `Future`. – Andrew Mao Mar 06 '13 at 19:28
  • Good point. But if your interruption logic gets very complex definitely consider Executors etc... – user949300 Mar 06 '13 at 19:32
1

Remember that classes are suggested to be named beginning with uppercase and do not forget to format your code please, use an IDE like Eclipse or Netbeans.

If you want to know when a thread has finished running (in this case your car), just add a println after the while ends, when you're out of while it means you achieved to complete all the distance.

public class Lamborgini extends Thread {

    public void run() {
        int distance = 1000;
        int steps = 0;
        int velocity = 45;
        int acelerationTime = 800;
        while (steps < distance) {
            System.out.println("Lamborgini running");
            steps += velocity;
            try {
                Thread.sleep(acelerationTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Lamborgini finished the race");
    }
}

public class Ferrari implements Runnable {
    @Override
    public void run() {
        int distance = 1000;
        int steps = 0;
        int velocity = 130;
        int acelerationTime = 950;
        while (steps < distance) {
            System.out.println("Ferrari running");
            steps += velocity;
            try {
                Thread.sleep(acelerationTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        System.out.println("Ferrari finished the race");
    }
}

public static class RaceMain {
    public static void main(String[] args) {
        Lamborgini lamborgini = new Lamborgini();
        lamborgini.start();

        Thread ferrari = new Thread(new Ferrari());
        ferrari.start();
    }
}

The right way if you want to start your Ferrari is to create a new thread and set as a parameter your Ferrari which is not a thread because it is a runnable.

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
0
lamborgini a = new lamborgini();
 lamborgini.start();

In this code block, you are starting the run() method inside lamborgini (which should really start with a capital letter) asynchronously. That is, it will run parallel to the thread that started it.

 ferrari b = new ferrari();
 ferrari.run();

This will not start a new thread; it will simply jump to the run() method and will stop all program execution until the run() method is finished.

christopher
  • 26,815
  • 5
  • 55
  • 89
0

Implementing the Runnable interface is the prefered way. You are here concentrating to run something not to change the thread behaviour.

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
0

First of all please use class name conventions for your classes (Class names start with capital letters)

To answer your question. When you're extending Thread, you just invoke the start() method on the object, because that object inherits the methods from Thread, therefore that object is a Thread itself.

When you're using Runnable, you don't call the run() method on the object that implements it. Instead what you should do is pass the object that implements Runnable to a new Thread object. For example:

ferrari b = new ferrari();
Thread t = new Thread(b);
t.start();

This way the run() method from the object that implements Runnable will execute.

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
0

Here are some thoughts about the differences about the class Thread and the interface Runnable:

1) In Java a class can not inherit one class meaning you can only extend one class. So with you extend Thread class there'll be no more inheritance for you.

2) If you are not adding new functionalities to Thread use Runnable.

3) Runnable represents a task and can be executed either by a Thread or a Executors.

4) Thread can't be restarted after finished runnable is reusable.

6) There is no need to inherit all from thread, if you only need to implement Run.

7)Extending Thread you will have a unique object associated to a single object while implementing Runnable it'll share the same object to multiple threads.

your threads starts in the order you set them, but there is no way to know when the operating system switches between the two threads.

emportella
  • 323
  • 4
  • 9
0

Sunilkumar from Vmoksha

IF you use thread we must extend only one class , we cant get the feature of other build in class so we tried avoid thread class and we go to runnable interface, if you use interface we can easily implement other class also