首页 > 代码库 > 多线程篇五:多个线程访问共享对象和数据的方式

多线程篇五:多个线程访问共享对象和数据的方式

1.多个线程共享一个变量实现方式1 :  将共享数据封装当另外一个对象中,然后将这个对象传递给各个Runnable对象

技术分享
package com.test.shareData;

/**
 * 多个线程共享一个变量:1.将共享数据封装当另外一个对象中,然后将这个对象传递给各个Runnable对象
 * @author Administrator
 *
 */
public class MultiThreadShareData {

    public static void main(String[] args) {
        ShareData data1=new ShareData();
        new Thread(new MyRunnable1(data1)).start();
        new Thread(new MyRunnable2(data1)).start();
        System.out.println("mian: "+data1.getCount());
    }

}


class MyRunnable1 implements Runnable{
    private ShareData data1;
    public MyRunnable1(ShareData data){
        this.data1=data;
    }
    @Override
    public void run() {
        data1.increment();
        System.out.println("MyRunnable1:"+data1.getCount());
    }
}

class MyRunnable2 implements Runnable{
    private ShareData data2;
    public MyRunnable2(ShareData data){
        this.data2=data;
    }
    @Override
    public void run() {
        data2.increment();
        System.out.println("MyRunnable2:"+data2.getCount());
    }
}

 class ShareData{
    private int count=100;
    void increment(){
        count++;
    }
    
    public int getCount() {
        return count;
    }
}
View Code

2.多个线程共享一个变量实现方式2 : 将多个Runnable对象作为一个类的内部了,将共享变量作为外部类的成员变量

技术分享
package com.test.shareData;

/**
 * 多个线程共享一个变量:2.将多个Runnable对象作为一个类的内部了,将共享变量作为外部类的成员变量
 * @author Administrator
 *
 */
public class MultiThreadShareData1 {
    static ShareData1 data1=new ShareData1();
    public static void main(String[] args) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                data1.increment();
            }
        }).start();
        new Thread(new Runnable(){
            @Override
            public void run() {
                data1.decrement();
            }
        }).start();
        System.out.println("mian: "+data1.getCount());
        //mian: 102
    }
}


 class ShareData1{
    private int count=100;
    void increment(){
        count++;
    }
    void decrement(){
        count--;
    }
    
    public int getCount() {
        return count;
    }
}
View Code

3.测试题 : 4个线程,其中两个每次对j加1,另外两个每次对j减1

技术分享
package com.test.shareData;

/**
 * 4个线程,其中两个每次对j加1,另外两个每次对j减1
 * @author Administrator
 *
 */
public class MultiThreadShareDataTest {
    private int j;
    
    public static void main(String[] args) {
        MultiThreadShareDataTest test=new MultiThreadShareDataTest();
        IncTest in =test.new IncTest();
        DecTest de =test.new DecTest();
        for(int i=0;i<2;i++){
            Thread t1=new Thread(in);
            t1.start();

            t1=new Thread(de);
            t1.start();
        }
    }
    
    private synchronized void inc(){
        j++;
        System.out.println("Current Thread :"+Thread.currentThread().getName()+" j:"+j);
    }
    
    private synchronized void dec(){
        j--;
        System.out.println("Current Thread :"+Thread.currentThread().getName()+" j:"+j);
    }
     
     
    class IncTest implements Runnable{
            @Override
            public void run() {
                for(int i=0;i<100;i++){
                    inc();
                }
            }
    }
     
    class DecTest implements Runnable{
            @Override
            public void run() {
                for(int i=0;i<100;i++){
                    dec();
                }
            }
    }
}
View Code

 

多线程篇五:多个线程访问共享对象和数据的方式