啥也不说了,直接上代码,看怎么用的
package unit;
import com.linkage.bss.crm.asynlaunch.ALSwitch;
import com.linkage.bss.crm.asynlaunch.AsynItem;
import com.linkage.bss.crm.asynlaunch.AsynLaunch;
import com.linkage.bss.crm.monitor.BTask;
import com.linkage.bss.crm.monitor.ListenerMonitors;
import com.linkage.bss.crm.monitor.BTask.BExecutor;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
//1.化同步为异步
//A.同步方式
saySomething("Hello World!");
//B.修改为异步方式
AsynItem item = new AsynItem() {
public boolean launch() {
saySomething("Hello World 2 !");
return true;
}
};
AsynLaunch.asynLaunch(item);
//C.同步方式
saySomething("Hello World 3 !");
//此处输出结果:
/*
Hello World!
Hello World 3 !
Hello World 2 !
*/
//2.侦听功能
//需求:每隔1000ms、5个线程同时 saySomething("Hello-" + 数字)
BTask task = BTask.generate("saySomething Listener",1000,5,new BExecutor() {
int[] arr = {1,2,3,4,5};
public boolean execute() {
int i = (int)(System.currentTimeMillis()%5);
saySomething("Hello - " + arr[i]);
return false;
}
});
ListenerMonitors.put(task);
Sleep(10000);
//关闭缓冲池
ALSwitch.stop();
}
private static void saySomething(String greek) {
System.out.println(greek);
}
private static void Sleep(long ts){
try {
Thread.sleep(ts);
} catch (Exception e) {
}
}
}
有问题请留言