线程范围内数据共享

用处:处理数据库中事务管理器较为常用,所有的操作都是针对同一份数据。系统角度进行开发编程。

 

第一种方法,这种方法比较粗糙

public class ThreadScopeShareData {

       

       private static int data = 0;

       private static Map<Thread , Integer> threadData = new HashMap<Thread,Integer>();

       public static void main(String[] args){

              for (int i = 0; i < 2; i++) {

                     new Thread(new Runnable(){

                            public void run(){

                                   data = new Random().nextInt();

                                   System.out.println(Thread.currentThread().getName()

                                                 + " has put data :"+data);

                                   threadData.put(Thread.currentThread(),data);

                                   new A().get();

                                   new B().get();

                            }

                     }).start();

              }

       }

       

       static class A{

              public void get(){

                     data = threadData.get(Thread.currentThread());

                     System.out.println("A from "+ Thread.currentThread().getName()+" get data :"+data);

              }

       }

       

       static class B{

              public void get(){

                     data = threadData.get(Thread.currentThread());

                     System.out.println("B from "+ Thread.currentThread().getName()+" get data :"+data);

              }

       }

}


 

 

 

 

 

 

第二种方法:ThreadLocal,该类相当于一个Map

public class ThreadLocalTest {

       

       static final ThreadLocal<Integer> x = new ThreadLocal<Integer>();

       public static void main(String[] args){

              for (int i = 0; i < 2; i++) {

                     new Thread(new Runnable(){

                            public void run(){

                                   int data = new Random().nextInt()/10000000;

                                   System.out.println(Thread.currentThread().getName()

                                                 + " has put data :"+data);

                                   x.set(data);

                                   new A().get();

                                   new B().get();

                            }

                     }).start();

              }

       }

       

       static class A{

              public void get(){

                     int data = x.get();

                     System.out.println("A from "+ Thread.currentThread().getName()+" get data :"+data);

              }

       }

       

       static class B{

              public void get(){

                     int data = x.get();

                     System.out.println("B from "+ Thread.currentThread().getName()+" get data :"+data);

              }

       }

}


 

第三种方法:操作多个数据,不单单是基本数据类型。可以整合成一个实体对象,然后往ThreadLocalset这个对象。

public class ThreadLocalTest {

       

       private static final ThreadLocal<Integer> x = new ThreadLocal<Integer>();

       private static final ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();

       

       public static void main(String[] args){

              for (int i = 0; i < 2; i++) {

                     new Thread(new Runnable(){

                            public void run(){

                                   int data = new Random().nextInt()/10000000;

                                   System.out.println(Thread.currentThread().getName()

                                                 + " has put data :"+data);

                                   x.set(data);

                                   MyThreadScopeData myData = new MyThreadScopeData();

                                   myData.setName(" name "+data);

                                   myData.setAge(data);

                                   myThreadScopeData.set(myData);

                                   new A().get();

                                   new B().get();

                            }

                     }).start();

              }

       }

       

       static class A{

              public void get(){

                     int data = x.get();

                     System.out.println("A from "+ Thread.currentThread().getName()+" get data :"+data);

                     MyThreadScopeData myData = myThreadScopeData.get();

                     System.out.println("A from "+ Thread.currentThread().getName()+" getMydata :"+myData.getName()+","+myData.getAge());

              }

       }

       

       static class B{

              public void get(){

                     int data = x.get();

                     System.out.println("B from "+ Thread.currentThread().getName()+" get data :"+data);

                     MyThreadScopeData myData = myThreadScopeData.get();

                     System.out.println("B from "+ Thread.currentThread().getName()+" getMydata :"+myData.getName()+","+myData.getAge());

              }

       }

}

 

class MyThreadScopeData{

       private String name;

       private int age;

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public int getAge() {

              return age;

       }

       public void setAge(int age) {

              this.age = age;

       }

       

}


 

第四种方式:很优雅的处理办法,仿单例模式处理。ThradLocal类放到实体类的背后了,设计也交给了实体类,调用者不需要关心实体类的设计,只需要get到实体类的实例对象即可,同一线程内,任何地方调用实体类的实例对象,都是同一个实例对象,操作的数据都是同一份,也就达到了线程内数据共享的效果。该处理方法很好的体现了面向对象的思想。

strust2也是如此的设计方式。

public class ThreadLocalTest {

       

       private static final ThreadLocal<Integer> x = new ThreadLocal<Integer>();

       private static final ThreadLocal<MyThreadScopeData> myThreadScopeData = new ThreadLocal<MyThreadScopeData>();

       

       public static void main(String[] args){

              for (int i = 0; i < 2; i++) {

                     new Thread(new Runnable(){

                            public void run(){

                                   int data = new Random().nextInt()/10000000;

                                   System.out.println(Thread.currentThread().getName()

                                                 + " has put data :"+data);

                                   x.set(data);

                                   MyThreadScopeData.getThreadInstance().setName(" name "+data);

                                   MyThreadScopeData.getThreadInstance().setAge(data);

                                   new A().get();

                                   new B().get();

                            }

                     }).start();

              }

       }

       

       static class A{

              public void get(){

                     int data = x.get();

                     System.out.println("A from "+ Thread.currentThread().getName()+" get data :"+data);

                     MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();

                     System.out.println("A from "+ Thread.currentThread().getName()+" getMydata :"+myData.getName()+","+myData.getAge());

              }

       }

       

       static class B{

              public void get(){

                     int data = x.get();

                     System.out.println("B from "+ Thread.currentThread().getName()+" get data :"+data);

                     MyThreadScopeData myData = MyThreadScopeData.getThreadInstance();

                     System.out.println("B from "+ Thread.currentThread().getName()+" getMydata :"+myData.getName()+","+myData.getAge());

              }

       }

}

 

class MyThreadScopeData{

       private MyThreadScopeData(){};

       

       private static ThreadLocal<MyThreadScopeData> map = new ThreadLocal<MyThreadScopeData>();

       

       public static MyThreadScopeData getThreadInstance(){

              MyThreadScopeData instance = map.get();

              if(instance == null){

                     instance = new MyThreadScopeData();

                     map.set(instance);

              }

              return instance;

       }

       

       private String name;

       private int age;

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public int getAge() {

              return age;

       }

       public void setAge(int age) {

              this.age = age;

       }

       

}


 

使用ThreadLocal类:

       类似于一种栈机制,不同的线程在其有不同的记录,当时间一久,越积越多,提供clean()方法供清理。当然其内部可以自动进行回收,不用担心如果自己不处理会溢出问题。线程一完成,则会自动回收对应的实例对象的引用。如果不同的ThreadLocal对象中,同一线程有记录,则当该线程处理完后,这些不同的ThreadLocal对象中的记录都会被回收,除非该线程对应的实例对象有其他的线程所关联操作。详细查询jdk文档。

 

提问:如何控制线程死亡状态?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值