1. 程式人生 > >分享一個非常好用的緩衝區和偵聽模板(Java、提供原始碼下載)

分享一個非常好用的緩衝區和偵聽模板(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) {
		}
	}
}


有問題請留言

源 碼 下 載