[color=red][size=xx-large]记不住密码怎么办?[/size][/color]
[url]http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword[/url]
[size=x-large][b]1. 线程池结构模型。[/b][/size]
线程池一般情况下需要有三个独立的小模块组成:
a. 任务队列
b. 任务队列协调器
c. 工作线程
下面的图示显示了他们的静态结构图。
[img]http://dl2.iteye.com/upload/attachment/0126/5834/6b81a090-6f21-34cd-868a-80eea9dbd18a.png[/img]
[size=x-large][b]2. 线程池交互模型。[/b][/size]
下图中显示了其交互模型。
a.客户代码负责入队,
b.工作线程负责loop队列进行工作,同时工作结束后告诉任务队列协调器。
c. 任务队列协调器,协调所有的工作线程和任务队列,使得任务一定可以被执行, 控制线程数目和核心线程数目,同时当客户代码入队时,通知正在wait的工作线程进行工作。
[img]http://dl2.iteye.com/upload/attachment/0126/5836/3b01865d-8904-351d-83c4-79d99871f48f.png[/img]
[size=x-large][b]3.测试代码[/b][/size]
测试代码进行测试时,模拟一下真实场景。也就是说调用者随机时间点进行调用。执行者的时间也是随机的。
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) {
}
}
}
[size=x-large][b]3.线程池[/b][/size]
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();
}
}
[size=x-large][b]4. 打印结果:[/b][/size]
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
从上面结果可以看出,单线程按照顺序进行执行。而多线程则是无顺序的。
谢谢!