这个代码线程安全吗?
创建可运行并通过反射调用方法:
public class A {
public static void someMethod (List<VO> voList){
int endIndex=0;
for (int firstIndex = 0; firstIndex < voList.size(); ) {
endIndex = Math.min(firstIndex + threadSize, voList.size());
Runner runner = null;
try {
runner = new Runner(voList.subList(firstIndex, endIndex),
B.class.getMethod("createSomeString", D.class));
} catch (NoSuchMethodException ex) {
log.warn(ex.getMessage());
}
//start a thread
runner.start();
}
}
private static class Runner extends Thread {
private Method method;
private List<C> list;
public Runner(Method method,List<C> clist) {
this.method = method;
this.list=clist;
}
}
public void run() {
for (C vo: list) {
String xml = (String) method.invoke(null,vo);
}
}
}
我想通过反射调用静态方法,这个代码块线程安全吗?
public class B {
public static String createSomeString(D a) throws Exception {
return a.name;
}
}
D.class 是普通的旧 java 对象类,如下所示:
public class D implements Serializable{
private String name;
}