简体   繁体   English

为什么在多线程中run()方法不能是静态的?

[英]Why can't the the run() method be static in Multi threading?

Am new to multi threading programming, when I declare the run() method as static its giving the compiler error as 当我将run()方法声明为static时,它是多线程编程的新手,给编译器提供了以下错误:

"This static method cannot hide the instance method from Thread". “此静态方法无法从Thread隐藏实例方法”。

I didn't understand what that means, so please help me. 我不明白这意味着什么,所以请帮助我。

public class hello extends Thread {

    public static synchronized  void run() {
        for(int i=0;i<1000;i++)
            System.out.println(i);
    }

    public static void main(String[] args) {
        hello t1 = new hello();
        hello t2 = new hello();

        t1.start();
        t2.start();

    }
}

It is not necessarily relevent to multi-threaded programming, it is true of Java in general. 它不一定与多线程编程有关,通常对于Java都是如此。 If you have a class: 如果您上课:

public class MySuperclass {
  public void myMethod() {
    //do stuff
  }
}

You cannot then over-ride it to make it static: 然后,您不能覆盖它以使其变为静态:

public class MySubclass extends MySuperclass {
  public static void myMethod() {
    //do other stuff
  }
}

This is not allowed. 这是不允许的。 That's what the error message means. 这就是错误消息的含义。

public void run();

is a Method declared in class Thread . 是在Thread类中声明的Method。 As it's not static in Thread you cannot "change" it into a static method in your subclass. 由于它在Thread中不是静态的,因此您不能将其“更改”为子类中的静态方法。 And given your example, you don't even need to do that. 并以您的示例为例,您甚至不需要这样做。

If you need to have the code executed inside the Thread public and static and synchronized , I'd advise to refactor that part out: 如果您需要有代码的线程内执行publicstaticsynchronized ,我建议重构的那部分:

@Override
public void run() {
  staticRun();
}

public static synchronized void staticRun() {
  for(int i=0;i<1000;i++)
        System.out.println(i);
}

In the main() method you create two instances of Thread - t1 and t2 and then you call start() on them and that is correct you cannot run/start the class but an instance. 在main()方法中,创建线程的两个实例-t1和t2,然后在它们上调用start(),这是正确的,您不能运行/启动该类,而是一个实例。 That is why run() method is not supposed to be static. 这就是为什么run()方法不应该是静态的。 It needs an object (a Thread object) to be executed. 它需要一个对象(一个Thread对象)来执行。 Just remove static from your declaration and it shall be almost fine. 只需从声明中删除static,就可以了。 Other thing is that you dont need to make your run method synchronized (it is counter productive - you use thread to execute in paralel and ynchronize only on specific parts never whole run method). 另一件事是,您不需要使run方法同步(这会产生反作用-您使用线程在并行中执行并且仅在特定部分上同步,而不是整个run方法同步)。

The instance method run() is already available in hello class due to inheritance . 由于继承 ,实例方法run()在hello类中已经可用 You are trying to create another method (static) with same name which is run(). 您正在尝试创建另一个名称与run()相同的方法(静态)。

If the method is not static, it will automatically override the implementation and hide the method instance of parent class.. 如果方法不是静态的,它将自动覆盖实现并隐藏父类的方法实例。

So, is the Error.. Simple! 所以,是错误。。简单!

Because java is Object Oriented language and anything is java is object. 因为Java是面向对象的语言,而Java就是对象。 And task which you are trying to perform in multiple threads is object too and you should create this object. 而您试图在多个线程中执行的任务也是对象,因此您应该创建该对象。 When object is created you should call start() method which is defined on Thread class. 创建对象时,应调用在Thread类上定义的start()方法。 So to implement your own logic, you should override this behavior by implementing run() method in Hello subclass. 因此,要实现自己的逻辑,应通过在Hello子类中实现run()方法来覆盖此行为。

  • Run method defines logic which should be performed in other thread (note, that run method doesn't create new thread) Run方法定义应在其他线程中执行的逻辑(请注意,该run方法不会创建新线程)
  • Start method tells java to create new native thread and perform your run method in this new thread Start方法告诉Java创建新的本机线程并在此新线程中执行run方法

Try: 尝试:

public class Hello extends Thread{

    public void run()
    {
       for(int i=0;i<1000;i++)
       System.out.println(i);
    }
}

public class Main
{
    public static void main(String[] args)
    {
          Hello hello t1 = new hello();
          Hello hello t2 = new hello();
          t1.start();
          t2.start();
    }
}

The thread Class has a run method, therefore if you define it again in the hello class then you are overriding it, (direct inheritance since Hello extends Thread ) now, it makes no sense to turn a Object method into a Class method, (run method belongs to an object and not to the class) that is the reason why your compiler is complaining with the message: thread Class具有一个run方法,因此,如果您在hello class再次定义它,则您现在将其覆盖(由于Hello extends Thread直接继承),所以将Object方法转换为Class方法是没有意义的(run方法属于一个对象而不属于该类),这就是您的编译器抱怨该消息的原因:

"...you can not This static method cannot hide the instance method from Thread". “ ...您无法使用此静态方法无法从Thread隐藏实例方法”。

in other words, you are violating inheritance rules with it. 换句话说,您违反了继承规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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