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. input-file类型accept 属性对性能的影响

    上传图片的时候,有时会加一些限制,如下,进行上传类型的过滤,如 <input type="file" name="pic" id="pic&qu ...

  2. PHP设计模式之单例模式

    <?php #千锋PHP http://www.qfedu.com/php/? #千锋PHP http://www.qfedu.com/php/? namespace app; /** * Cl ...

  3. ERROR 3077 (HY000): To have multiple channels, repository cannot be of type FILE; Please check the repository configuration and convert them to TABLE.

    在5.7.16搭建多源复制时,出现如下错误:   mysql> change master to master_host='192.168.56.156',master_user='repl', ...

  4. 创建一个gradle项目

    1.创建项目 一定要选这个安装的路径 项目创建成功,修改build.gradle文件,主要是为了下载依赖的jar包,原始模板, 而我修改之后,如下 apply plugin: 'idea' apply ...

  5. 空间最短路径,BFS(POJ3278)

    题目链接:http://poj.org/problem?id=3278 #include <cstdio> #include <queue> #include <stri ...

  6. querystring处理参数小利器

    相信上一章的讲解,相信大家对url地址有一个更直观的认识,在url解析的时候可以用querystring这样一个module替换,然后对这个query集成一个对象,这里不管是前端开发还是后端开发,都常 ...

  7. DOM节点(二):操作节点

    appendChild() 用于向childNodes列表的末尾添加一个节点. var returnedNode = someNode.appendChild(newNode); 如果传入的节点已经是 ...

  8. 【转】NodeJS教程--基于ExpressJS框架的文件上传

    本文是翻译的一篇文章,原文地址:Handle File Uploads in Express (Node.js). 在NodeJS发展早期上传文件是一个较难操作的功能,随后出现了formidable. ...

  9. Git版本控制的原理

    Git工作区域 工作目录(Working Directory) 暂存区(Stage/Index) 资源库(Repository或Git Directory) 远程的git仓库(Remote Direc ...

  10. hadoop中compare函数

    在看hadoop  的二次排序的时候,改写了下, 加了第三个参数,  本来以为是在 public int compareTo(IntPair o) { System.out.println(" ...