我需要一个“接受参数的可运行对象”,尽管我知道这样的可运行对象实际上并不存在。

这可能是我的应用程序设计中的根本缺陷,或者是我疲惫的大脑中的心理障碍,所以我希望在这里找到一些关于如何在不违反基本OO原则的情况下完成以下内容的建议:

  private Runnable mOneShotTask = new Runnable(String str) {
    public void run(String str) {
       someFunc(str);
    }
  };  

你知道如何完成上面的事情吗?


当前回答

Well it's been almost 9 years since I originally posted this and to be honest, Java has made a couple improvements since then. I'll leave my original answer below, but there's no need for people to do what is in it. 9 years ago, during code review I would have questioned why they did it and maybe approved it, maybe not. With modern lambdas available, it's irresponsible to have such a highly voted answer recommending an antiquated approach (that, in all fairness, was dubious to begin with...) In modern Java, that code review would be immediately rejected, and this would be suggested:

void foo(final String str) {
    Thread t = new Thread(() -> someFunc(str));
    t.start();
}

像以前一样,以一种有意义的方式处理线程之类的细节留给读者练习。但是坦率地说,如果您害怕使用lambdas,那么您应该更害怕多线程系统。

原来的答案,只是因为:

你可以在方法中声明一个类

void Foo(String str) {
    class OneShotTask implements Runnable {
        String str;
        OneShotTask(String s) { str = s; }
        public void run() {
            someFunc(str);
        }
    }
    Thread t = new Thread(new OneShotTask(str));
    t.start();
}

其他回答

我首先想知道你在这里要完成什么,需要一个参数传递给new Runnable()或run()。 通常的方法应该是有一个Runnable对象,它通过在启动前设置成员变量将数据(str)传递给它的线程。run()方法然后使用这些成员变量值执行someFunc()

你可以把它放到一个函数里。

String paramStr = "a parameter";
Runnable myRunnable = createRunnable(paramStr);

private Runnable createRunnable(final String paramStr){

    Runnable aRunnable = new Runnable(){
        public void run(){
            someFunc(paramStr);
        }
    };

    return aRunnable;

}

(当我使用这个时,我的参数是一个整数ID,我用它来创建ID的hashmap——> myRunnables。这样,我可以使用hashmap在处理程序中发布/删除不同的myRunnable对象。)

你有两个选择:

定义一个命名类。将参数传递给命名类的构造函数。 让你的匿名类关闭你的“参数”。一定要把它标记为期末考试。

目前为止最好的方法:

Consumer<String> oneShot = str -> {
    
   somefunc(str);
    
};

oneShot.accept("myString");

Well it's been almost 9 years since I originally posted this and to be honest, Java has made a couple improvements since then. I'll leave my original answer below, but there's no need for people to do what is in it. 9 years ago, during code review I would have questioned why they did it and maybe approved it, maybe not. With modern lambdas available, it's irresponsible to have such a highly voted answer recommending an antiquated approach (that, in all fairness, was dubious to begin with...) In modern Java, that code review would be immediately rejected, and this would be suggested:

void foo(final String str) {
    Thread t = new Thread(() -> someFunc(str));
    t.start();
}

像以前一样,以一种有意义的方式处理线程之类的细节留给读者练习。但是坦率地说,如果您害怕使用lambdas,那么您应该更害怕多线程系统。

原来的答案,只是因为:

你可以在方法中声明一个类

void Foo(String str) {
    class OneShotTask implements Runnable {
        String str;
        OneShotTask(String s) { str = s; }
        public void run() {
            someFunc(str);
        }
    }
    Thread t = new Thread(new OneShotTask(str));
    t.start();
}