如何在两个线程之间共享数据
Java里面进行多线程通信的主要方式就是共享内存的方式,共享内存主要的关注点有两个:可见性和有序性原子性。Java内存模型(JMM)解决了可见性和有序性的问题,而锁解决了原子性的问题,理想情况下我们希望做到"同步"和"互斥"。有以下常规实现方法:
将数据抽象成一个类,并将数据的操作作为这个类的方法
将数据抽象成一个类,并将对这个数据的操作作为整个类的方法,这么设计可以和容易做到同步,只要在方法上加"synchronized"
public class MyData{
private int j = 0;
public synchronized void add(){
j++;
System.out.println("线程"+Thread.currentThread().getName()+"j为:"+j);
}
public synchronized void dec(){
j--;
System.out.println("线程"+Thread.currentThread().getName()+"j为:"+j);
}
public int getData(){
return j;
}
}
public class AddRunnable implements Runnable{
MyData data;
public AddRunnable(MyData data){
this.data = data;
}
@Override
public void run(){
data.add();
}
}
public class DecRunnable implements Runnable{
MyData data;
public DecRunnable(MyData data){
this.data = data;
}
@Override
public void run(){
data.dec();
}
}
public static void main(String[] args){
MyData data = new MyData();
Runnable add = new AddRunnable(data);
Runnable dec = new DecRunnable(data);
for(int i = 0; i < 2; i++){
new Thread(add).start();
new Thread(dec).start();
}
}
Runnable对象作为一个类的内部类
将Runnable对象作为一个类的内部类,共享数据作为这个类的成员变量,每个线程对共享数据的操作方法也封装在外部类,以便实现对数据的各个操作的同步和互斥,作为内部类的各个Runnable对象调用外部类的这些方法。
public class MyData{
private int j = 0;
public synchronized void add(){
j++;
System.out.println("线程"+Thread.currentThread().getName()+"j为:"+j);
}
public synchronized void dec(){
j--;
System.out.println("线程"+Thread.currentThread().getName()+"j为:"+j);
}
public int getData(){
return j;
}
}
public class TestThread{
public static void main(String[] args){
final MyData data = new MyData();
for(int i = 0;i < 2;i++){
new Thread(new Runnable(){
public void run(){
data.add();
}
}).start();
new Thread(new Runnable(){
public void run(){
data.dec();
}
}).start();
}
}
}