Java编程思想

本文通过一个具体的线程同步练习案例,展示了如何正确地使用synchronized关键字来避免并发问题,并解释了为何某些特定的代码实现会导致数据不一致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程装饰性花园练习问题总结

在做练习17的时候遇到一个问题,做下记录,代码如下:

public class Test17 {
    static class Count {
        public int count = 0;
        Random random = new Random(47);
        public synchronized int increamcount() {
            int temp = count;
            while (random.nextBoolean()) {
               Thread.yield();
            }
            return count = ++temp;
        }
        public synchronized int totalCount() {
            return count;
        }
    }

    static class Sensor implements Runnable{

        private int number = 0;

        private static Count count = new Count();

        private final int id;

        private static List<Sensor> sensorList = new ArrayList<>();

        public Sensor(int i) {
            id = i;
            sensorList.add(this);
        }

        public static void cancel() {
            canceled = true;
        }

        private static volatile boolean canceled = false;

        @Override
        public void run() {
            while (!canceled) {
                synchronized (this) {
                    number++;
//                    count.increamcount();
                }
//                System.out.println(this + " total : " + count.totalCount());
                //不能用以上注释掉的写法,因为在执打印的之前,count.increamcount()方法有可能被其他线程执行多次,所以会出现多个相同的count的结果
                System.out.println(this + " total : " + count.increamcount());

                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    System.out.println("sleep interrupted");
                }
            }
            System.out.println("stopping " + this);
        }

        public String toString() {
            return "Sensor " + id + " : " + getValue();
        }

        public synchronized int getValue() {
            return number;
        }

        public static int getTotalValue() {
            return count.totalCount();
        }

        public static int sumEntrances() {
            int sum = 0;
            for(Sensor sensor : sensorList) {
                sum += sensor.getValue();
            }
            return sum;
        }
    }

    public static void main(String[] args) {
        ExecutorService executor =
                new ThreadPoolExecutor(1,17,60L,TimeUnit.MILLISECONDS,new SynchronousQueue<>());
        for (int i = 0; i < 5; i++) {
            executor.execute(new Sensor(i));
        }
        try {
            TimeUnit.SECONDS.sleep(10);
            Sensor.cancel();
            executor.shutdown();
            if(!executor.awaitTermination(250,TimeUnit.MILLISECONDS)) {
                System.out.println("Some tasks were not terminated");
            }
            System.out.println("total :" + Sensor.getTotalValue());
            System.out.println("Sum of sensor :" + Sensor.sumEntrances());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在我刚开始的写法中

 @Override
        public void run() {
            while (!canceled) {
                synchronized (this) {
                    number++;
                    count.increamcount();
                     //为了结果更清晰
                    System.out.println(this + " !!!!!!!");
                }
                System.out.println(this + " total : " + count.totalCount());
                //不能用以上注释掉的写法,因为在执打印的之前,count.increamcount()方法有可能被其他线程执行多次,所以会出现多个相同的count的结果
//                System.out.println(this + " total : " + count.increamcount());

                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    System.out.println("sleep interrupted");
                }
            }
            System.out.println("stopping " + this);
        }

打印结果为:
Sensor 0 : 1 !!!
Sensor 2 : 1 !!!
Sensor 1 : 1 !!!
Sensor 4 : 1 !!!
Sensor 3 : 1 !!!
Sensor 4 : 1 total : 5
Sensor 1 : 1 total : 5
Sensor 2 : 1 total : 5
Sensor 0 : 1 total : 5
Sensor 3 : 1 total : 5
Sensor 3 : 2 !!!
Sensor 0 : 2 !!!
Sensor 0 : 2 total : 10
Sensor 2 : 2 !!!
Sensor 2 : 2 total : 10
Sensor 1 : 2 !!!
Sensor 1 : 2 total : 10
Sensor 3 : 2 total : 10
Sensor 4 : 2 !!!
Sensor 4 : 2 total : 10
Sensor 4 : 3 !!!
Sensor 4 : 3 total : 14
Sensor 2 : 3 !!!
Sensor 2 : 3 total : 15
Sensor 3 : 3 !!!
Sensor 3 : 3 total : 15
Sensor 0 : 3 !!!
Sensor 0 : 3 total : 15
Sensor 1 : 3 !!!
Sensor 1 : 3 total : 15
Sensor 2 : 4 !!!
Sensor 4 : 4 !!!
Sensor 4 : 4 total : 20
Sensor 0 : 4 !!!
Sensor 0 : 4 total : 20
Sensor 3 : 4 !!!
Sensor 3 : 4 total : 20
Sensor 1 : 4 !!!
Sensor 1 : 4 total : 20
Sensor 2 : 4 total : 20
Some tasks were not terminated
total :20
Sum of sensor :20
stopping Sensor 3 : 4
stopping Sensor 4 : 4
stopping Sensor 2 : 4
stopping Sensor 1 : 4
stopping Sensor 0 : 4

可以很清晰的看到在第一次sleep,在执行 System.out.println(this + " total : " + count.totalCount());前5个线程已经完成了增加操作。
其中会出现total重复的情况,经过排查,修改如下

//
@Override
        public void run() {
            while (!canceled) {
                synchronized (this) {
                    number++;
//                    count.increamcount();
                }
//                System.out.println(this + " total : " + count.totalCount());
                //不能用以上注释掉的写法,因为在执打印的之前,count.increamcount()方法有可能被其他线程执行多次,所以会出现多个相同的count的结果
                System.out.println(this + " total : " + count.increamcount());

                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    System.out.println("sleep interrupted");
                }
            }
            System.out.println("stopping " + this);
        }

