- Immutable
-
Multithreading shares a resource, and the resource itself is immutable, so it can be safely shared.
- Applicable environment
- Sample Example
- 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