如何在java中对方法进行排队

如何在java中对方法进行排队,java,Java,如何在Java中将方法及其参数添加到队列中? 例如: class Demo { int add(int x, int y) { return x*y; } // Add this method } 如果我们必须使用参数对该方法进行排队,我们如何才能做到这一点 i、 e 如果您使用的是Java 8,您可以创建一个IntSupplier队列,如下所示: Queue<IntSupplier> queue = // some new queue q

如何在Java中将方法及其参数添加到队列中? 例如:

class Demo {
    int add(int x, int y) {
        return x*y;
     }
    // Add this method
}
如果我们必须使用参数对该方法进行排队,我们如何才能做到这一点

i、 e


如果您使用的是Java 8,您可以创建一个
IntSupplier
队列,如下所示:

Queue<IntSupplier> queue = // some new queue
queue.add(() -> add(10, 20));
queue.add(() -> add(20, 30));

// The getAsInt-method calls the supplier and gets its value.
int result1 = queue.remove().getAsInt();
int result2 = queue.remove().getAsInt();
Queue Queue=//一些新队列
queue.add(()->add(10,20));
queue.add(()->add(20,30));
//getAsInt方法调用供应商并获取其值。
int result1=queue.remove().getAsInt();
int result2=queue.remove().getAsInt();

如果您使用的是Java 8,您可以创建一个
IntSupplier
队列,如下所示:

Queue<IntSupplier> queue = // some new queue
queue.add(() -> add(10, 20));
queue.add(() -> add(20, 30));

// The getAsInt-method calls the supplier and gets its value.
int result1 = queue.remove().getAsInt();
int result2 = queue.remove().getAsInt();
Queue Queue=//一些新队列
queue.add(()->add(10,20));
queue.add(()->add(20,30));
//getAsInt方法调用供应商并获取其值。
int result1=queue.remove().getAsInt();
int result2=queue.remove().getAsInt();
使用Java8:

        @Test
        public void test(){

            QueueMethod q1= ()->System.out.println("q1 hello");
            QueueMethod q2= ()->System.out.println("q2 hello");
            Queue<QueueMethod> queues=new  LinkedList<QueueMethod>();
            queues.add(q1);
            queues.add(q2);     
            queues.forEach(q->q.invoke());      
        }

        @FunctionalInterface
        interface QueueMethod{      
            void invoke();
        }


 Output: 

q1 hello
q2 hello
@测试
公开无效测试(){
QueueMethodQ1=()->System.out.println(“q1 hello”);
QueueMethod q2=()->System.out.println(“q2你好”);
Queue queues=new LinkedList();
添加(q1);
添加(q2);
queues.forEach(q->q.invoke());
}
@功能接口
接口队列方法{
void invoke();
}
输出:
你好
q2你好
使用Java8:

        @Test
        public void test(){

            QueueMethod q1= ()->System.out.println("q1 hello");
            QueueMethod q2= ()->System.out.println("q2 hello");
            Queue<QueueMethod> queues=new  LinkedList<QueueMethod>();
            queues.add(q1);
            queues.add(q2);     
            queues.forEach(q->q.invoke());      
        }

        @FunctionalInterface
        interface QueueMethod{      
            void invoke();
        }


 Output: 

q1 hello
q2 hello
@测试
公开无效测试(){
QueueMethodQ1=()->System.out.println(“q1 hello”);
QueueMethod q2=()->System.out.println(“q2你好”);
Queue queues=new LinkedList();
添加(q1);
添加(q2);
queues.forEach(q->q.invoke());
}
@功能接口
接口队列方法{
void invoke();
}
输出:
你好
q2你好

您可以通过以下方式使用反射:

public class Catalog {
  public void print(Integer x, Integer y){      
    System.out.println(x*y);
  }

}

public static void main(String[] args) {
    Catalog cat = new Catalog();
    Queue<Method> queueObject = new LinkedList<Method>();
    try {
        Method printMethod = cat.getClass().getDeclaredMethod("print", new Class[]{Integer.class,Integer.class});       

        //Now you can add and remove your methods from queues
        queueObject.add(printMethod);

        //invoke the just added method
        System.out.println(queueObject.element().invoke(cat,10,20));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}`
公共类目录{
公共无效打印(整数x,整数y){
系统输出打印ln(x*y);
}
}
公共静态void main(字符串[]args){
Catalog cat=新目录();
Queue queueObject=new LinkedList();
试一试{
方法printMethod=cat.getClass().getDeclaredMethod(“print”,新类[]{Integer.Class,Integer.Class});
//现在,您可以从队列中添加和删除方法
queueObject.add(printMethod);
//调用刚刚添加的方法
System.out.println(queueObject.element().invoke(cat,10,20));
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}`

我得到这个输出:200

您可以通过以下方式使用反射:

public class Catalog {
  public void print(Integer x, Integer y){      
    System.out.println(x*y);
  }

}

public static void main(String[] args) {
    Catalog cat = new Catalog();
    Queue<Method> queueObject = new LinkedList<Method>();
    try {
        Method printMethod = cat.getClass().getDeclaredMethod("print", new Class[]{Integer.class,Integer.class});       

        //Now you can add and remove your methods from queues
        queueObject.add(printMethod);

        //invoke the just added method
        System.out.println(queueObject.element().invoke(cat,10,20));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}`
公共类目录{
公共无效打印(整数x,整数y){
系统输出打印ln(x*y);
}
}
公共静态void main(字符串[]args){
Catalog cat=新目录();
Queue queueObject=new LinkedList();
试一试{
方法printMethod=cat.getClass().getDeclaredMethod(“print”,新类[]{Integer.Class,Integer.Class});
//现在,您可以从队列中添加和删除方法
queueObject.add(printMethod);
//调用刚刚添加的方法
System.out.println(queueObject.element().invoke(cat,10,20));
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}`

我得到这个输出:200

队列
队列。添加(()->添加(10,20))
。一个更好的问题应该包括您尝试了什么?对于示例代码,您希望实现什么样的错误、行为等挑战?
队列
队列。添加(()->添加(10,20))
。一个更好的问题应该包括您尝试了什么?对于示例代码,您希望实现什么样的错误、行为等挑战?非常感谢,如果我们有不同的参数,如(byte[],int)@akashdoijode,只要函数的返回类型相同,那就没有问题了。如果每个函数的返回类型不同,则需要使用
Supplier
而不是
IntSupplier
。如果您这样做,您将丢失一些类型信息。非常感谢,如果我们有不同的参数,如(byte[],int)@akashdoijode,那没有问题,只要函数的返回类型相同。如果每个函数的返回类型不同,则需要使用
Supplier
而不是
IntSupplier
。但是,如果这样做,您将丢失一些类型信息。为什么对不必使用反射的对象使用反射?为什么对不必使用反射的对象使用反射?