`
daojin
  • 浏览: 690234 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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 {
                                               if (!mQueues.isEmpty()) continue;
						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

谢谢!

 

  • 大小: 26 KB
  • 大小: 18.3 KB
2
0
分享到:
评论
1 楼 3482561 2017-08-18  
    

相关推荐

    Android面试题集2020版(包含答案整理)第二部分.pdf

    在Android开发中,Binder是一种高效并且常用的跨进程通信(IPC)机制,它不仅实现了进程间的通信,而且确保了数据传输的安全和稳定。为了全面理解Binder机制,我们将从多个角度分析其原理及模型组成,并结合具体实现...

    Android 面试题集

    【Android面试题集】 在Android开发领域,面试是评估求职者技能的重要环节。这份题集涵盖了从基础到高级的各种知识点,旨在帮助准备Android面试的开发者巩固和深化对Android平台的理解。以下是一些关键领域的详细...

    android面试题目几套

    以下是对"android面试题目几套"中可能涉及的一些核心知识点的详细解释: 1. **Android基础知识** - **四大组件**:Activity(活动)、Service(服务)、BroadcastReceiver(广播接收器)和ContentProvider(内容...

    张泽华Android面试题整理

    这个资料集是张泽华对于Android面试题目的系统化梳理,包含了从基础到高级的各种问题,旨在帮助开发者在面试前进行充分的复习,强化技术实力,确保在面试过程中能够游刃有余地解答各种技术难题。 【标签】:“张...

    Android面试宝典5.0

    这部分汇总了诸多精选的Android面试题目,包括但不限于Android源码编译、Activity、Service、UI性能分析等方面的问题。对于想要全面掌握Android面试要点的开发者来说,这一部分是提高面试成功率的关键。 四、Java...

    Android面试宝典4.0

    《Android面试宝典4.0》是一本专门为准备参加Android相关职位面试的求职者准备的面试题集和知识点解析书籍,书中不仅覆盖了Android基础和高级知识点,还包含了Java基础和高级面试内容。此外,书中还涉及面试技巧、...

    Android示例面试题集

    这份"Android示例面试题集"旨在帮助求职者准备常见的Android面试问题,同时也为面试官提供了一套参考性的题目库。下面,我们将深入探讨其中涉及的一些核心知识点。 1. Android体系结构: - Android系统分为Linux...

    总结整理的Android面试Java基础知识点面试资料精编汇总文档资料合集.zip

    总结整理的Android面试Java基础知识点面试资料精编汇总文档资料合集: Android面试 常见58题.docx Android常见原理性面试专题.docx Android面试常问基础知识点.docx BAT Android面试20题详解.docx Java基础面试题....

    史上最全Android面试笔试大全

    【Android面试笔试题】这份资料集是为准备Android面试的开发者量身打造的,涵盖了从基础到高级的各种知识点,旨在帮助求职者系统性地提升自己的技术能力,顺利走上高薪之路。以下是一些核心的Android面试知识点: 1...

    Android面试题集2020版(包含答案整理)第二部分1

    【Android面试题集2020版(包含答案整理)第二部分1】 在Android系统中,Binder机制是实现跨进程通信的关键技术。Binder是Android特有的,它不仅是一种通信方式,而且是一个内核驱动,用于连接应用程序和服务之间的...

    Android面试题集[借鉴].pdf

    在Android面试中,开发者需要掌握一系列关键知识点,包括但不限于Java、C/C++的基础以及Android系统架构。以下是对这些知识点的详细解释: 1. **Android手机操作系统的四层架构**: - **Applications(应用程序)*...

    2014年android面试题大全

    以上是2014年Android面试中可能涉及的一些重要知识点,随着Android技术的不断发展,面试题目的覆盖面也在不断扩大,如Android Jetpack组件、Kotlin语言特性、MVVM架构等都成为近年来的热门话题。持续学习和跟踪最新...

    [Android实例] 面试题集.zip

    【Android实例】面试题集.zip是一个包含Android编程面试问题的文档集合,主要针对计算机科学和技术领域,特别是专注于Android应用开发的求职者。这份资源可能是为了帮助开发者准备面试,强化他们的技术知识,理解...

    [Android实例] 面试题集

    【Android实例】面试题集是针对Android开发人员在求职面试中可能会遇到的问题的集合,它涵盖了Android应用开发的各个方面,包括基础知识、UI设计、性能优化、网络编程、数据存储、多线程、权限管理以及一些高级特性...

    android 面试笔试题(有答案)

    【Android面试笔试题详解】 Android作为全球最大的移动操作系统,其开发者需求旺盛,面试竞争激烈。以下是一些核心的Android知识点,这些题目在许多面试中都可能遇到,它们涵盖了基础概念、编程技巧、性能优化以及...

    【Android项目实例】常见面试题集一览

    【Android项目实例】常见面试题集一览,是针对Android开发者准备的一份综合性的面试题库,旨在帮助求职者提升在Android移动开发领域的专业素养。这份题集涵盖了Android基础、UI设计、性能优化、网络编程、数据存储等...

    Android大厂面试题库

    这份“Android大厂面试题库”集合了来自Sohu、QQ、Taobao、Baidu、360等知名公司的面试题目,旨在帮助求职者更好地准备前两轮技术面试。下面,我们将详细探讨这些大厂可能关注的Android知识点。 一、Android基础 1....

    [Android实例] 面试题集.rar

    【Android实例】面试题集是...这份【Android实例】面试题集可能包含上述所有或部分知识点的题目和解析,是毕业生和有志于Android开发者的宝贵学习资料。通过深入学习和实践,可以有效提升个人在Android领域的专业能力。

Global site tag (gtag.js) - Google Analytics