这是自己瞎写的,主要是为了能4个线程同时执行。。凑活了。。
主要要加上TestNG,实现如下:
@Test(invocationCount = 1000) public void test() throws ExecutionException, InterruptedException { final ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); Future future1 = fixedThreadPool.submit(runnable1); Future future2 = fixedThreadPool.submit(runnable2); Future future3 = fixedThreadPool.submit(runnable3); Future future4 = fixedThreadPool.submit(runnable4); Future future5 = fixedThreadPool.submit(runnable5); Future future6 = fixedThreadPool.submit(runnable6); Future future7 = fixedThreadPool.submit(runnable7); Future future8 = fixedThreadPool.submit(runnable8); Future future9 = fixedThreadPool.submit(runnable9); Future future10 = fixedThreadPool.submit(runnable10); future1.get(); future2.get(); future3.get(); future4.get(); future5.get(); future6.get(); future7.get(); future8.get(); future9.get(); future10.get(); fixedThreadPool.shutdown(); }以下是根据String值来找到类,执行方法
public class Main { public void method(String fileName, String url) { if (!fileName.equals("")) { DataInit.init(fileName); } try { Class<?> cla = Class.forName("com.jane.classname." + className); Method method = cla.getMethod("getNo", String.class); method.invoke(cla.newInstance(), url); } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.getCause().printStackTrace();//防止报异常(java.lang.reflect.InvocationTargetException),特殊处理 } } }