Can I have multiple run methods in a class?

前端 未结 5 719
眼角桃花
眼角桃花 2021-01-03 03:21

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.

相关标签:
5条回答
  • 2021-01-03 03:58
    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.

    0 讨论(0)
  • 2021-01-03 03:59

    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
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-03 04:02

    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!
    }
    
    0 讨论(0)
  • 2021-01-03 04:18

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

    0 讨论(0)
  • 2021-01-03 04:22

    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.

    0 讨论(0)
提交回复
热议问题