做企业网站需要招标公告上面的技术参数写什么,wordpress excel搜索,免费的行情软件网站下载不用下载,电子商务网站建设和管理的意义Thread-Per-Message是为每一个消息的处理开辟一个线程#xff0c;以并发方式处理#xff0c;提高系统整体的吞吐量。这种模式再日常开发中非常常见#xff0c;为了避免线程的频繁创建和销毁#xff0c;可以使用线程池来代替。 示例代码如下#xff1a;
public class Requ… Thread-Per-Message是为每一个消息的处理开辟一个线程以并发方式处理提高系统整体的吞吐量。这种模式再日常开发中非常常见为了避免线程的频繁创建和销毁可以使用线程池来代替。 示例代码如下
public class Request {
private String business;public Request(String business) {
this.businessbusiness;
}
public String toString() {
return this.business;
}
}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;public class TaskHandler implements Runnable{
private Request request;public TaskHandler(Request request) {
this.requestrequest;
}private void slowly() {
try {
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}Override
public void run() {
System.out.println(Begin handle request);
slowly();
System.out.println(End handle request);
}}
import org.multithread.threadpool.BaseThreadPool;
import org.multithread.threadpool.ThreadPool;public class Operator {
private final ThreadPool threadPoolnew BaseThreadPool(2,6,4,100);public void call(String business) {
TaskHandler taskHandlernew TaskHandler(new Request(business));
threadPool.execute(taskHandler);
}}
public class TPMtest {
public static void main(String[] args) {
Operator opnew Operator();
String rootRoot-Request-;
for(int i0;i30;i) {
op.call(rooti);
}
}
}
运行结果
Begin handle Root-Request-0
Begin handle Root-Request-1
End handle Root-Request-1
Begin handle Root-Request-2
End handle Root-Request-0
Begin handle Root-Request-3
End handle Root-Request-3
Begin handle Root-Request-4
End handle Root-Request-2
Begin handle Root-Request-5
Begin handle Root-Request-6
Begin handle Root-Request-7
End handle Root-Request-7
Begin handle Root-Request-8
End handle Root-Request-4
Begin handle Root-Request-9
End handle Root-Request-6
Begin handle Root-Request-10
End handle Root-Request-5
Begin handle Root-Request-11
End handle Root-Request-8
Begin handle Root-Request-12
Begin handle Root-Request-13
End handle Root-Request-10
Begin handle Root-Request-14
End handle Root-Request-9
Begin handle Root-Request-15
End handle Root-Request-11
Begin handle Root-Request-16
End handle Root-Request-15
Begin handle Root-Request-17
End handle Root-Request-14
Begin handle Root-Request-18
End handle Root-Request-18
Begin handle Root-Request-19
End handle Root-Request-12
Begin handle Root-Request-20
End handle Root-Request-20
Begin handle Root-Request-21
End handle Root-Request-16
Begin handle Root-Request-22
End handle Root-Request-22
Begin handle Root-Request-23
End handle Root-Request-23
Begin handle Root-Request-24
End handle Root-Request-13
Begin handle Root-Request-25
End handle Root-Request-19
Begin handle Root-Request-26
End handle Root-Request-26
Begin handle Root-Request-27
End handle Root-Request-27
Begin handle Root-Request-28
Begin handle Root-Request-29
End handle Root-Request-25
End handle Root-Request-17
End handle Root-Request-21
End handle Root-Request-24
End handle Root-Request-28
End handle Root-Request-29