2013-05-17 15:50:46
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://computerdragon.blog.51cto.com/6235984/1202752
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package
whut.readwritelock;
public
class
Data {
private
final
char
[] buffer;
private
final
ReadWriteLock lock=
new
ReadWriteLock();
public
Data(
int
size)
{
this
.buffer=
new
char
[size];
for
(
int
i=
0
;i<buffer.length;i++)
buffer[i]=
'*'
;
}
public
char
[] read()
throws
InterruptedException
{
lock.readLock();
try
{
return
doRead();
}
finally
{
lock.readUnlock();
}
}
private
char
[] doRead()
{
char
[] newbuf=
new
char
[buffer.length];
for
(
int
i=
0
;i<buffer.length;i++)
newbuf[i]=buffer[i];
slowly();
return
newbuf;
}
public
void
write(
char
c)
throws
InterruptedException
{
lock.writeLock();
try
{
doWrite(c);
}
finally
{
lock.writeUnlock();
}
}
private
void
doWrite(
char
c)
{
for
(
int
i=
0
;i<buffer.length;i++)
{
buffer[i]=c;
slowly();
//这里的sleep并不会切换到别的线程
//这里就是体现了使用while的好处
//当该线程sleep时候,其余等待读取的还在wait中,而要写入的线程会判断它的状态,还没有释放锁
}
}
private
void
slowly()
{
try
{
Thread.sleep(
50
);
}
catch
(InterruptedException e)
{
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
whut.readwritelock;
public
class
ReaderThread
extends
Thread{
private
final
Data data;
public
ReaderThread(Data data)
{
this
.data=data;
}
public
void
run()
{
try
{
while
(
true
)
{
char
[] readbuf=data.read();
System.out.println(Thread.currentThread().getName()
+
" reads "
+String.valueOf(readbuf));
}
}
catch
(InterruptedException e)
{
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package
whut.readwritelock;
import
java.util.Random;
public
class
WriterThread
extends
Thread{
private
static
final
Random random=
new
Random();
private
final
Data data;
private
final
String filler;
private
int
index=
0
;
public
WriterThread(Data data,String filler)
{
this
.data=data;
this
.filler=filler;
}
public
void
run()
{
try
{
while
(
true
)
{
char
c=nextChar();
data.write(c);
Thread.sleep(random.nextInt(
1000
));
}
}
catch
(InterruptedException e)
{
}
}
private
char
nextChar()
{
char
c=filler.charAt(index);
index++;
if
(index>=filler.length())
index=
0
;
return
c;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package
whut.readwritelock;
//关键部分
public
class
ReadWriteLock {
private
int
readingReaders=
0
;
//实际正在读取的线程数目
private
int
waitingWriters=
0
;
//正在等待写入的线程数目
private
int
writingWriters=
0
;
//实际正在写入的线程数目
private
boolean
preferWriter=
true
;
//写入优先的话,值为true
//读取的时候获取锁
public
synchronized
void
readLock()
throws
InterruptedException
{
//当有写入的时候,或者写入为优先级并且有等待的写入线程
while
(writingWriters>
0
||(preferWriter&&waitingWriters>
0
))
{
wait();
}
readingReaders++;
}
//读完毕后释放锁
public
synchronized
void
readUnlock()
throws
InterruptedException
{
readingReaders--;
preferWriter=
true
;
notifyAll();
}
//写入的时候获取锁
public
synchronized
void
writeLock()
throws
InterruptedException
{
waitingWriters++;
//正在等待的写入的线程数目
try
{
//有写入或者读入的时候
while
(readingReaders>
0
||writingWriters>
0
)
{
wait();
}
}
finally
{
waitingWriters--;
//被唤醒了,则就是进而真正写入
}
writingWriters++;
}
//写入毕后释放锁
public
synchronized
void
writeUnlock()
throws
InterruptedException
{
writingWriters--;
preferWriter=
false
;
//写入后马上更换优先级,让读者继续
notifyAll();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
whut.readwritelock;
public
class
ReadWriteMain {
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
Data data=
new
Data(
10
);
//读取线程
new
ReaderThread(data).start();
new
ReaderThread(data).start();
new
ReaderThread(data).start();
new
ReaderThread(data).start();
new
ReaderThread(data).start();
new
ReaderThread(data).start();
//写入线程
new
WriterThread(data,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
).start();
new
WriterThread(data,
"abcdefghijklmnopqrstuvwxyz"
).start();
}
}
|
2013-05-21 09:41:28
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://computerdragon.blog.51cto.com/6235984/1205324
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package
whut.threadpool;
//用到了生产者与消费者模式
//生成线程池,接受客户端线程的请求,找到一个工作线程分配该客户端请求
public
class
Channel {
private
static
final
int
MAX_REQUEST =
100
;
// 并发数目,就是同时可以接受多少个客户端请求
//利用数组来存放请求,每次从数组末尾添加请求,从开头移除请求来处理
private
final
Request[] requestQueue;
// 存储接受客户线程的数目
private
int
tail;
//下一次存放Request的位置
private
int
head;
//下一次获取Request的位置
private
int
count;
// 当前request数量
private
final
WorkerThread[] threadPool;
// 存储线程池中的工作线程
// 运用数组来存储
public
Channel(
int
threads) {
this
.requestQueue =
new
Request[MAX_REQUEST];
this
.head =
0
;
this
.head =
0
;
this
.count =
0
;
threadPool =
new
WorkerThread[threads];
// 启动工作线程
for
(
int
i =
0
; i < threadPool.length; i++) {
threadPool[i] =
new
WorkerThread(
"Worker-"
+ i,
this
);
}
}
public
void
startWorkers() {
for
(
int
i =
0
; i < threadPool.length; i++) {
threadPool[i].start();
}
}
// 接受客户端请求线程
public
synchronized
void
putRequest(Request request) {
// 当Request的数量大于或等于同时接受的数目时候,要等待
while
(count >= requestQueue.length)
try
{
wait();
}
catch
(InterruptedException e) {
}
requestQueue[tail] = request;
tail = (tail +
1
) % requestQueue.length;
count++;
notifyAll();
}
// 处理客户端请求线程
public
synchronized
Request takeRequest() {
while
(count <=
0
)
try
{
wait();
}
catch
(InterruptedException e) {
}
Request request = requestQueue[head];
head = (head +
1
) % requestQueue.length;
count--;
notifyAll();
return
request;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
whut.threadpool;
import
java.util.Random;
//向Channel发送Request请求的
public
class
ClientThread
extends
Thread{
private
final
Channel channel;
private
static
final
Random random=
new
Random();
public
ClientThread(String name,Channel channel)
{
super
(name);
this
.channel=channel;
}
public
void
run()
{
try
{
for
(
int
i=
0
;
true
;i++)
{
Request request=
new
Request(getName(),i);
channel.putRequest(request);
Thread.sleep(random.nextInt(
1000
));
}
}
catch
(InterruptedException e)
{
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
whut.threadpool;
//具体工作线程
public
class
WorkerThread
extends
Thread{
private
final
Channel channel;
public
WorkerThread(String name,Channel channel)
{
super
(name);
this
.channel=channel;
}
public
void
run()
{
while
(
true
)
{
Request request=channel.takeRequest();
request.execute();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package
whut.threadpool2;
import
java.util.Vector;
/*
* 这个主要的作用如下
* 0,缓冲客户请求线程(利用生产者与消费者模式)
* 1,存储客户端请求的线程
* 2,初始化启动一定数量的线程
* 3,主动来唤醒处于任务池中wait set的一些线程来执行任务
*/
public
class
Channel {
public
final
static
int
THREAD_COUNT=
4
;
public
static
void
main(String[] args) {
//定义两个集合,一个是存放客户端请求的,利用Vector,
//一个是存储线程的,就是线程池中的线程数目
//Vector是线程安全的,它实现了Collection和List
//Vector 类可以实现可增长的对象数组。与数组一样,
//它包含可以使用整数索引进行访问的组件。但Vector 的大小可以根据需要增大或缩小,
//以适应创建 Vector 后进行添加或移除项的操作。
//Collection中主要包括了list相关的集合以及set相关的集合,Queue相关的集合
//注意:Map不是Collection的子类,都是java.util.*下的同级包
Vector pool=
new
Vector();
//工作线程,初始分配一定限额的数目
WorkerThread[] workers=
new
WorkerThread[THREAD_COUNT];
//初始化启动工作线程
for
(
int
i=
0
;i<workers.length;i++)
{
workers[i]=
new
WorkerThread(pool);
workers[i].start();
}
//接受新的任务,并且将其存储在Vector中
Object task=
new
Object();
//模拟的任务实体类
//此处省略具体工作
//在网络编程中,这里就是利用ServerSocket来利用ServerSocket.accept接受一个Socket从而唤醒线程
//当有具体的请求达到
synchronized
(pool)
{
pool.add(pool.size(), task);
pool.notifyAll();
//通知所有在pool wait set中等待的线程,唤醒一个线程进行处理
}
//注意上面这步骤添加任务池请求,以及通知线程,都可以放在工作线程内部实现
//只需要定义该方法为static,在方法体用同步块,且共享的线程池也是static即可
//下面这步,可以有可以没有根据实际情况
//取消等待的线程
for
(
int
i=
0
;i<workers.length;i++)
{
workers[i].interrupt();
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package
whut.threadpool2;
import
java.util.List;
public
class
WorkerThread
extends
Thread {
private
List pool;
//任务请求池
private
static
int
fileCompressed=
0
;
//所有实例共享的
public
WorkerThread(List pool)
{
this
.pool=pool;
}
//利用静态synchronized来作为整个synchronized类方法,仅能同时一个操作该类的这个方法
private
static
synchronized
void
incrementFilesCompressed()
{
fileCompressed++;
}
public
void
run()
{
//保证无限循环等待中
while
(
true
)
{
//共享互斥来访问pool变量
synchronized
(pool)
{
//利用多线程设计模式中的
//Guarded Suspension Pattern,警戒条件为pool不为空,否则无限的等待中
while
(pool.isEmpty())
{
try
{
pool.wait();
//进入pool的wait set中等待着,释放了pool的锁
}
catch
(InterruptedException e)
{
}
}
//当线程被唤醒,需要重新获取pool的锁,
//再次继续执行synchronized代码块中其余的工作
//当不为空的时候,继续再判断是否为空,如果不为空,则跳出循环
//必须先从任务池中移除一个任务来执行,统一用从末尾添加,从开始处移除
pool.remove(
0
);
//获取任务池中的任务,并且要进行转换
}
//下面是线程所要处理的具体工作
}
}
}
|