《Cracking the Coding Interview》——第16章:线程与锁——题目6
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- 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 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目5
2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目2
2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1
2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...
随机推荐
- 数组:获取GET的键名
1.今天仓鼠遇到这个情况:通过$_GET获取参数,但是参数变成了键名形式 2.那仓鼠想要拿到这个键名,那就要:使用array_keys()获取数组中的所有键名,然后进行转换 代码如下: 结果: 以上 ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- Jmeter入门4 添加断言 判断响应数据是否符合预期
发出请求之后,通过添加断言可以判断响应数据是否是我们的预期结果. 1 在Jmeter中发送一个登录的http请求(参数故意输入错误).结果肯定是登陆失败啦. 但结果树中http请求的图标显示‘绿色’表 ...
- selenium添加cookie切换到不同环境
背景:网站中需要切环境到预发布,在用谷歌浏览器可以使用工具,但是在selenium启动时,是不会带任何插件,向开发了解下,切换环境本质是添加cookie值,那么这个就简单了 1.使用selenium中 ...
- 【HHHOJ】ZJOI2019模拟赛(十三)03.10 解题报告
点此进入比赛 得分: \(97+0+10=107\) 排名: \(Rank\ 3\) \(Rating\):\(+47\) \(T1\):[HHHOJ187]Hashit(点此看题面) 容易想到可以用 ...
- 抽象类和final
抽象类: 概念:在继承过程中,形成一个继承金字塔,位于金字塔底部的类越来越具体(强大),位于塔顶的越来越抽象(简单). 关键字 :abstract 抽象类特性: [1]抽象类过于抽象,实例化后无语义 ...
- Flashing Fluorescents(状压DP)
Flashing Fluorescents 时间限制: 1 Sec 内存限制: 128 MB提交: 56 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 You ha ...
- Entityframework对应sqlserver版本问题
修改.edmx文件中 providermanifesttoken 的值
- chrome 浏览器插件开发(二)—— 通信 获取页面变量 编写chrome插件专用的库
在chrome插件的开发过程中,我遇到了一些问题,在网上找了不少文章,可能是浏览器升级的原因,有一些是有效的也有无效的.下面我简单的分享一下我遇到的坑,以及我把这些坑的解决方案整理而成的js库 —— ...
- C#注释语句
C#注释语句 注释就是在程序中标记.说明某个程序段的作用.注释语句不会被执行. 一.单行注释 // 这是一行注释 二.多行注释 /* 这是多行注释 第一行 第二行 ...... ...