分享一个非常好用的缓冲区和侦听模板(Java、提供源代码下载)

啥也不说了,直接上代码,看怎么用的

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) {
		}
	}
}


有问题请留言

源 码 下 载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值