如何向java线程中传参
通过构造器传参
public class MyRunnable implements Runnable {
public MyRunnable(Object parameter) {
// store parameter for later user
}
public void run() {
}
}
Runnable r = new MyRunnable(param_value);
new Thread(r).start();
匿名类
final X parameter = ...; // the final is important
Thread t = new Thread(new Runnable() {
p = parameter;
public void run() {
...
};
t.start();
Loading...