如何向java线程中传参

走着路睡觉
  • java
小于 1 分钟

通过构造器传参

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();
上次编辑于:
贡献者: zhaojingbo
Loading...