简体   繁体   中英

Can I have multiple run methods in a class?

If so, how? I need to have 2 run methods in each object I will instantiate.

I meant a run method for threading.

What i need is more like a race of two cars. each car should have a name and a run() method in a class which extends Thread. I will have to call on the two cars in my main to see which will win. The first or the second. I need help in starting my program

A class can't contain two methods with the same signature . The signature of a method is its name followed by its arguments.

You may thus have run(int) and run(String) and run() in the same class. This is called method overloading . You can't have two run() methods, because you (and the compiler) could not know which one to execute when calling obj.run() .

Thread t1 = new Thread(new Runnable(){public void run(){...}})
Thread t2 = new Thread(new Runnable(){public void run(){....}})
t1.start();
t2.start();

By using inner class you can achieve multiple run methods in a single class.

What for? I assume you're talking about implementing the runnable interface, so you want two methods with the signature: public void run(); ?

Having two methods with the same signature makes no sense; how would you distinguish between them when calling the method from elsewhere in your code?

If you want two different things to happen based on a certain condition when run() is invoked, then you need to add a conditional statement at the start of the method:

public void run() {

    if (some_condition) {

        // code for the first scenario

    } else {

        // code for the second

    }

}

Try:

 public class Car extends Thread
 {
      public String name;

      public Car(String name)
      {
           this.name = name;
      }
      public void run()
      {
           //your code
      }
 }

 public static void main(String[] args)
 {
      Thread car1 = new Car("car1's name");
      Thread car2 = new Car("car2's name");
      car1.start();
      car2.start();

 }

You can add your logic into Car.run(), but basically that's how I would do that. In your logic determine when it starts and ends. When both threads finish, you can see which one is the winner. You could also add a while loop and have a field in Car that is a boolean type to determine if it's finished or not.

As you asked, you want to know if it's possible to have more than one run() method in a class that implements the Runnable interface, or extends the Thread class.

You can just think about the definition of a thread. A thread is a single instructions block, contained inside a process, that's why a thread is called as a lightweight process . Generally, a thread executes only a specific task.

A new thread is created when the main process of a program needs to do multiple operations in background, such as autosaving or grammar check, if the main program is a word processor, for example. This is the concept of multithreading .

Now, if you read the Runnable or Thread API, you can see that the class that extends Thread class or implements the Runnable class, overrides (in case you are extending Thread class) the run() method or implements the abstract method run() (in case you are implementing the Runnable class).

As other users said, you can use the overloading technique. You can implement another run() method in the class, with a different signature, because it's not a legal practice to have multiple methods with the same signature (it generates confusion for the compiler when you have to invoking one of them) and because it's non sense.

But, remember, at the moment of the starting of the thread, using, for example Thread.start() , then the method will invoke always the run() method you defined previously, with the empty signature. It's possible only to invoke another run method, with a different signature as run(String s) , in the main run() method of thread.

For example:

public class Thread implements Runnable{

   public void run(){
         //instructions
         Object o = new Object();
         run(o);
    }

   public void run(Object o){
       //other instructions
    }
} 


public class OtherClass{
    Thread t = new Thread();
    t.start(); //this will invoke the main run() method!
}

you can create two classes and call start method(basic idia) look at the example! //class Second

public class Second extends Thread {
public void run() {
for (int c = 100; c > 0; c--) {
    System.out.println(c);
  }
 }
}

//class Main

public class Main extends Thread {
public void run() {
for (int i = 0; i < 100; i++) {
    System.out.println(i);
 }

}

public static void main(String[] args) throws InterruptedException {
Main thread_1 = new Main();
Second thread_2 = new Second();

// Let's fix concurrency problem
// method 1:

thread_1.start();
thread_1.join();
thread_2.start();
thread_2.join();

//method 2:
//or you can use  is alive
/* thread_1.start();
while (thread_1.isAlive()){
    System.out.print("");
}thread_2.start();*/


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM