Java 多线程之间共享数据
1、线程范围的共享变量
多个业务模块针对同一个static
变量的操作 要保证在不同线程中 各模块操作的是自身对应的变量对象
public class threadscopesharadata { private static int data = 0 ; public static void main(string[] args) { for(int i = 0 ;i<2 ;i++){ new thread(new runnable(){ @override public void run() { data = new random().nextint(); system.out.println(thread.currentthread().getname()+ " put random data:"+data); new a().get() ; new b().get() ; } }).start() ; } } static class a { public int get(){ system.out.println("a from " + thread.currentthread().getname() + " get data :" + data); return data ; } } static class b{ public int get(){ system.out.println("b from " + thread.currentthread().getname() + " get data :" + data); return data ; } } }
模块a ,b都需要访问static
的变量data 在线程0中会随机生成一个data值 假设为10 那么此时模块a和模块b在线程0中得到的data
的值为10 ;在线程1中 假设会为data赋值为20 那么在当前线程下
模块a和模块b得到data的值应该为20
看程序执行的结果:
thread-0 put random data:-2009009251
thread-1 put random data:-2009009251
a from thread-0 get data :-2009009251
a from thread-1 get data :-2009009251
b from thread-0 get data :-2009009251
b from thread-1 get data :-2009009251
thread-0 put random data:-2045829602
thread-1 put random data:-1842611697
a from thread-0 get data :-1842611697
a from thread-1 get data :-1842611697
b from thread-0 get data :-1842611697
b from thread-1 get data :-1842611697
会出现两种情况:
- 1.由于线程执行速度,新的随机值将就的随机值覆盖 data 值一样
- 2.data 值不一样,但 a、b线程都
2、使用map实现线程范围内数据的共享
可是将data数据和当前允许的线程绑定在一块,在模块a和模块b去获取数据data的时候 是通过当前所属的线程去取得data的结果就行了。
声明一个map集合 集合的key为thread 存储当前所属线程 value 保存data的值,
代码如下:
public class threadscopesharadata { private static map<thread, integer> threaddata = new hashmap<>(); public static void main(string[] args) { for (int i = 0; i < 2; i++) { new thread(new runnable() { @override public void run() { int data = new random().nextint(); system.out.println(thread.currentthread().getname() + " put random data:" + data); threaddata.put(thread.currentthread(), data); new a().get(); new b().get(); } }).start(); } } static class a { public void get() { int data = threaddata.get(thread.currentthread()); system.out.println("a from " + thread.currentthread().getname() + " get data:" + data); } } static class b { public void get() { int data = threaddata.get(thread.currentthread()); system.out.println("b from " + thread.currentthread().getname() + " get data:" + data); } } }
thread-0 put random data:-123490895
thread-1 put random data:-1060992440
a from thread-0 get data:-123490895
a from thread-1 get data:-1060992440
b from thread-0 get data:-123490895
b from thread-1 get data:-1060992440
3、threadlocal实现线程范围内数据的共享
(1)订单处理包含一系列操作:减少库存量、增加一条流水台账、修改总账,这几个操作要在同一个事务中完成,通常也即同一个线程中进行处理,如果累加公司应收款的操作失败了,则应该把前面的操作回滚,否则,提交所有操作,这要求这些操作使用相同的数据库连接对象,而这些操作的代码分别位于不同的模块类中。
(2)银行转账包含一系列操作: 把转出帐户的余额减少,把转入帐户的余额增加,这两个操作要在同一个事务中完成,它们必须使用相同的数据库连接对象,转入和转出操作的代码分别是两个不同的帐户对象的方法。
(3)例如strut2
的actioncontext
,同一段代码被不同的线程调用运行时,该代码操作的数据是每个线程各自的状态和数据,对于不同的线程来说,getcontext
方法拿到的对象都不相同,对同一个线程来说,不管调用getcontext
方法多少次和在哪个模块中getcontext
方法,拿到的都是同一个。
(4)实验案例:定义一个全局共享的threadlocal
变量,然后启动多个线程向该threadlocal变量中存储一个随机值,接着各个线程调用另外其他多个类的方法,这多个类的方法中读取这个threadlocal
变量的值,就可以看到多个类在同一个线程*享同一份数据。
(5)实现对threadlocal
变量的封装,让外界不要直接操作threadlocal
变量。
- 对基本类型的数据的封装,这种应用相对很少见。
- 对对象类型的数据的封装,比较常见,即让某个类针对不同线程分别创建一个独立的实例对象。
public class threadlocaltest { private static threadlocal<integer> threadlocal = new threadlocal<>(); public static void main(string[] args) { for (int i = 0; i < 2; i++) { new thread(new runnable() { @override public void run() { int data = new random().nextint(); system.out.println(thread.currentthread().getname() + " put random data:" + data); threadlocal.set(data); new a().get(); new b().get(); } }).start(); } } static class a { public void get() { int data = threadlocal.get(); system.out.println("a from " + thread.currentthread().getname() + " get data:" + data); } } static class b { public void get() { int data = threadlocal.get(); system.out.println("b from " + thread.currentthread().getname() + " get data:" + data); } } }
thread-0 put random data:-2015900409
thread-1 put random data:-645411160
a from thread-0 get data:-2015900409
a from thread-1 get data:-645411160
b from thread-0 get data:-2015900409
b from thread-1 get data:-645411160
4、优化
public class threadlocaltest { private static threadlocal<integer> threadlocal = new threadlocal<>(); //private static threadlocal<mythreadscopedata> mythreadscopedatathreadlocal = new threadlocal<>(); public static void main(string[] args) { for (int i = 0; i < 2; i++) { new thread(new runnable() { @override public void run() { int data = new random().nextint(); system.out.println(thread.currentthread().getname() + " put random data:" + data); threadlocal.set(data); // mythreadscopedata mythreadscopedata = new mythreadscopedata(); // mythreadscopedata.setname("name" + data); // mythreadscopedata.setage(data); // mythreadscopedatathreadlocal.set(mythreadscopedata); //获取与当前线程绑定的实例并设置值 mythreadscopedata.getthreadinstance().setname("name" + data); mythreadscopedata.getthreadinstance().setage(data); new a().get(); new b().get(); } }).start(); } } static class a { public void get() { int data = threadlocal.get(); // mythreadscopedata mydata = mythreadscopedatathreadlocal.get(); // // // system.out.println("a from " + thread.currentthread().getname() // + " getmydata: " + mydata.getname() + "," + mydata.getage()); 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 = threadlocal.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 static threadlocal<mythreadscopedata> map = new threadlocal<>(); private string name; private int age; private mythreadscopedata() { } //定义一个静态方法,返回各线程自己的实例 //这里不必用同步,因为每个线程都要创建自己的实例,所以没有线程安全问题。 public static mythreadscopedata getthreadinstance() { //获取当前线程绑定的实例 mythreadscopedata instance = map.get(); if (instance == null) { instance = new mythreadscopedata(); map.set(instance); } return instance; } 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; } }
thread-1 put random data:-1041517189
thread-0 put random data:-98835751
a from thread-1 getmydata: name-1041517189,-1041517189
a from thread-0 getmydata: name-98835751,-98835751
b from thread-1 getmydata: name-1041517189,-1041517189
b from thread-0 getmydata: name-98835751,-98835751
5、实例
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1,写出程序。
(1)如果每个线程执行的代码相同,可以使用同一个runnable
对象,这个runnable对象中有那个共享数据,例如,卖票系统就可以这么做。
public class sellticket { //卖票系统,多个窗口的处理逻辑是相同的 public static void main(string[] args) { ticket t = new ticket(); new thread(t).start(); new thread(t).start(); } } /** * 将属性和处理逻辑,封装在一个类中 * * @author yang */ class ticket implements runnable { private int ticket = 10; public synchronized void run() { while (ticket > 0) { ticket--; system.out.println("当前票数为:" + ticket); } } }
(2)如果每个线程执行的代码不同,这时候需要用不同的runnable
对象,例如,设计2个线程。一个线程对j增加1,另外一个线程对j减1,银行存取款系统。
public class multithreadsharedata { private int j; public static void main(string[] args) { multithreadsharedata multithreadsharedata = new multithreadsharedata(); for(int i=0;i<2;i++){ new thread(multithreadsharedata.new sharedata1()).start();//增加 new thread(multithreadsharedata.new sharedata2()).start();//减少 } } //自增 private synchronized void inc(){ j++; system.out.println(thread.currentthread().getname()+" inc "+j); } //自减 private synchronized void dec(){ j--; system.out.println(thread.currentthread().getname()+" dec "+j); } class sharedata1 implements runnable { public void run() { for(int i=0;i<5;i++){ inc(); } } } class sharedata2 implements runnable { public void run() { for(int i=0;i<5;i++){ dec(); } } } }
thread-0 inc 1
thread-0 inc 2
thread-0 inc 3
thread-0 inc 4
thread-0 inc 5
thread-1 dec 4
thread-1 dec 3
thread-2 inc 4
thread-2 inc 5
thread-2 inc 6
thread-2 inc 7
thread-2 inc 8
thread-1 dec 7
thread-1 dec 6
thread-1 dec 5
thread-3 dec 4
thread-3 dec 3
thread-3 dec 2
thread-3 dec 1
thread-3 dec 0
到此这篇关于java 多线程之间共享数据的文章就介绍到这了,更多相关java 多线程共享数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 用ps制作漂亮的心形云彩
下一篇: C++中 set的用法