Java multithreaded design mode detailed learning notes nine--thread-per-message__java

Source: Internet
Author: User
public class Main {public

	static void Main (string[] args) {
		System.out.println ("Main Begin");
		Host host = new host ();
		Host.request ("A");
		Host.request (' B ');
		Host.request (' C ');
		System.out.println ("Main end");
	}
public class Host {

	private helper helper = new Helper ();
	
	public void request (final int count, final char c) {
		System.out.println ("    Request" + Count +, "+ C +") Begin );
		New Thread (New Runnable () {
			@Override public
			void Run () {
				Helper.handle (count, c);
			}
		}). Start ();
		System.out.println ("    + count +", "+ C +") end);
	}
public class Helper {public

	void handle (int count, char c) {
		System.out.println ("    Handle" ("+ Count +", "+ C + ") Begin");
		for (int i = 0; i < count; i++) {
			slowly ();
			System.out.print (c);
		}
		System.out.println ("");
		System.out.println ("    Handle (" + Count + "," + C + ") end");

	private void Slowly () {
		try {
			thread.sleep ();
		} catch (Interruptedexception e) {
			//TODO Auto-generated Catch block
			e.printstacktrace ();}}
Main Begin
Request (10,a) Begin
Request (10,a) End
Request (20,b) Begin
Request (20,b) End
Request (30,c) Begin
Handle (10,a) Begin
Request (30,c) End
Main End
Handle (30,c) Begin
Handle (20,b) Begin
Cbacbacabbcabacbacbcacbacabba
Handle (10,a) End
Ccbcbcbcbcbbccbcbcbb
Handle (20,b) End
Ccccccccccc
Handle (30,c) End


Thread-per-message explained that "one thread per message", "End of Message delegate" and "End of execution message" would be different threads, that is, the thread that delegates the message to the thread that executes the message. "This job is yours."

By printing the information, you will find that the request method is finished before the handle method is finished. The main way of the main method, after calling the host's request method, immediately ended, send out "help show a Ah" "To help show the B Ah" "Help show the C ah" these instructions, their own end. No matter how time-consuming the handle method is, it does not affect the responsiveness of the request method. The request method does not wait for the handle method to finish executing, and then exits immediately.

Improve responsiveness and reduce latency
When using Thread-per-message, a new thread is started in the host participant because the boot thread takes some time, so for thread-per-message pattern, which is intended to enhance responsiveness, " The handle's operation takes time "and the time it takes to start a thread" is the relationship between the fish and the bear's paw. To reduce the time it takes to start a thread, you can use worker thread pattern. Thread-per-message pattern starts a thread each time a request is made, and Worker thread pattern initiates a sufficient number of threads in advance, reusing these threads, and lifting the execution performance of the program.

Scope of Use:
Suitable for use when the order of operation is irrelevant
The order in which the handle method executes is not necessarily the order in which the request method is invoked. So when the order of operation is meaningful, it is not suitable to use thread-per-message pattern.
When you don't need to return a value
In thread-per-message pattern, the request method does not wait for the handle method to finish, meaning that the request method does not get the return value of the handle method. So thread-per-message pattern can only be used when no return value is needed, and you need to know that future pattern can be used when processing results.
Application in the production of the server
In order for the server to handle the majority of requests, you can use thread-per-message pattern, a client-delivered request, to be received by the main thread. When the request is actually processed, the other thread is responsible, and the main thread of the server returns to the state of waiting for the other client's request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.