多线程和并发库应用七-线程间数据共享2

19 篇文章 0 订阅
15 篇文章 0 订阅

上两篇讲到了数据共享的方式本篇主要一个小题目来巩固一下
假设有4个线程两个要对j加1 ,两个要对j 减1
两种方式实现线程的数据共享
1. 将共享数据封装在另一个对象中,然后将这个对象逐一传递给各个Runable 对象。每个线程对共享数据的操作也分配到那个对象中去完成。这样容易实现针对该数据进行的各个操作的互斥和通信。

public class ThreadShareData {
    public static void  main(String[] args ){
        ShareData1 sd1=new ShareData1();
        new Thread(new Runnable1(sd1)).start();
        new Thread(new Runnable1(sd1)).start();
        new Thread(new Runnable2(sd1)).start();
        new Thread(new Runnable2(sd1)).start();
    }
}
class Runnable1 implements  Runnable{
    private ShareData1 sd1;

    public Runnable1(ShareData1 sd1){
        this.sd1=sd1;
    }
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                sd1.decrement();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Runnable2 implements  Runnable{
    private ShareData1 sd1;

    public Runnable2(ShareData1 sd1){
        this.sd1=sd1;
    }
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
                sd1.increment();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
class ShareData1{
    private int j=0;
    public void increment(){
        System.out.print(j+" ");
        j++;
        System.out.println(" add after "+j);
    }
    public void decrement(){
        System.out.print(j+" ");
        j--;
        System.out.println(" decre after "+j);
    }

}

2.将各个runnable 对象作为内部类 对象作为外部成员变量

 public class ThreadShareData2 {
    public static void  main(String[] args ){

        final ShareData1 sd1=new ShareData1();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.increment();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.decrement();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.increment();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();
        new Thread(new Runnable(){

            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                        sd1.decrement();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }            }
        }).start();

    }
}

class ShareData1 {
    private int j=0;
    public synchronized void increment(){
        System.out.print(j+" ");
        j++;
        System.out.println(" add after "+j);
    }
    public synchronized  void decrement(){
        System.out.print(j+" ");
        j--;
        System.out.println(" decre after "+j);
    }

    public void run() {

    }
}

文章地址:http://www.haha174.top/article/details/253024
项目源码:https://github.com/haha174/thread-learning.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值