} }----------------------------------------------------------------------------------------------------------------------------- package com.synnex.just.test;public class Main { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { SomeMethod m = new SomeMethod(); SomeThread t = new SomeThread(m); t.start();
private String currentMethodName;
public void m1(){
setCurrentMethodName("m1");
System.out.println("m1 executing .....");
}
public void m2(){
setCurrentMethodName("m2");
System.out.println("m2 executing .....");
}
public void m3(){
setCurrentMethodName("m3");
System.out.println("m3 executing .....");
}
public void m4(){
setCurrentMethodName("m4");
System.out.println("m4 executing .....");
} public String getCurrentMethodName() {
return currentMethodName;
}
public void setCurrentMethodName(String currentMethodName) {
this.currentMethodName = currentMethodName;
}}
--------------------------------------------------------------------------------------------------------------------------------package com.synnex.just.test;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class SomeThread extends Thread { private SomeMethod m ;
public SomeThread(SomeMethod m){
this.m=m;
}
@Override
public void run() {
Class<?> classz=null;
try {
classz = Class.forName("com.synnex.just.test.SomeMethod");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method[] declaredMethods = classz.getDeclaredMethods();
while(true){
for(Method method : declaredMethods){
if(!method.getName().startsWith("m")){
continue;
}
try {
Thread.currentThread().sleep(2000);
method.invoke(m, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}-----------------------------------------------------------------------------------------------------------------------------
package com.synnex.just.test;public class Main { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
SomeMethod m = new SomeMethod();
SomeThread t = new SomeThread(m);
t.start();
while(true){
Thread.sleep(5000);
System.out.println("Current method"+m.getCurrentMethodName());
}
}}
不知道能不能满足楼主要求。
话说回来着就是个任务进度跟踪的功能啊,只要每个请求都有对应的唯一任务号就能做到。