zoukankan
html css js c++ java
java写的简单线程池
import java.util.
*
;
import java.io.
*
;
import java.net.
*
;
//
线程池类,创建处于使用中和空闲中的进程队列
class
ThreadPool
{
private
Vector freeThreads
=
new
Vector();
private
Vector inUseThreads
=
new
Vector();
//
Vetcor对象
private
static
int
value
=
100
;
//
线程池的最大并发线程数
public
ThreadPool()
{
fillpool(value);
}
private
void
fillpool(
int
poolsize)
{
for
(
int
i
=
0
;i
<
poolsize;i
++
)
{
//
this为所处的类内的当前该类对象,当前为ThreadPool类对象
PoolableThread pt
=
new
PoolableThread(
this
);
//
传入线程池对象
pt.start();
//
启动线程
freeThreads.add(pt);
//
加入空闲线程队列
}
try
{
Thread.sleep(
100
);
//
线程休眠
}
catch
(InterruptedException ie)
{}
}
public
synchronized
void
runTask(Runnable task)
{
//
运行Runnable接口
if
(freeThreads.isEmpty())
{
throw
new
RuntimeException(
"
All threads are in use
"
);
//
空闲线程为空,抛出异常
}
PoolableThread t
=
(PoolableThread)freeThreads.remove(
0
);
//
提取空闲线程
inUseThreads.add(t);
//
加入到使用状态队列当中
t.setTask(task);
//
传入Runnable接口类型对象
}
synchronized
void
free(PoolableThread t)
{
inUseThreads.remove(t);
//
释放使用状态队列
freeThreads.add(t);
//
加入到空闲队列中
}
}
//
PoolableThread进程类
class
PoolableThread extends Thread
{
Runnable task
=
null
;
ThreadPool pool;
PoolableThread(ThreadPool pool)
{
this
.pool
=
pool;
//
PoolableThread类当前对象(this)的pool变量初始化
}
synchronized
void
setTask(Runnable task)
{
this
.task
=
task;
//
PoolableThread类当前对象(this)的task变量初始化
notify();
//
唤醒等待进程
}
synchronized
void
executeTasks()
{
//
等待进程被唤醒后执行Runnable接口
for
(; ; )
{
try
{
if
(task
==
null
)
{
wait();
//
如果Runnable对象实例task为空,进入等待状态
}
}
catch
(InterruptedException ex)
{}
task.run();
//
执行Runnable接口run方法
task
=
null
;
//
执行完后释放Runnable对象task
pool.free(
this
);
//
释放
}
}
public
void
run()
{
//
PoolableThread进程类run()方法
executeTasks();
}
}
//
Runnable接口类
public
class
PoolTest implements Runnable
{
public
static
ThreadPool tp
=
new
ThreadPool();
//
线程池类对象
Socket socket;
BufferedReader
in
;
PrintWriter
out
;
public
PoolTest(Socket socket)throws IOException
{
this
.socket
=
socket;
//
初始化socket
this
.
in
=
new
BufferedReader(
new
InputStreamReader(socket.getInputStream()));
//
建立输入流管道
this
.
out
=
out
=
new
PrintWriter(
new
BufferedWriter(
new
OutputStreamWriter(socket.getOutputStream())),
true
);
//
建立输出流管道
tp.runTask(
this
);
//
运行Runnable类接口
try
{Thread.sleep(
100
);}
catch
(Exception e)
{}
}
public
static
void
main(String[] args)throws IOException
{
ServerSocket s
=
new
ServerSocket(
8080
);
//
初始化服务器端口
int
i
=
0
;
System.
out
.println(
"
Server start..
"
+
s);
try
{
while
(
true
)
{Socket socket
=
s.accept();
//
无限监听客户请求
System.
out
.println(
"
Connectino accept:
"
+
socket
+
"
"
+
i);
i
++
;
try
{
new
PoolTest(socket);
//
调用Runnable接口进程
}
catch
(IOException e)
{socket.close();}
}
}
finally
{s.close();}
}
public
void
run()
{
try
{
while
(
true
)
{
String str
=
in
.readLine();
//
读取输入流
if
(str.equals(
"
end
"
))
break
;
System.
out
.println(
"
Echo ing :
"
+
str);
}
System.
out
.println(
"
Close
"
);
}
catch
(IOException e)
{}
finally
{
try
{socket.close();}
catch
(IOException e)
{}
}
}
}
//
服务器程序结束
请各位朋友指正
查看全文
相关阅读:
[恢]hdu 2116
[恢]hdu 1203
[恢]hdu 1181
[恢]hdu 1280
[恢]hdu 1250
[恢]hdu 1215
[恢]hdu 1237
[恢]hdu 1276
PowerShell yarn : 无法加载文件 C:\Users\Admin\AppData\Roaming\npm\yarn.ps1,因为在此系统因为在此系统上禁止运行脚本。
vue : 无法加载文件 C:\Users\1111111\AppData\Roaming\npm\vue.ps1,因为在此系统禁止运行脚本
原文地址:https://www.cnblogs.com/oisiv/p/217513.html
最新文章
POJ 1068, Parencodings
POJ 2965, The Pilots Brothers' refrigerator
POJ 1573, Robot Motion
POJ 2586, Y2K Accounting Bug
POJ 1753, Flip Game
POJ 2993, Emag eht htiw Em Pleh
POJ 2632, Crashing Robots
索引的优点和缺点(转)
零售系统中的临时调价如何处理(回顾)
FastReport 预览按钮屏蔽问题
热门文章
DXperience 6.3X 中文资源包源码
解决 IBM T42、T43 HLDTST RW/DVD GCC4242N 无法刻录 问题
服务器端调用客户端方法
如何恢复一个非用户sa创建的数据库,且使用原用户创建者进行访问
制造业信息化常用名词中英对照
项目管理规范(文档清单)
BOM中的提前期与工艺中提前期的区别
今天终于有机会注册菊花论坛会员了
[恢]hdu 2206
[恢]hdu 1242
Copyright © 2011-2022 走看看