有人能告诉我如何将参数传递给线程吗?
另外,匿名类是如何工作的?
有人能告诉我如何将参数传递给线程吗?
另外,匿名类是如何工作的?
当前回答
对于匿名类:
在这里回答问题编辑是如何工作的匿名类
final X parameter = ...; // the final is important
Thread t = new Thread(new Runnable() {
p = parameter;
public void run() {
...
};
t.start();
命名的类:
您有一个扩展Thread(或实现Runnable)的类和一个带有您希望传递的参数的构造函数。然后,当你创建新线程时,你必须传入参数,然后启动线程,就像这样:
Thread t = new MyThread(args...);
t.start();
Runnable是一个比Thread更好的解决方案。所以我更喜欢:
public class MyRunnable implements Runnable {
private X parameter;
public MyRunnable(X parameter) {
this.parameter = parameter;
}
public void run() {
}
}
Thread t = new Thread(new MyRunnable(parameter));
t.start();
这个答案基本上与这个类似的问题相同:如何将参数传递给Thread对象
其他回答
Android专用
为了回调的目的,我通常使用输入参数实现自己的泛型Runnable:
public interface Runnable<TResult> {
void run(TResult result);
}
用法很简单:
myManager.doCallbackOperation(new Runnable<MyResult>() {
@Override
public void run(MyResult result) {
// do something with the result
}
});
在经理:
public void doCallbackOperation(Runnable<MyResult> runnable) {
new AsyncTask<Void, Void, MyResult>() {
@Override
protected MyResult doInBackground(Void... params) {
// do background operation
return new MyResult(); // return resulting object
}
@Override
protected void onPostExecute(MyResult result) {
// execute runnable passing the result when operation has finished
runnable.run(result);
}
}.execute();
}
您可以从Runnable派生一个类,并在构造过程中(例如)传入参数。
然后使用Thread启动它。开始(Runnable r);
如果你的意思是在线程运行时,那么只需在调用线程中保持对派生对象的引用,并调用适当的setter方法(在适当的地方同步)。
要么编写一个实现Runnable的类,并在适当定义的构造函数中传递所需的任何内容,要么编写一个使用适当定义的构造函数来扩展Thread的类,该构造函数使用适当的参数调用super()。
创建线程时,需要一个Runnable实例。传递形参最简单的方法是将它作为参数传递给构造函数:
public class MyRunnable implements Runnable {
private volatile String myParam;
public MyRunnable(String myParam){
this.myParam = myParam;
...
}
public void run(){
// do something with myParam here
...
}
}
MyRunnable myRunnable = new myRunnable("Hello World");
new Thread(myRunnable).start();
如果你想在线程运行时改变参数,你可以简单地给你的runnable类添加一个setter方法:
public void setMyParam(String value){
this.myParam = value;
}
一旦你有了这个,你可以通过像这样调用来改变参数的值:
myRunnable.setMyParam("Goodbye World");
当然,如果您希望在参数更改时触发一个操作,则必须使用锁,这使事情变得相当复杂。
这个答案来得很晚,但也许有人会觉得它有用。它是关于如何在不声明命名类的情况下将参数传递给Runnable(对于内联程序来说很方便):
String someValue = "Just a demo, really...";
new Thread(new Runnable() {
private String myParam;
public Runnable init(String myParam) {
this.myParam = myParam;
return this;
}
@Override
public void run() {
System.out.println("This is called from another thread.");
System.out.println(this.myParam);
}
}.init(someValue)).start();
当然,您可以将start的执行推迟到某个更方便或合适的时刻。这取决于你init方法的签名(所以它可能需要更多和/或不同的参数),当然甚至它的名字,但基本上你得到了一个想法。
事实上,还有另一种将参数传递给匿名类的方法,即使用初始化块。考虑一下:
String someValue = "Another demo, no serious thing...";
int anotherValue = 42;
new Thread(new Runnable() {
private String myParam;
private int myOtherParam;
// instance initializer
{
this.myParam = someValue;
this.myOtherParam = anotherValue;
}
@Override
public void run() {
System.out.println("This comes from another thread.");
System.out.println(this.myParam + ", " + this.myOtherParam);
}
}).start();
所有都发生在初始化程序块内部。