MyException - 我的异常网
当前位置:我的异常网» Android » Android 口试题目之 线程池

Android 口试题目之 线程池

www.MyException.Cn  网友分享于:2013-08-22  浏览:0次
Android 面试题目之 线程池
记不住密码怎么办?
http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword

1. 线程池结构模型。
线程池一般情况下需要有三个独立的小模块组成:
a. 任务队列
b. 任务队列协调器
c. 工作线程
   下面的图示显示了他们的静态结构图。



2. 线程池交互模型。
下图中显示了其交互模型。
a.客户代码负责入队,
b.工作线程负责loop队列进行工作,同时工作结束后告诉任务队列协调器。
c. 任务队列协调器,协调所有的工作线程和任务队列,使得任务一定可以被执行, 控制线程数目和核心线程数目,同时当客户代码入队时,通知正在wait的工作线程进行工作。


3.测试代码
测试代码进行测试时,模拟一下真实场景。也就是说调用者随机时间点进行调用。执行者的时间也是随机的。
package interview;

public class TestThreadPool {
	static volatile int lastIndex = 0;
	static final int MAX_SLEEP = 3;
	static int runCount = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println("testThreadPoolWithN =============== ");

		testThreadPoolWithN();

		System.out.println("testThreadPoolWith1 =============== ");
		testThreadPoolWith1();
		// System.out.println("count " + count);
	}

	private static void testThreadPoolWith1() {
		ThreadPool threadPool = new ThreadPool(1, 1, 0);

		for (int i = 0; i < 200; ++i) {
			final int index = i;
			randomSleep();
			threadPool.execute(new Runnable() {

				@Override
				public void run() {
					randomSleep();
					assert ((lastIndex + 1) == index);
					lastIndex = index;

					System.out.println("running " + index);
				}

			});

		}
	}

	private static void testThreadPoolWithN() {
		ThreadPool threadPool = new ThreadPool(10, 20, 0);
		for (int i = 0; i < 200; ++i) {
			final int index = i;
			randomSleep();
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					runCount++;
					randomSleep();

					System.out.println("running " + index);
				}

			});
		}
		assert (runCount == 2000);
	}

	public static void randomSleep() {
		try {
			Thread.sleep((long) (Math.random() * MAX_SLEEP));
		} catch (InterruptedException e1) {

		}
	}

}



3.线程池
package interview;

import java.util.LinkedList;
import java.util.Queue;

public class ThreadPool {

	private Object mLock = new Object();

	private Queue<Runnable> mQueues = new LinkedList<>();

	private int mCount = 0;

	private int mMaxCount = 3;

	private int mCoreSize = 1;

	private long mNonCoreKeepLiveTime = 0;

	public ThreadPool(int coreSize, int maxSize, long nonCoreKeepLiveTime) {
		mCoreSize = coreSize;
		mMaxCount = maxSize;
		mNonCoreKeepLiveTime = nonCoreKeepLiveTime;
	}

	public int getRunningCount() {
		synchronized (mLock) {
			return mCount;
		}
	}

	private class Worker implements Runnable {

		private Runnable mFirstTask;

		private Worker(Runnable task) {
			mFirstTask = task;
		}

		@Override
		public void run() {
			if (mFirstTask != null) {
				mFirstTask.run();
			}
			while (true) {
				Runnable task;
				synchronized (mLock) {
					task = mQueues.poll();
				}
				if (task != null) {
					task.run();
					continue;
				}
				synchronized (mLock) {
					if (mCount > mCoreSize) {
						try {
							mLock.wait(mNonCoreKeepLiveTime);
						} catch (InterruptedException e) {

						}
						if (mQueues.isEmpty()) {
							break;
						}
					} else {
						try {
							mLock.wait();
						} catch (InterruptedException e) {

						}
					}

				}
			}
			exisWorker(this);
		}
	}

	private void exisWorker(Worker worker) {
		int size;
		synchronized (mLock) {
			mCount--;
			size = mQueues.size();
			int min = mCoreSize;
			if (size > 0 && min == 0) {
				min = 1;
			}
			if (mCount >= min) {
				return;
			}
		}
		addWorker();
	}

	private void addWorker() {
		synchronized (mLock) {
			if (mCount >= mMaxCount) {
				return;
			}
			mCount++;
		}
		Thread thread = new Thread(new Worker(null));
		thread.start();
	}

	public void execute(Runnable runnable) {
		synchronized (mLock) {
			mQueues.add(runnable);
			mLock.notifyAll();
		}
		addWorker();
	}
}


