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. DataGrid 样式

    <SolidColorBrush x:Key="OutsideFontColor" Color="#FF000000" /> <LinearG ...

  2. 使用selenium grid与BrowserMobProxyServer联合使用

    背景:项目主要是做埋点数据,要使用 BrowserMobProxyServer,它相当于做一个代理,在你访问一个网页时,通过代理,获取打开网页的数据,对比你需要对比数据,所以这个工具提供获取页面请求的 ...

  3. 富文本 文字图片点击,(TextView)

    textview上的富文本支持 文字,图片的点击事件 - (void)protocolIsSelect:(BOOL)select { NSMutableAttributedString *attrib ...

  4. Python IDE PyCharm的快捷键大全

    Python IDE PyCharm的快捷键大全 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl ...

  5. Ubuntu 14.04 VPS安装配置***的方法

    #安装*** $ sudo apt-get update $ sudo apt-get install python-gevent python-pip $ sudo pip install shad ...

  6. Server_Tomcat

    1 Tomcat概述 Tomcat服务器由Apache提供,开源免费.由于Sun和其他公司参与到了Tomcat的开发中,所以最新的JSP/Servlet规范总是能在Tomcat中体现出来.当前最新版本 ...

  7. 写给iOS小白的MVVM教程(序)

    这几天,需要重构下部分代码,这里简要记录下.但是涉及的技术要点还是很多,所以分为多个篇章叙述.此教程来源于,并将于应用于实践,不做过多的概念性阐释和争论.每个篇章都会附上实际的可执行的代码.因涉及的技 ...

  8. poj_1320_Street Numbers

    A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of ...

  9. MySQL5.6基于GTID的主从复制配置

    全局事务标示符(Global Transactions Identifier)是MySQL 5.6复制的一个新特性. GTID实际上是由UUID+TID组成的.其中UUID是一个MySQL实例的唯一标 ...

  10. 汇编:1位16进制数到ASCII码转换

    ;============================ ;1位16进制数到ASCII码转换 ; { X+30H (0≤X≤9) ;Y= { ; { X+37H (0AH≤X≤0FH) DATAS ...