[英]Thread.start() doesn't initialize run()
I have some problems with my code. 我的代码有问题。 For some reason my thread.start() doesn't activate my run() method. 由于某种原因,我的thread.start()无法激活我的run()方法。 In pure desperation I have simply replaced my code in run() with a printing function, but nothing is being printed. 在纯粹的绝望中,我只是用打印函数替换了run()中的代码,但是什么也没打印。 Can someone help me by explaining what is wrong in my code? 有人可以通过解释代码中的错误来帮助我吗?
public class Screen extends JPanel implements Runnable{
Thread thread = new Thread();
Frame frame;
public Screen(Frame frame){
this.frame = frame;
frame.setSize(horizontal * 25 + 24 , (vertical) * 25 + 48);
this.frame.addKeyListener(new KeyHandler(this));
thread.start();
}
public void run(){
System.out.println("Boom");
}
}
I got a lot of stuff in between and under this code, but this is the only part essential to the thread and frame. 在这段代码之间以及在这段代码下,我都有很多东西,但这是线程和框架必不可少的部分。
You must pass the Thread
a Runnable
. 您必须将Thread
传递给Runnable
。 Since thread
is an instance variable and the class implements Runnable
I guess you want to do this: 由于thread
是一个实例变量,并且该类实现了Runnable
我想您想这样做:
Thread thread = new Thread(this);
But be careful when calling overridable methods from a constructor and be more careful if these methods are called by a separate thread that runs the code in parralel to the constructor initialization. 但是,从构造函数调用可重写方法时要小心,如果由在构造函数初始化时并行运行代码的单独线程调用这些方法,则要格外小心。 It might run while the constructor still initializes the object. 它可能在构造函数仍在初始化对象时运行。 Think about what will happen if you subclass Screen
, override the run
method and the run method accesses properties of the superclass Screen
while it is initializing. 想想如果您继承Screen
子类,重写run
方法,并且run方法在初始化时访问超类Screen
属性,将会发生什么情况。
Also see What's wrong with overridable method calls in constructors? 另请参阅构造函数中的可重写方法调用出了什么问题?
This is because your thread doesn't know about the run method. 这是因为您的线程不知道run方法。 You can do it by changing 您可以通过更改
Thread thread = new Thread();
to 至
Thread thread = new Thread(this);
because your class is an instance of Runnable. 因为您的类是Runnable的实例。
ps try to avoid messing with threads and Swing. ps尽量避免弄乱线程和Swing。 Use SwingWorkers if you really have to. 如果确实需要,请使用SwingWorkers。
Thread needs a Runnable
instance, which has run()
method to call. 线程需要一个Runnable
实例,该实例具有run()
方法来调用。 But you are not providing Runnable
instance to Thread
. 但是您没有向Thread
提供Runnable
实例。
Do Thread t = new Thread(this);
做Thread t = new Thread(this);
You are creating a simple thread. 您正在创建一个简单的线程。
Thread thread = new Thread();
It does call run()
method but of class Thread
and not your Screen class runnable implementation. 它会调用run()
方法,但会调用Thread
类,而不是Screen类的可运行实现。
You can do 你可以做
Thread thread = new Thread(new Screen());
You have two solutions: 您有两种解决方案:
1) Using the override run
method in your class 1)在您的课程中使用替代run
方法
Thread thread = new Thread(this);
2) Using new Runnable
2)使用new Runnable
Thread th = new Thread(new Runnable() {
@Override
public void run() {
// your code
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.