4. 打印结果:
testThreadPoolWithN =============== 
running 0
running 1
running 3
running 2
running 4
running 5
running 6
running 7
running 8
running 9
running 10
running 11
running 12
running 13
running 15
running 14
running 16
running 17
running 19
running 18
running 21
running 20
running 22
running 23
running 24
running 25
running 26
running 27
running 28
running 29
running 30
running 31
running 32
running 33
running 35
running 34
running 36
running 37
running 38
running 39
running 41
running 40
running 42
running 44
running 43
running 45
running 46
running 47
running 48
running 49
running 51
running 50
running 52
running 54
running 53
running 55
running 56
running 57
running 58
running 59
running 60
running 61
running 63
running 62
running 64
running 65
running 67
running 66
running 68
running 69
running 70
running 71
running 72
running 74
running 73
running 76
running 75
running 77
running 79
running 78
running 80
running 81
running 84
running 82
running 85
running 83
running 86
running 88
running 87
running 89
running 90
running 91
running 92
running 93
running 94
running 97
running 96
running 98
running 95
running 99
running 100
running 101
running 102
running 104
running 105
running 103
running 107
running 106
running 108
running 110
running 109
running 112
running 111
running 113
running 114
running 115
running 116
running 117
running 118
running 119
running 120
running 121
running 122
running 123
running 124
running 125
running 126
running 127
running 128
running 129
running 130
running 131
running 133
running 132
running 134
running 136
running 135
running 138
running 137
running 139
running 140
running 141
running 142
running 143
running 144
running 145
running 146
running 147
running 148
running 149
running 150
running 151
running 152
running 153
running 154
running 155
running 156
running 157
running 158
running 160
running 161
running 159
running 164
running 165
running 162
running 163
running 166
running 167
running 168
running 169
running 170
running 171
running 172
running 173
running 174
running 175
running 176
running 177
running 180
running 179
running 178
running 182
running 181
running 183
running 184
running 185
running 186
running 188
running 187
running 189
running 190
running 191
running 192
running 194
running 193
running 195
running 196
running 197
running 198
running 199
testThreadPoolWith1 =============== 
running 0
running 1
running 2
running 3
running 4
running 5
running 6
running 7
running 8
running 9
running 10
running 11
running 12
running 13
running 14
running 15
running 16
running 17
running 18
running 19
running 20
running 21
running 22
running 23
running 24
running 25
running 26
running 27
running 28
running 29
running 30
running 31
running 32
running 33
running 34
running 35
running 36
running 37
running 38
running 39
running 40
running 41
running 42
running 43
running 44
running 45
running 46
running 47
running 48
running 49
running 50
running 51
running 52
running 53
running 54
running 55
running 56
running 57
running 58
running 59
running 60
running 61
running 62
running 63
running 64
running 65
running 66
running 67
running 68
running 69
running 70
running 71
running 72
running 73
running 74
running 75
running 76
running 77
running 78
running 79
running 80
running 81
running 82
running 83
running 84
running 85
running 86
running 87
running 88
running 89
running 90
running 91
running 92
running 93
running 94
running 95
running 96
running 97
running 98
running 99
running 100
running 101
running 102
running 103
running 104
running 105
running 106
running 107
running 108
running 109
running 110
running 111
running 112
running 113
running 114
running 115
running 116
running 117
running 118
running 119
running 120
running 121
running 122
running 123
running 124
running 125
running 126
running 127
running 128
running 129
running 130
running 131
running 132
running 133
running 134
running 135
running 136
running 137
running 138
running 139
running 140
running 141
running 142
running 143
running 144
running 145
running 146
running 147
running 148
running 149
running 150
running 151
running 152
running 153
running 154
running 155
running 156
running 157
running 158
running 159
running 160
running 161
running 162
running 163
running 164
running 165
running 166
running 167
running 168
running 169
running 170
running 171
running 172
running 173
running 174
running 175
running 176
running 177
running 178
running 179
running 180
running 181
running 182
running 183
running 184
running 185
running 186
running 187
running 188
running 189
running 190
running 191
running 192
running 193
running 194
running 195
running 196
running 197
running 198
running 199


从上面结果可以看出,单线程按照顺序进行执行。而多线程则是无顺序的。

如果觉得本博客对你有帮助,点一下以下链接,支持一下,支付宝官方认证,无毒无广告,还能帮你解决密码记不住的问题!

http://android.myapp.com/myapp/detail.htm?apkName=com.wa505.kf.epassword

谢谢!
1 楼 3482561 前天  
    

文章评论

老程序员的下场
老程序员的下场
 程序员的样子
程序员的样子
那些争议最大的编程观点
那些争议最大的编程观点
每天工作4小时的程序员
每天工作4小时的程序员
旅行,写作,编程
旅行,写作,编程
2013年中国软件开发者薪资调查报告
2013年中国软件开发者薪资调查报告
Web开发人员为什么越来越懒了?
Web开发人员为什么越来越懒了?
10个调试和排错的小建议
10个调试和排错的小建议
程序员必看的十大电影
程序员必看的十大电影
代码女神横空出世
代码女神横空出世
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
Java程序员必看电影
Java程序员必看电影
程序员都该阅读的书
程序员都该阅读的书
亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
为什么程序员都是夜猫子
为什么程序员都是夜猫子
漫画:程序员的工作
漫画:程序员的工作
一个程序员的时间管理
一个程序员的时间管理
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
鲜为人知的编程真相
鲜为人知的编程真相
程序员的鄙视链
程序员的鄙视链
程序员眼里IE浏览器是什么样的
程序员眼里IE浏览器是什么样的
编程语言是女人
编程语言是女人
老美怎么看待阿里赴美上市
老美怎么看待阿里赴美上市
我是如何打败拖延症的
我是如何打败拖延症的
如何成为一名黑客
如何成为一名黑客
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
中美印日四国程序员比较
中美印日四国程序员比较
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
团队中“技术大拿”并非越多越好
团队中“技术大拿”并非越多越好
60个开发者不容错过的免费资源库
60个开发者不容错过的免费资源库
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
总结2014中国互联网十大段子
总结2014中国互联网十大段子
看13位CEO、创始人和高管如何提高工作效率
看13位CEO、创始人和高管如何提高工作效率
Google伦敦新总部 犹如星级庄园
Google伦敦新总部 犹如星级庄园
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
2013年美国开发者薪资调查报告
2013年美国开发者薪资调查报告
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
我的丈夫是个程序员
我的丈夫是个程序员
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
程序员应该关注的一些事儿
程序员应该关注的一些事儿
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有