有人能告诉我如何将参数传递给线程吗?

另外,匿名类是如何工作的?


当前回答

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();
}

其他回答

首先,我想指出其他答案都是正确的。 但是,在构造函数中使用形参可能对所有人来说都不是最好的主意。

在很多情况下,你会想要使用“匿名内部类”,并重写run()方法,因为为每个用途定义特定的类是很痛苦的。 (新MyRunnable(){…})

在创建Runnable时,参数可能无法在构造函数中传递。例如,如果你将这个对象传递给一个方法,这个方法将在单独的线程中执行一些工作,然后调用你的runnable,将该工作的结果应用到它。

在这种情况下,使用如下方法: public MyRunnable withParameter(对象参数),可能是更有用的选择。

我并不是说这是解决问题的最好办法,但它可以解决问题。

您可以扩展Thread类或Runnable类,并根据需要提供参数。文档中有一些简单的例子。我将把它们移植到这里:

 class PrimeThread extends Thread {
     long minPrime;
     PrimeThread(long minPrime) {
         this.minPrime = minPrime;
     }

     public void run() {
         // compute primes larger than minPrime
          . . .
     }
 }

 PrimeThread p = new PrimeThread(143);
 p.start();

 class PrimeRun implements Runnable {
     long minPrime;
     PrimeRun(long minPrime) {
         this.minPrime = minPrime;
     }

     public void run() {
         // compute primes larger than minPrime
          . . .
     }
 }


 PrimeRun p = new PrimeRun(143);
 new Thread(p).start();

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();
}

在Java 8中,你可以在并发API和ExecutorService中使用lambda表达式,作为直接使用线程的高级替代:

创建一个用于创建新线程的线程池 根据需要,但将重用之前构造的线程 可用。这些池通常会提高执行许多短期异步任务的程序的性能。

    private static final ExecutorService executor = Executors.newCachedThreadPool();

    executor.submit(() -> {
        myFunction(myParam1, myParam2);
    });

参见executors javadocs。

创建线程时,需要一个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");

当然,如果您希望在参数更改时触发一个操作,则必须使用锁,这使事情变得相当复杂。