Java Multithreading (quad) mode-immutable

Source: Internet
Author: User

  • Immutable
    • Multithreading shares a resource, and the resource itself is immutable, so it can be safely shared.

    • Applicable environment
      • When sharing resources that do not require dynamic changes, such as some fixed calculation parameters.

    • Sample Example
      • The postman looks at the house number: a group of postman.

    • Number
    • Package Immutable;public class Housenumber {private final String number= ' 12345678 ';p ublic string GetNumber () {return this . number;}}

    • Postman
    • Package Immutable;public class Messager implements Runnable {private housenumber number=null;private String myname=null; Public Messager (String name,housenumber number) {This.myname=name;this.number=number;} @Overridepublic void Run () {while (true) {System.out.println (this.myname+ ":" +this.number.getnumber ())}}}

    • Test class
    • Package Immutable;public class Test {public static void main (string[] args) {housenumber hn=new housenumber (); Messager m1=new messager ("M1", HN); Messager m2=new messager ("m2", hn); Messager m3=new messager ("M3", HN); Thread T1=new thread (m1); Thread t2=new thread (m2); Thread t3=new thread (m3); T1.start (); T2.start (); T3.start ();}}

This article is from the Java Technology Stack notes blog, so be sure to keep this source http://stroll.blog.51cto.com/11038467/1855348

Java Multithreading (quad) mode-immutable

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.