java中的线程和public void run()方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19528267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread and public void run() method in java
提问by
public Thread thread = new Thread();
public void start() {
running = true;
thread.start();
}
public void run() {
while(running) {
System.out.println("test");
try {
thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
My problem is that the program will not print out "test" nor will it seem to loop despite 'running' being true. Is there a way I can continuously loop in the run method?
我的问题是该程序不会打印出“测试”,也不会在“运行”为真的情况下似乎循环。有没有办法可以在 run 方法中连续循环?
回答by Sotirios Delimanolis
You haven't actually asked run()
to be called. All you've done is declare a run()
method unrelated to the Thread
.
你实际上并没有要求run()
被叫。您所做的只是声明一个run()
与Thread
.
Put your run()
method in a Runnable
and pass that to the Thread
.
将您的run()
方法放入 aRunnable
并将其传递给Thread
.
public Thread thread = new Thread(new Runnable() {
public void run() {
while (running) {
System.out.println("test");
try {
thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
回答by rgettman
The problem appears to be that you aren't running the run
method that you think you're running in the thread.
问题似乎是您没有运行run
您认为在线程中运行的方法。
First, you've created a Thread
called thread
. In your class's start
method, you set running
to true
and call thread.start()
. But that just calls Thread
's run()
method, which does nothing.
首先,您已经创建了一个Thread
名为thread
. 在您的类的start
方法中,您设置running
为true
并调用thread.start()
. 但这只是调用Thread
的run()
方法,它什么也不做。
public void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
公共无效运行()
如果该线程是使用单独的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,此方法不执行任何操作并返回。
You aren't calling your own run
method.
你不是在调用你自己的run
方法。
You have created a run
method. I can't see your class definition here, but I'm assuming that your class implements Runnable
. You need to send an instance of your class as an argument to the Thread
, by using the Thread
constructor that takes a Runnable
. Then the Thread
will know to run your Runnable
's run()
method.
您已经创建了一个run
方法。我在这里看不到你的类定义,但我假设你的类实现了Runnable
. 您需要Thread
使用Thread
带有Runnable
. 然后Thread
就会知道运行你Runnable
的run()
方法。
回答by Sage
Well you need to call start()
to start the thread. Otherwise neither running
will be true
nor thread.start()
get executed. Well i can guess you were intended to do something like this:
那么你需要调用start()
来启动线程。否则既running
不会是真的,也不会thread.start()
被执行。好吧,我猜你打算做这样的事情:
class MyTask implements Runnable
{
boolean running = false;
public void start() {
running = true;
new Thread(this).start();
}
public void run() {
while(running) {
System.out.println("test");
try {
Thread.sleep(1000);
// you were doing thread.sleep()! sleep is a static function
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
new MyTask().start();
}
}