【问题标题】:Passing methods (or voids) to Java ArrayList将方法(或 voids)传递给 Java ArrayList
【发布时间】:2019-12-30 18:41:00
【问题描述】:

我想知道是否有任何方法可以将“void”对象传递给 Java ArrayList。

例如,我想通过将 void 对象“public void doThis()”引用为 list1.put(doThis()); 将其传递给数组列表;

将方法的引用传递给数组列表对象时,我得到以下信息:

The method add(Object) in the type ArrayList<Object> is not applicable for the arguments (void)

我正在尝试创建一个类,它可以获取对其他类中方法的引用的 ArrayList 并迭代以执行线程中的方法。


package asWebRest.shared;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ThreadRipper {

    private int maxThreads = 4;
    private int maxThreadsHost = 8;


    public void runProcess(String pString) {
        System.out.println(" --> Running [ "+pString+" ]");
        String s = null;
        String[] pArray = { "bash", "-c", pString };
        try { 
            Process p = new ProcessBuilder(pArray).start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((s = stdInput.readLine()) != null) { System.out.println(s); }
            while ((s = stdError.readLine()) != null) { System.out.println(s); }
            p.destroy();
        }
        catch (IOException e) { e.printStackTrace(); }
        System.out.flush();
    }

    public String runProcesses() {

        boolean pool = false;
        WebCommon wc = new WebCommon();
        ArrayList<Object> threadList = new ArrayList<Object>();
        threadList.add(runProcess("ls -l"));
        threadList.add(runProcess("ls -s"));
        threadList.add(runProcess("ls -al"));

        String runResults = "";
        int procLoop = 0;
        int inPool = 0;

        for(int i = 0; i < threadList.size(); i++) {            
            Object exTask = threadList.get(i);  
            if(!pool) {
                if(inPool < maxThreads) {
                    runResults += "Loop [" + procLoop + "] Thread [" + inPool + "]: " + exTask + "\n";
                    inPool++;
                } else {
                    procLoop++;
                    inPool = 0;
                }
            } else {
                runResults += "Pool Thread [" + inPool + "]: " + exTask + "\n";
                inPool++;
            }       
        }

        return runResults;

    }

    public int getMaxThreads() { return maxThreads; }
    public int getMaxThreadsHost() { return maxThreadsHost; }

}

【问题讨论】:

  • 在添加 void 方法的结果后,您希望 ArrayList 包含什么?
  • 我想您想保留要等待的进程列表,那么您为什么不返回 Process 实例,而不是销毁它,并且 - 最后 - 等待所有进程终止?
  • 我想这样做,所以我有灵活性也可以线程出并调用不使用 Process 实例的不同方法。
  • 好的,然后回到我的第一个问题:假设你调试你的程序,检查你添加了你的 void 返回的虚构 ArrayList 的内容:会出现什么?

标签: java tomcat


【解决方案1】:

您正在做的是添加runProcess("ls -l") 的返回而不是对runProcess 的引用,并且由于runProcess 不返回任何内容,Java 会显示一个错误,您无法将void 添加到列表中。

您需要使用Runnables 的列表。

List&lt;Runnable&gt; processes = new ArrayList&lt;Runnable&gt;();

以这种方式将其添加到列表中,

processes.add(()-&gt;runProcess("ls -l"));

然后你可以对进程列表做任何你选择的事情,

for(Runnable process : processes) { 
   process.run();// You can other fancy stuff here.
   new Thread(process).start(); // eg : spawn in new thread.
}

【讨论】:

    【解决方案2】:

    为什么不尝试使用 java.lang.reflect.Method 和所有相关信息并将其传递给您的列表?

    例如,如果您想执行一个知道它所属的类、名称和实例(如果需要)、参数以及返回类型的方法,您可以这样做:

    Method myMethod = myClass.class.getMethod(methodName, args...);
    Object[] args = myArgs... //fill with the relevant data
    

    设置 DTO 并添加到您的数组中。

    然后当你想执行它时

    myMethod .invoke(instance, args)
    

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 2020-06-20
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 2014-04-27
      • 2016-11-01
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多