这个模式是每发来一个请求就分配一个线程,让这个线程去执行工作。委托消息的一端与执行消息的一端是不同的线程。示例中Host类是一个接受请求并构造和启动线程的类。
public class Host { private final Helper helper=new Helper(); public void request(final int count,final char c){ System.out.println(" request("+count+","+c+") Begin"); new Thread(){ //每次创建一个线程来执行helper的handle方法 public void run(){ helper.handle(count,c); } }.start(); System.out.println(" request("+count+","+c+") End"); } }
public class Helper { //注意:没有继承Thread public void handle(int count,char c){ System.out.println(" handle("+count+","+c+") Begin"); for(int i=0;i<count;i++){ try { slowly(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.print(c); } System.out.println(""); System.out.println(" handle("+count+","+c+") End"); } private void slowly() throws InterruptedException { Thread.sleep(100); } }
public class Main { public static void main(String[] args){ System.out.println("Main Begins"); Host host=new Host(); host.request(10, 'A'); host.request(20, 'B'); host.request(30, 'C'); System.out.println("Main Ends"); } }
执行结果:
Main Begins
request(10,A) Begin
request(10,A) End
request(20,B) Begin
handle(10,A) Begin
request(20,B) End
request(30,C) Begin
handle(20,B) Begin
request(30,C) End
Main Ends
handle(30,C) Begin
BCABCACABBCACABCABCABCABACBAC
handle(10,A) End
BBCCBCBCBCBCBCBCBCBBC
handle(20,B) End
CCCCCCCCCC
handle(30,C) End
这种模式中,首先request方法是不需要返回值的,如果需要返回值,参考Future Pattern。
他通过调用方法以及启动线程来传递消息的。这种模式可以应用在服务器的制作上,首先客户端发送一个请求,有主线程来接受,主线程把具体的请求给别的线程执行,而自己继续等待其他客户端的请求。
import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; public class Service { private Service(){} public static void serve(Socket s) throws IOException, InterruptedException{ DataOutputStream out=new DataOutputStream(s.getOutputStream()); out.writeBytes("HTTP/1.0 200 OK \r\n"); out.writeBytes("Content-type:text/html\r\n"); out.writeBytes("\r\n"); out.writeBytes("<html><head><title>CountDown</title></head><body>"); out.writeBytes("<h1>Countdown start</h1>"); for(int i=10;i>=0;i--){ out.writeBytes("<h2>"+i+"</h2>"); out.flush(); Thread.sleep(1000); } out.writeBytes("</body></html>"); s.close(); } }
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class MiniServer { private int portnumber;//端口号 public MiniServer(int portnumber){ this.portnumber=portnumber; } public void execute() throws IOException, InterruptedException{ ServerSocket server=new ServerSocket(portnumber); while(true){ Socket s=server.accept(); System.out.println("connected to "+s); Service.serve(s); } } }
import java.io.IOException; public class Main { public static void main(String[] arg) throws IOException, InterruptedException{ new MiniServer(8888).execute(); } }
在浏览器中输入:http://127.0.0.1:8888/执行
相关推荐
在Java多线程编程中,Thread-per-Message模式是一种常见的并发处理策略。这种模式的核心思想是为每个消息或任务创建一个新的线程来处理,使得消息的发送者和处理者不在同一个线程上下文中运行,从而实现任务的异步...
throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); } ``` 这里的`sThreadLocal`是一个`ThreadLocal`类型的变量,用于存储每个线程对应...
throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(true)); // Looper.loop() while (mRun) { Message msg = mQueue.next(); // blocks until ...
7、Thread-Per-Message ———— 这个工作交给你了 8、Worker Thread ———— 等到工作来,来了就工作 9、Future ———— 先给您这张提货单 10、Two-Phase Termination ———— 快把玩具收拾好,去睡觉吧 11、...
User defined properties such as CPU, memory logging, thread statistics per process/thread Extensible activity, sink, error listeners for pre, post event processing Granular context such as thread...
第7章 read-Per-Message——这个工作交给你了 第8章 Worker Thread——等到工作来,来了就工作 第9章 Future——先给您这张提货单 第10章 Two-Phase Termination——快把玩具收拾好,去睡觉吧 第11章 Thread-...
SMTP server will relay the message on to the SMTP server of the recipient(s) to eventually be acquired by the user(s) through POP or IMAP. This does not require your SMTP server to be an open relay,...
Multi-thread-able and parallel-able usage. Pages should be linked together so you can do range queries by going to the next page easily. The MGIndex MGIndex takes the best features of a b+tree and ...
你来用 第6章 Read-Write Lock——大家想看就看吧,不过看的时候不能写喔 第7章 read-Per-Message——这个工作交给你了 第8章 Worker Thread——等到工作来,来了就工作 第9章 Future——先给您这张提货单 第10章 ...
#什么是批量发布者 ...require_param -m (integer) #message count that number of per thread. options -t (integer) #thread count. (default 5) -P (string) #pid file path. ## MEMO 线程数 (-t) * 消息数
Unix网络编程卷1,第三版,英文版。大名顶顶的Richard Stevens所写 目录: ... Addison-Wesley Professional Computing Series Foreword Preface Introduction Changes from the Second Edition ...
15.7 Message Queues 561 15.8 Semaphores 565 15.9 Shared Memor y 571 15.10 POSIX Semaphores 579 15.11 Client–Server Proper ties 585 15.12 Summary 587 Chapter 16. Network IPC: Sockets 589 16.1 ...
- **特点**: 提供了一种称为one loop per thread(每个线程一个事件循环)的多线程编程模型,简化了多线程网络服务器的开发过程。 #### 2. one loop per thread模型详解 - **概念**: 在该模型中,每个线程都有自己...
Fixed a minor bug with the boundary.- Change the generator of the message id.- Added the field MessageId and InReplyTo to the TSakMsg component.- Added the field In-Reply-To that is added to the ...
28)..Fixed: Irnored exceptions (via per-exception/events) now bring up default RTL handler 29)..Fixed: Format error in Viewer 30)..Fixed: Leak of EurekaLog exception information object 31)..Fixed: ...
│ 高并发编程第二阶段33讲、多线程Thread-Per-Message设计模式.mp4 │ 高并发编程第二阶段34讲、多线程Two Phase Termination设计模式-上.mp4 │ 高并发编程第二阶段35讲、多线程Two Phase Termination设计模式-...
│ 高并发编程第二阶段33讲、多线程Thread-Per-Message设计模式.mp4 │ 高并发编程第二阶段34讲、多线程Two Phase Termination设计模式-上.mp4 │ 高并发编程第二阶段35讲、多线程Two Phase Termination设计模式-...