2014-04-27 20:25

题目:关于java中标有synchronized的成员方法?

解法:这代表同一个对象实例synchronized方法不能被多个线程同时调用。注意有这么多个地方都加粗了,如果这些条件有一个不满足的话,就是可以调用的。另外,如果此方法是静态成员方法, 那么总可以认为是“同一实例”的,因为静态方法就属于一个类,类似于单体。

代码:

 // 16.6 How do you explain the "synchronized" keyword?
There're three properties about a member method of a class:
1. static or not
2. synchronized or not
3. of same object instance of a class or not
For example:
public class FooBar {
public void f() {};
public synchronized void g() {};
public static synchronized void h() {};
} There're three member methods in the class FooBar:
1. f() is non-static and unsynchronized.
2. g() is non-static and synchronized.
3. h() is static and synchronized. The keyword "synchronized" makes sure that an instance of code segment is executed by only one thread at a time.
The instance of the code segment may be a static one, or a non-static one belonging to one object instance. Unsynchronized methods is free of these constraints. They can be called by arbitrary numbers of threads at the same time.
Synchronized methods of different object instances is independent from each other. They can be called by different threads at the same time. Here's a truth table for your information:
----------------------------------------------------------------
synchronized|static |sameinstance |can be called at same time?
0 |0 |0 |1
0 |0 |1 |1
0 |1 |0 |1
0 |1 |1 |1
1 |0 |0 |1
1 |0 |1 |0
1 |1 |0 |1
1 |1 |1 |0
----------------------------------------------------------------
Plus: Static method can be regarded as singleton, thus it's always one instance only.

《Cracking the Coding Interview》——第16章:线程与锁——题目6的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. 《Cracking the Coding Interview》——第16章:线程与锁——题目5

    2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...

  8. 《Cracking the Coding Interview》——第16章:线程与锁——题目2

    2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...

  9. 《Cracking the Coding Interview》——第16章:线程与锁——题目1

    2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...

随机推荐

  1. 22 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  2. HTC vive VR设备软硬件安装+运行unity开发的VR程序

    总结在HTC vive VR开发过程中的HTC vive的安装调试 1.首先确保电脑的配置满足要求: 进入官网,测试电脑是否满足要求 链接:https://www.vive.com/us/produc ...

  3. 中期ppt制作

    陀螺仪的使用解释:https://zhuanlan.zhihu.com/p/29244429 手机坐标轴的图片:http://jcjs.siat.ac.cn/ch/reader/create_pdf. ...

  4. CNN中卷积的意义

    在传统的神经网络中,比如多层感知机(MLP),其输入通常是一个特征向量.需要人工设计特征,然后将用这些特征计算的值组成特征向量.在过去几十年的经验来看,人工找的特征并不总是好用.有时多了,有时少了,有 ...

  5. arraylist,list ,数组区别

    https://www.cnblogs.com/a164266729/p/4561651.html

  6. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  7. Linux查看CPU和内存使用情况[转]

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...

  8. C#继承简介与规则

    一.C#继承简介 1. 类的层次结构 下面是一个类的层次结构图: 上图反映了鱼类的派生关系,其中最高层的实体往往具有最一般最普遍的特征,越下层的实体就越具体,并且下层包含了上层的特征.如果将上层的实体 ...

  9. EBS R12中FND凭证打印警告:OPP响应超时

    接近年关,最近年结忙的飞起,此为背景,今天运维那边反应日记账凭证打印报错,看了下后台请求发现请求有警告. 查看日志发现报了“并发:OPP响应超时”的警告,这个地方响应超时可能是配置文件中“并发:OPP ...

  10. Qt.5.9.6移植

    工具及软件包 交叉编译工具链 arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 软件包 dbus-1.10.0.tar.g ...