使用Java线程并行/串行执行一组功能

时间:2018-07-25 11:54:42

标签: java multithreading

有下面的表,其中包含MyRunnable类中的所有函数/方法名称, !(https://imgur.com/a/c3SavnK

因此对于RFC,我要按顺序执行所有组, 但是在每个组中,这些功能将根据并行标志并行/串行运行。

对于组#1>主Java>在并行线程中shld生成FNC1,FNC33, FNC12应该仅在线程完成之前启动一次,标记为并行N 并且在上一个线程> FNC343完成后,FNC892应该并行执行。

第2组>都标记为N表示并行,因此它的串行线程。

第3组>串行执行FNC31, FNC32标记为手册,因此手册步骤上方的所有功能均应完成,并且代码应退出。.

所以在我的主班上, 在迭代每个组#的for循环中,结果集中的每一行都进入if循环,以检查是否跳过并创建了一个worker,如下所示,该MyRunnable方法是使用反射调用的。

worker = new MyRunnable(met_name,met_num,rfc_num);
executor.execute(worker);

MyRunnable片段:

 public class MyRunnable implements Runnable {
 private String mname;
 protected int rfc_num;
 protected int met_num;

public MyRunnable() {
    // Do nothing - no args constructor

}

public MyRunnable(String mname,int met_num,int rfc_num) {
    this.mname=mname;
    this.rfc_num=rfc_num;
    this.met_num=met_num;
    // TODO Auto-generated constructor stub
}

protected static void FNC12(int mnum,int rnum){ System.out.println("Running code for FNC12");}

   // All other functions here

@Override
public void run() {
    System.out.println("INSIDE RUN:"+mname+" "+rfc_num+" "+met_num);
    Method Call_met;
    try {
            Class<?> base = Class.forName("test.MyRunnable");
            Call_met = base.getDeclaredMethod(mname, int.class,int.class);
            System.out.println(mname+" "+rfc_num+" "+met_num);
            Call_met.invoke(base.newInstance(),met_num,rfc_num);
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


}

}

到目前为止,一旦在组中遇到“并行否”,我将无法停止/完成线程 或在新线程中串行运行并行N个函数。谁能帮忙代码或逻辑吗?

1 个答案:

答案 0 :(得分:0)

建议使用ExecutorService。它消除了手动创建/启动/加入线程的需要。将main方法中的模式用于您工作的并行部分。

package com.stackoverflow.q51518249;

import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

public class MultiThreading {
    public static void main(final String[] args) {
        final ForkJoinPool pool = ForkJoinPool.commonPool();

        // execute 10 tasks
        for (int i = 0; i < 10; i++) {
            pool.execute(new MyRunnable(i));
        }

        pool.awaitQuiescence(5, TimeUnit.SECONDS);
    }

    public static final class MyRunnable implements Runnable {
        private final int number;

        public MyRunnable(final int number) {
            this.number = number;
        }

        @Override
        public void run() {
            System.out.println("Thread " + number + " started.");

            // business logic

            System.out.println("Thread " + number + " finished.");
        }
    }
}

这将产生如下输出:

Thread 0 started.
Thread 1 started.
Thread 2 started.
Thread 3 started.
Thread 2 finished.
Thread 4 started.
Thread 1 finished.
Thread 5 started.
Thread 5 finished.
Thread 6 started.
Thread 3 finished.
Thread 7 started.
Thread 0 finished.
Thread 8 started.
Thread 8 finished.
Thread 9 started.
Thread 4 finished.
Thread 7 finished.
Thread 6 finished.
Thread 9 finished.