场景:
有三个小孩儿,买了一个变形金刚玩具(Transformer)....
三个小孩都争着玩这个玩具....没有一个人可以玩...
第一种方式:每个人各玩一会....
第二种方式:再买两个玩具,一个人玩一个.
每个小孩都当成一个线程.三个小孩就有三个线程.
Child.java
2 3 import java.util.Random; 4 import java.util.concurrent.TimeUnit; 5 6 public class Child implements Runnable{ 7 private Transformer2 transformer; 8 9 private String name ; 10 11 12 public Child(Transformer2 transformer) { 13 this.transformer = transformer; 14 } 15 16 public Child() { 17 } 18 19 public Transformer2 getTransformer() { 20 return transformer; 21 } 22 23 public void setTransformer(Transformer2 transformer) { 24 this.transformer = transformer; 25 } 26 27 public String getName() { 28 return name; 29 } 30 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 36 public void play(){ 37 38 39 while (true) { 40 try { 41 //transformer = new Transformer();//每次调用该方法,该对象都是新的。 42 43 //想要的效果是每个小孩儿都有一个玩具,但是不管他玩多少次,这个玩具永远都是唯一的. 44 transformer = Transformer2.getInstance(); 45 System.out.println(Thread.currentThread().getName() +" :"+transformer ); 46 String str = "擎天柱" + new Random().nextInt(10); 47 System.out.println(Thread.currentThread().getName() 48 + ": 将变形金刚设置成 :" + str); 49 transformer.setType(str); 50 TimeUnit.SECONDS.sleep(2); 51 52 String type = transformer.getType(); 53 54 System.out.println(Thread.currentThread().getName() 55 + ": 获取到的变形金刚是:" + type); 56 57 } catch (InterruptedException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 } 63 64 @Override 65 public void run() { 66 play(); 67 } 68 69 }
Transformer.java
2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 7 /* 8 * 每个线程都有且仅有一个 该类对象的引用。 9 * 不应该在外部类中随便的去new 所以构造方法私有 10 * 11 */ 12 public class Transformer { 13 14 private static Map<Thread, Transformer> maps = new HashMap<Thread, Transformer>(); 15 16 private String type; 17 18 private Transformer(){} 19 20 public static Transformer getInstance() { 21 Transformer instance = maps.get(Thread.currentThread()); 22 if(instance == null) { 23 instance = new Transformer(); 24 maps.put(Thread.currentThread(),instance); 25 } 26 return instance; 27 } 28 public String getType() { 29 return type; 30 } 31 32 public void setType(String type) { 33 this.type = type; 34 } 35 36 37 }
Transformer2.java(使用ThreadLocal类)
2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 7 /* 8 * 每个线程都有且仅有一个 该类对象的引用。 9 * 10 */ 11 public class Transformer2 { 12 13 //private static Map<Thread, Transformer2> maps = new HashMap<Thread, Transformer2>(); 14 private static ThreadLocal<Transformer2> maps = new ThreadLocal<Transformer2>(); 15 16 private String type; 17 18 19 private Transformer2(){} 20 21 public static Transformer2 getInstance() { 22 Transformer2 instance = maps.get(); 23 if(instance == null) { 24 instance = new Transformer2(); 25 maps.set(instance); 26 } 27 return instance; 28 } 29 public String getType() { 30 return type; 31 } 32 33 public void setType(String type) { 34 this.type = type; 35 } 36 }
测试类:
2 3 public class Test { 4 5 public static void main(String[] args) { 6 7 //Transformer transformer = new Transformer(); 8 9 for (int i = 0; i < 3; i++) { 10 new Thread(new Child()).start(); 11 } 12 } 13 }
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/6189267.html,如需转载请自行联系原作者