Android_BlockingQueue_并发线程

本博文为子墨原创,转载请注明出处!
http://blog.csdn.net/zimo2013/article/details/17301661
由于博主最近时间比较紧张,该部分内容暂时贴上关键代码,待以后有时间再详细说明,请见谅!对于该类,博主在查看Volley的源代码时才发现,请见 Android_开源框架_Volley(Google IO 2013)源代码及内部实现分析 一文!
/**Service提供的方法*/
public interface IMyBinder {
	public void add(Bean bean);
}
/** Service,主要处理网络耗时等操作 */
public class MyService extends Service {

	/** 主要用handler传递一个操作 */
	private Handler handler = new Handler(Looper.getMainLooper());// 默认为MainLooper
	//
	private BlockingQueue<Bean> queue;
	private WorkerThread thread;

	@Override
	public void onCreate() {
		queue = new ArrayBlockingQueue<Bean>(5);
		thread = new WorkerThread();
		//开启线程
		thread.start();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	@Override
	public void onDestroy() {
		if (thread != null && !thread.isInterrupted()) {
			thread.quit();
		}
		super.onDestroy();
	}

	public interface DoWorkCallback {
		public void onUpdateProgress(int progress);
		public void onUpdateCompleted();
	}

	public class MyBinder extends Binder implements IMyBinder {
		@Override
		public void add(Bean bean) {
			queue.add(bean);
		}
	}

	private class WorkerThread extends Thread {
		private boolean isQuited = false;

		@Override
		public void run() {
			while (true) {
				Bean bean = null;
				try {
					//该方法可能是阻塞的
					bean = queue.take();
					if (bean != null) {
						//传递一个操作,有MainLooper执行该runnable的run方法
						handler.post(bean.runnable);
					}
				} catch (InterruptedException e) {
					if (!isQuited) {
						continue;
					} else {
						return;
					}
				}
			}
		}

		//退出线程
		public void quit() {
			isQuited = true;
			queue.clear();
			interrupt();
		}
	}
}
public class MainActivity extends Activity {

	private ProgressBar bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		bar = (ProgressBar) findViewById(R.id.bar);
	}

	public void click(View view) {
		Intent intent = new Intent(this, MyService.class);
		bindService(intent, connection, BIND_AUTO_CREATE);
	}

	private ServiceConnection connection = new ServiceConnection() {

		/**
		 * 绑定完成,调用该方法,该方法运行在主线程中
		 */
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			IMyBinder binder = (IMyBinder)service;
			System.out.println("onServiceConnected:"+Thread.currentThread());
			//doWork 接收一个url String 和一个回调接口,主要用于更新ui
			Bean bean = new Bean();
			bean.runnable = new Runnable() {
				
				@Override
				public void run() {
					//TODO:耗时操作
				}
			};
			bean.callback = new DoWorkCallback() {
				
				@Override
				public void onUpdateProgress(int progress) {
					
				}
				
				@Override
				public void onUpdateCompleted() {
					
				}
			};
			binder.add(bean);
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {

		}
	};
}
public class Bean {
	public Runnable runnable;
	public DoWorkCallback callback;

}

你可能感兴趣的:(android,并发,线程,callback,BlockingQueue)