打印结果:
Sensor 0 : 1 total : 1
Sensor 4 : 1 total : 5
Sensor 3 : 1 total : 4
Sensor 2 : 1 total : 3
Sensor 1 : 1 total : 2
Sensor 1 : 2 total : 6
Sensor 0 : 2 total : 7
Sensor 4 : 2 total : 10
Sensor 2 : 2 total : 9
Sensor 3 : 2 total : 8
Sensor 4 : 3 total : 11
Sensor 3 : 3 total : 12
Sensor 0 : 3 total : 13
Sensor 1 : 3 total : 14
Sensor 2 : 3 total : 15
Sensor 4 : 4 total : 16
Sensor 3 : 4 total : 17
Sensor 0 : 4 total : 18
Sensor 1 : 4 total : 19
Sensor 2 : 4 total : 20
Some tasks were not terminated
total :20
Sum of sensor :20
stopping Sensor 4 : 4
stopping Sensor 3 : 4
stopping Sensor 0 : 4
stopping Sensor 1 : 4
stopping Sensor 2 : 4

第1章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 实现方案的隐藏 1.4 方案的重复使用 1.5 继承:重新使用接口 1.5.1 改善基础类: 尽管extends关键字暗示着我们要为接口“扩展”新功能,但实情并非肯定如此。为区分我们的新类,第二个办法是改变基础类一个现有函数的行为。我们将其称作“改善”那个函数。 为改善一个函数,只需为衍生类的函数建立一个新定义即可。我们的目标是:“尽管使用的函数接口未变,但它的新版本具有不同的表现”。 1.5.2 等价和类似关系 1.6 多形对象的互换使用 1.6.1 动态绑定 1.6.2 抽象的基础类和接口 1.7 对象的创建和存在时间 1.7.1 集合与继承器 1.7.2 单根结构 1.7.3 集合库与方便使用集合 1.7.4 清除时的困境:由谁负责清除? 1.8 违例控制:解决错误 1.9 多线程 1.10 永久性 1.11 Java和因特网: 既然Java不过另一种类型的程序设计语言,大家可能会奇怪它为什么值得如此重视,为什么还有这么多的人认为它是计算机程序设计的一个里程碑呢?如果您来自一个传统的程序设计背景,那么答案在刚开始的时候并不是很明显。Java除了可解决传统的程序设计问题以外,还能解决World Wide Web(万维网)上的编程问题。 1.11.1 什么是Web? 1.11.2 客户端编程 1.11.3 服务器端编程 1.11.4 一个独立的领域:应用程序 1.12 分析和设计 1.12.1 不要迷失: 在整个开发过程中,最重要的事情就是:不要将自己迷失!但事实上这种事情很容易发生。大多数方法都设计用来解决最大范围内的问题。当然,也存在一些特别困难的项目,需要作者付出更为艰辛的努力,或者付出更大的代价。但是,大多数项目都是比较“常规”的,所以一般都能作出成功的分析与设计,而且只需用到推荐的一小部分方法。但无论多么有限,某些形式的处理总是有益的,这可使整个项目的开发更加容易,总比直接了当开始编码好! 也就是说,假如你正在考察一种特殊的方法,其中包含了大量细节,并推荐了许多步骤和文档,那么仍然很难正确判断自己该在何时停止。时刻提醒自己注意以下几个问题: (1) 对象是什么?(怎样将自己的项目分割成一系列单独的组件?) (2) 它们的接口是什么?(需要将什么消息发给每一个对象?) 在确定了对象和它们的接口后,便可着手编写一个程序。出于对多方面原因的考虑,可能还需要比这更多的说明及文档,但要求掌握的资料绝对不能比这还少。 整个过程可划分为四个阶段,阶段0刚刚开始采用某些形式的结构。 1.12.2 阶段0:拟出一个计划: 1.12.3 阶段1:要制作什么?: 1.12.4 阶段2:如何构建? 1.12.5 阶段3:正式创建 1.12.6 阶段4:校订 1.12.7 计划的回报 1.13 Java还是C++? 第2章 一切都是对象 “尽管以C++为基础,但Java是一种更纯粹的面向对象程序设计语言”。 无论C++还是Java都属于杂合语言。但在Java中,设计者觉得这种杂合并不象在C++里那么重要。杂合语言允许采用多种编程风格;之所以说C++是一种杂合语言,是因为它支持与C语言的向后兼容能力。由于C++是C的一个超集,所以包含的许多特性都是后者不具备的,这些特性使C++在某些地方显得过于复杂。 Java语言首先便假定了我们只希望进行面向对象的程序设计。也就是说,正式用它设计之前,必须先将自己的思想转入一个面向对象的世界(除非早已习惯了这个世界的思维方式)。只有做好这个准备工作,与其他OOP语言相比,才能体会到Java的易学易用。在本章,我们将探讨Java程序的基本组件,并体会为什么说Java乃至Java程序内的一切都是对象。 2.1 用句柄操纵对象 2.2 必须创建所有对象: 创建句柄时,我们希望它同一个新对象连接。通常用new关键字达到这一目的。new的意思是:“把我变成这些对象的一种新类型”。所以在上面的例子中,可以说: String s = new String("asdf"); 它不仅指出“将我变成一个新字串”,也通过提供一个初始字串,指出了“如何生成这个新字串”。 当然,字串(String)并非唯一的类型。Java配套提供了数量众多的现成类型。对我们来讲,最重要的就是记住能自行创建类型。事实上,这应是Java程序设计的一项基本操作,是继续本书后余部分学习的基础。 2.2.1 保存在什么地方 2.2.2 特殊情况:主类型 2.2.3 Java中的数组 2.3 绝对不要清除对象:在大多数程序设计语言中,变量的“存在时间”(Lifetime)一直是程序员需要着重考虑的问题。变量应持续多长的时间?如果想清除它,那么何时进行?在变量存在时间上纠缠不清会造成大量的程序错误。在下面的小节里,将阐示Java如何帮助我们完成所有清除工作,从而极大了简化了这个问题。 2.3.1 作用域 2.3.2 对象的作用域 2.4 新建数据类型:类 2.4.1 字段和方法 2.5 方法、自变量和返回值 2.5.1 自变量列表 2.6 构建Java程序:正式构建自己的第一个Java程序前,还有几个问题需要注意。 2.6.1 名字的可见性 2.6.2 使用其他组件 2.6.3 static关键字 2.7 我们的第一个Java程序 2.8 注释和嵌入文档 2.8.1 注释文档 2.8.2 具体语法 2.8.3 嵌入HTML 2.8.4 @see:引用其他类 2.8.5 类文档标记 2.8.6 变量文档标记:变量文档只能包括嵌入的HTML以及@see引用。 2.8.7 方法文档标记 2.8.8 文档示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值