服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java多线程Thread-per-Message模式详解

java多线程Thread-per-Message模式详解

2021-05-05 11:48小明是个程序猿 Java教程

本篇文章给大家分享java多线程Thread-per-Message模式的相关知识点,对此有兴趣的朋友参考学习下。

thread-per-message模式(这项工作就交给你了)

当你很忙碌的时候,这个时候公司楼下有个快递,于是你委托你的同事帮你拿一下你的快递,这样你就可以继续做自己的工作了

在thread-per-message模式中,消息的委托端和执行端是不同的线程,消息的委托端会告诉执行端线程,这个工作就交给你了

java多线程Thread-per-Message模式详解

host类:

针对请求创建线程的类,主要通过开启新的线程,调用helper的handle,并将要打印的文字传递。

java" id="highlighter_857044">
?
1
2
3
4
5
6
7
8
9
10
11
12
public class host {
private final helper helper = new helper();
public void request(final int count,final char c){
system.out.println("request开始");
new thread(){
public void run(){
helper.handle(count, c);
}
}.start();
system.out.println("request结束");
}
}

helper类:

提供字符显示的功能,slowly方法模拟打印耗时

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class helper {
public void handle(int count ,char c){
system.out.println("handle方法开始");
for(int i=0;i<count;i++){
slowly();
system.out.print(c);
}
system.out.println("");
system.out.println("handle方法结束");
}
private void slowly(){
try {
thread.sleep(100);
} catch (interruptedexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}

main类:

创建host的实例,并调用request的方法

?
1
2
3
4
5
6
7
8
public static void main(string[] args) {
system.out.println("main begin");
host host = new host();
host.request(10, 'a');
host.request(20, 'b');
host.request(30, 'c');
system.out.println("main end");
}

测试结果:

main begin

request方法开始了

request方法结束

request方法开始了

request方法结束

request方法开始了

request方法结束

main end

handle方法开始

handle方法开始

handle方法开始

bacbacacbacbacbacbacbacbacba

handle方法结束

cbcbcbcbcbcbcbcbcbcbcb

handle方法结束

cccccccccc

handle方法结束

从运行的结果可以看出,request方法,并没有等待handle方法执行结束后再执行,而是调用handle方法后就返回到request方法中,直到运行结束,所以相当于request方法将所要进行的打印一定数量字符的工作转交给了handle方法,而request方法则可以再执行笨方法中的其他的语句,不必等待handle方法完成。这也同时告诉我们,当某些工作比较耗时时,则可以通过这种模式启动新的线程来执行处理。可以将此模式应用于服务器,这样就可以减少服务器的响应时间。

讲解一下进程和线程:

线程和进程最大的区别就是内存是否共存。

每个进程有自己的独立的内存空间,一个进程不可以擅自读取和写入其他的进程的内存,由于进程的内存空间是彼此独立的,所以一个进程无需担心被其他的进程所破坏。

线程之间是可以共存的,一个线程向实例中写入内容,其他线程就可以读取该实例的内容,由于多个线程可以访问同一个实例,我们就需要保证其正确执行互斥处理。

host设计优化:

1.使用java.util.concurrent包下的threadfactory接口设计host类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class host {
public void request(final int count,final char c){
system.out.println("request方法开始了");
threadfactory.newthread(
new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
 }
).start();;
system.out.println("request方法结束");
}
}

对应的host实例化对象:

host host = new host(executors.defaultthreadfactory());

这样设计的优势在于,原来的使用new创建的实例代码依赖于java.lang.thread类,无法控制创建线程的部分,可复用性较低,假如使用threadfactory来保存对应类的对象,调用newthread方法创建新的线程,这样便实现了线程的创建,这样不再依赖于thread类,而是取决于构造函数中传入的threadfactory对象,实现了控制线程创建的细节。

使用java.util.concurrent.executor接口重新设计host类:

前面的threadfactory接口隐藏了线程创建的细节,但是并未隐藏线程创建的操作,如果使用executor接口,那么线程创建的操作也会被隐藏起来

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class host{
private final helper helper = new helper();
private final executor executor;
public host(executor executor){
this.executor = executor;
}
public void request(final int count,final char c){
system.out.println("request方法开始了");
executor.execute(new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
});
system.out.println("request方法结束");
}
}

使用java.util.concurrent.scheduledexecutorservice类创建,其可以实现调度运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class host{
private final helper helper = new helper();
private final scheduledexecutorservice scheduledexecutorservice;
public host(scheduledexecutorservice scheduledexecutorservice){
this.scheduledexecutorservice = scheduledexecutorservice;
}
public void request(final int count,final char c){
system.out.println("request方法开始了");
scheduledexecutorservice.schedule(new runnable() {
@override
public void run() {
// todo auto-generated method stub
helper.handle(count, c);
}
}, 3l, timeunit.seconds);
system.out.println("request方法结束");
}
}

测试主函数入口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
scheduledexecutorservice scheduledexecutorservice = executors.newscheduledthreadpool(5);
host host = new host(
scheduledexecutorservice
);
try {
host.request(10, 'a');
host.request(20, 'b');
host.request(30, 'c');
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
scheduledexecutorservice.shutdown();
system.out.println("main end");
}

总结

client 角色调用host角色的request方法发来的请求,该请求的实际处理则交给helper的handle去执行,然而,如果client直接从request中调用handle方法,那么直到实际操作结束之前,都无法从handle方法返回(request返回),这样一来request的响应性能就下降了,因此,host角色会启动用于处理来自client角色请求的新线程,并让该线程来调用handle,这样一来发出请求的线程便可以立即从handle中返回。这就是thread-per-message模式。

原文链接:https://blog.csdn.net/qq_31350373/article/details/80454306

延伸 · 阅读

精彩推荐