用参数运行吗?

时间:2011-05-02 03:09:38

标签: java runnable

我需要一个“接受参数的Runnable”,虽然我知道这样的runnable并不存在。

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

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

知道如何完成上述内容吗?

8 个答案:

答案 0 :(得分:209)

您可以在方法

中声明一个类
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();
}

答案 1 :(得分:40)

你可以把它放在一个函数中。

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的哈希图 - > myRunnables。这样,我可以使用hashmap在处理程序中发布/删除不同的myRunnable对象。 )

答案 2 :(得分:29)

theView.post(new Runnable() {
    String str;
    @Override                            
    public void run() {
        par.Log(str);                              
    }
    public Runnable init(String pstr) {
        this.str=pstr;
        return(this);
    }
}.init(str));

创建init函数,返回对象本身并用它初始化参数。

答案 3 :(得分:11)

我使用以下实现Runnable接口的类。使用此类,您可以轻松地创建带参数的新线程

public abstract class RunnableArg implements Runnable {

    Object[] m_args;

    public RunnableArg() {
    }

    public void run(Object... args) {
        setArgs(args);
        run();
    }

    public void setArgs(Object... args) {
        m_args = args;
    }

    public int getArgCount() {
        return m_args == null ? 0 : m_args.length;
    }

    public Object[] getArgs() {
        return m_args;
    }
}

答案 4 :(得分:10)

您有两种选择:

  1. 定义命名类。将您的参数传递给指定类的构造函数。

  2. 让您的匿名课程关闭“参数”。请务必将其标记为final

答案 5 :(得分:7)

从Java 8开始,最好的答案是使用void doSomething(Consumer<String> something) { something.accept("hello!"); } ... doSomething( (something) -> System.out.println(something) ) ...

Paginator methods

这是功能性接口之一,这意味着您可以将其称为lambda表达式:

[{
        "title": "Dashboard",
        "url": "#",
        "children": [],
        "roles": [
            "admin",
            "user",
        ],
    },
    {
        "title": "Admin",
        "url": "/",
        "children": [
            {
                "title": "Users",
                "url": "users",
                "children": [],
                "roles": [
                    "admin"
                ],
            },
            {
                "title": "Students",
                "children": [],
                "roles": [
                    "user",
                    "root",
                ],
            },
            {
                "title": "Sites",
                "url": "sites",
                "children": [],
                "roles": [
                    "admin",
                    "customer admin",
                ],
            },
            {
                "title": "Menu",
                "children": [],
                "roles": [
                    "admin"
                ],

            },
            {
                "title": "Roles",
                "children": [],
                "roles": [
                    "admin",
                    "root"
                ],
            },
            {
                "title": "Permissions",
                "children": [],
                "roles": [
                    "admin",
                    "root"
                ],
            },
            {
                "title": "Logs",
                "children": [],
                "roles": [
                    "admin",
                    "user",
                ],
            }
        ],
        "roles": [
            "customer admin",
            "admin",

        ],
    },
    {
        "title": "Studio",
        "children": [
            {
                "title": "Discovery to Design",
                "children": [],
                "roles": [
                    "admin",
                    "user"
                ],
            },
            {
                "title": "Build",
                "children": [
                    {
                        "title": "Data",
                        "children": [],
                        "roles": [
                            "admin",
                            "user",

                        ],
                    },
                    {
                        "title": "Content",
                        "children": [],
                        "roles": [
                            "admin",
                            "customer admin"
                        ],
                    },
                    {
                        "title": "Packages",
                        "children": [],
                        "roles": [
                            "admin",
                            "user"
                        ],
                    }
                ],
                "roles": [
                    "admin",
                    "user",
                    "root"
                ],
            },
            {
                "title": "Publish",
                "children": [],
                "roles": [
                    "admin",
                    "user"
                ],
            }
        ],
        "roles": [
            "admin",
            "user"
        ],
    },
    {
        "title": "Base",
        "children": [],
        "roles": [
            "admin",
            "user",
            "root"
        ],
    },
    {
        "title": "About Us",
        "children": [],
        "roles": [
            "user",
            "admin",
        ],
    },
    {
        "title": "Monitoring",
        "children": [],
        "roles": [
            "user"
        ],
    }]

答案 6 :(得分:5)

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

答案 7 :(得分:0)

/**
 * @author AbdelWadoud Rasmi
 * <p>
 * The goal of this class is to pass some parameters to a runnable instance, a good example is
 * after caching a file you need to pass the new path to user to do some work on it.
 */
public abstract class ParameterizedRunnable implements Runnable {
    private Object[] params;

    /**
     * @param params: parameters you want to pass the the runnable.
     */
    public ParameterizedRunnable(Object... params) {
        this.params = params;
    }

    /**
     * Code you want to run
     *
     * @param params:parameters you want to pass the the runnable.
     */
    protected abstract void run(Object... params);

    @Override
    public final void run() {
        run(params);
    }

    /**
     * setting params
     */
    public void setParams(Object... params) {
        this.params = params;
    }

    /**
     * getting params
     */
    public Object[] getParams() {
        return params;
    }
}