[英]How to run a class' method using thread
If I do the following, I will be able to create an object as a thread and run it. 如果执行以下操作,则可以将对象创建为线程并运行它。
class ThreadTest
{
public static voic main(String[] args)
{
HelloThread hello = new HelloThread();
Thread t = new Thread(hello);
t.start();
}
}
class HelloThread extends Thread
{
public void run()
{
System.out.println(" Hello ");
}
}
Now, if my HelloThread
class has a another method call runThisPlease()
, how are we supposed to run it with a thread? 现在,如果我的
HelloThread
类有另一个方法调用runThisPlease()
,我们应该如何用线程运行它?
Example: 例:
class HelloThread extends Thread
{
public void run()
{
System.out.println(" Hello ");
}
public void runThisPlease(String input)
{
System.out.println (" Using thread on another class method: " + input );
}
}
Que: When I try Thread t = new Thread(hello.runThisPlease());
Thread t = new Thread(hello.runThisPlease());
:当我尝试Thread t = new Thread(hello.runThisPlease());
, it doesn't work. ,它不起作用。 So how can we call the method
runThisPlease()
using a thread? 那么如何使用线程调用方法
runThisPlease()
?
Edit: Argument needed in method for runThisPlease()
; 编辑:
runThisPlease()
方法中需要的参数;
In java 8 you can use 在Java 8中,您可以使用
Thread t = new Thread(hello::runThisPlease);
hello::runThisPlease
will be converted to a Runnable
with a run method that calls hello.runThisPlease();
hello::runThisPlease
将通过调用hello.runThisPlease();
的run方法转换为Runnable
hello.runThisPlease();
. 。
If your want to call a method, that needs parameters, eg System.out.println
, you can of course use a normal lambda expression too: 如果要调用需要参数的方法,例如
System.out.println
,则当然也可以使用普通的lambda表达式:
final String parameter = "hello world";
Thread t = new Thread(() -> System.out.println(parameter));
If you use a java version < 8, you can of course replace the method reference / lambda expression with anonymus inner classes that extend Runnable
(which is what a java8 compiler does, AFAIK), see other answers. 如果使用的Java版本<8,则当然可以将方法reference / lambda表达式替换为可扩展
Runnable
匿名内部类(这是Java8编译器执行的操作,即AFAIK),请参见其他答案。
However you can also use a anonymus inner class that extends Thread
: 但是,您也可以使用扩展
Thread
的匿名内部类:
final HelloThread hello = //...
Thread t = new Thread() {
@Override
public void run() {
hello.runThisPlease();
}
};
Simply calling the runThisPlease()
from within the run()
method will make it part of a new thread. 只需从
run()
方法中调用runThisPlease()
,它将使其成为新线程的一部分。
Try this: 尝试这个:
class HelloThread extends Thread
{
public void run()
{
System.out.println(" Hello .. Invoking runThisPlease()");
runThisPlease();
}
public void runThisPlease()
{
System.out.println (" Using thread on another class method ");
}
}
You have to only call this method inside run()
method. 您只需要在
run()
方法内调用此方法。
public void run(){
System.out.println(" Hello ");
runThisPlease();
}
If you want to pass some argument then you can use below code 如果您想传递一些参数,则可以使用以下代码
String str = "Welcome";
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println(str);
}});
Things are maybe more clear if you use the Runnable interface: 如果使用Runnable接口,情况可能会更加清楚:
public class HelloThread implements Runnable
{
@Override
public void run() {
// executed when the thread is started
runThisPlease();
}
public void runThisPlease() {...}
}
To launch this call: 要发起此呼叫:
Thread t=new Thread(new HelloThread());
t.start();
The Thread class can not see your extra method because it is not part of the Runnable interface. Thread类看不到您的额外方法,因为它不是Runnable接口的一部分。 As a convenience Thread implements Runnable but I don't think it helps in clarity :(
为了方便起见,线程实现了Runnable,但我认为它没有帮助:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.