synchronized一个(二)
今天遇到了一个关于synchronized的一个问题,关于其持有锁的问题。这个问题以前是有看过相关文章的,但是一直没有记录,今天大概记录一下当前的认知。
对于静态方法,synchronized的使用的锁实际上是以Class对象作为锁,对于非静态方法,持有的锁为方法所在的对象。可能有点难以理解,但是,仔细想想,静态方法是类级别的,而非静态方法属于对象级别的。这样或许好理解一下。
关于synchronized的常规用法
同步控制块。进入此段代码前,必须得到syncObject对象的锁,如果其他线程正在持有这个锁,那么就得等到这个线程释放以后,才能进入临界区。
synchronized(syncObject){
//处理逻辑
}
亦或使用其修饰整改方法,这个时候,就需要注意到开头说的锁的问题了。对于不同级别的方法,所持有的锁是不同的。
synchronized fun(){
//处理逻辑
}
下面我们,通过demo来说这个情况。注释代码,说明了静态方法与非静态方法使用不同的锁。三个线程并发,为了说明即使在同一个对象当中,也必须使用相同的对象锁,synchronized才能确保临界区同一个时刻只能被一个线程访问。结果中先输出fun2表明线程2并没有因线程1持有当前demo对象的锁而被阻塞,但是线程3却被阻塞了。
package com.woniu.test.concurrency; import java.util.HashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class TestSynchronized { public static void main(String[] args) throws InterruptedException {
// ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
// executor.scheduleAtFixedRate(new Demo(), 1, 2, TimeUnit.SECONDS);
//
// while(true) {
// System.out.println(System.currentTimeMillis() + ", woniu id is " + Demo.getMap().get("woniu"));
// Thread.sleep(1000);//这里的延迟会因updateMap持有相同的锁,导致延迟的时间变得更长
// } final Demo demo = new Demo(); new Thread(new Runnable() { public void run() {
try {
demo.fun1();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
).start(); Thread.sleep(1000);
new Thread(new Runnable() { public void run() {
try {
demo.fun2();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
).start(); new Thread(new Runnable() { public void run() {
try {
demo.fun3();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
).start();
}
} class Demo implements Runnable { private static HashMap<String, Integer> map = new HashMap<String, Integer>();
private Object obj = new Object();
public Demo() {
map.put("woniu", 1);
} public void run() {
try {
updateMap();
} catch (InterruptedException e) {
e.printStackTrace();
}
} //如果updateMap没有使用static进行修饰,此处休息3000ms,对于主线程的while循环式不起作用的,因为两者使用的锁不一致。
public synchronized static void updateMap() throws InterruptedException {
System.out.println("begin to update");
Thread.sleep(3000);
map.put("woniu", map.get("woniu") + 1);
System.out.println("update end");
} public synchronized static HashMap<String, Integer> getMap() {
return map;
} public synchronized void fun1() throws InterruptedException {
Thread.sleep(3000);
System.out.println("this is fun1()");
} public void fun2() throws InterruptedException {
synchronized (obj) {
System.out.println("this is fun2()");
}
} public void fun3() throws InterruptedException {
synchronized (this) {
System.out.println("this is fun3()");
}
}
}
synchronized一个(二)的更多相关文章
- 二维码合成,将苹果和安卓(ios和android)合成一个二维码,让用户扫描一个二维码就可以分别下载苹果和安卓的应用
因为公司推广的原因,没有合适的将苹果和安卓(ios和android)合成一个二维码的工具. 因为这个不难,主要是根据浏览器的UA进行判断,所以就自己开发了一个网站 网站名称叫:好推二维码 https ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
- 正确运用synchronized和二次判断 实现多线程安全
正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示: 测试代码如下: 特别注意if(shutdownRequested) ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...
- new一个二维数组
.定义一个二维数组 char **array1 array1 = new char *[x]; for(i=0;i<x;++i) array1[i] = new char[y]; ...用的时候 ...
- C语言里的指针探析——type *name[] 在函数参数里面,是一个二维指针
type *name[] 在函数参数里面声明和不在函数里面声明其实不一样. type *name[] 如果在函数参数里声明,则name 是一个二维指针,并不是一个指针数组,而如果不在函数参数里声明,则 ...
- 公司开发的APP,如何生成一个二维码,供客户下载使用
1.其实和简单,因为一般的用户使用扫一扫,大多数都是用微信自带的扫一扫工具 而,微信打开的二维码页面,会自动屏蔽apk文件,所以显然把apk的url生成一个二维码,让用户扫一扫就能直接下载,这样是行不 ...
随机推荐
- LockSupport HotSpot里park/unpark的实现
每个java线程都有一个Parker实例,Parker类是这样定义的: class Parker : public os::PlatformParker { private: volatile int ...
- 表单(上)EasyUI Form 表单、EasyUI Validatebox 验证框、EasyUI Combobox 组合框、EasyUI Combo 组合、EasyUI Combotree 组合树
EasyUI Form 表单 通过 $.fn.form.defaults 重写默认的 defaults. 表单(form)提供多种方法来执行带有表单字段的动作,比如 ajax 提交.加载.清除,等等. ...
- js中var a={}什么意思
创建一个变量a, 并给a赋值:{}是一个空的对象,是 new Object();的简写.
- .net webform 把word转为html
首先添加引用 引用命名空间 using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; u ...
- HTTP从入门到入土(3)——TCP三次握手
TCP三次握手 客户端与服务器之间互相发送HTTP请求响应之前需要先进行TCP连接,因为HTTP是一个无连接.无状态协议,不存在连接的概念,只有请求和响应的概念.而请求和响应实际上只是数据包,他们需要 ...
- 前端学习笔记之HTML/CSS 速写神器 Emmet
HTML/CSS 速写神器:Emmet 在前端开发的过程中,一个最繁琐的工作就是写 HTML.CSS 代码.数量繁多的标签.属性.尖括号.标签闭合等,让前端们甚是苦恼.于是,我向大家推荐 Emmet, ...
- 设置VS快捷代码片段
一.自定义sinppets方式 1.在VS安装路径[D:\vs2013\VC\Snippets\2052\Visual C++]下新建一个snippt文件 2.添加代码 <?xml versio ...
- 如何为openwrt生成补丁
答:使用quilt工具 步骤如下: 1. 配置quilt $cat> ~/.quiltrc <<EOF QUILT_DIFF_ARGS="--no-timestamps - ...
- Knockout 监控数组对象属性
代码: function Product(ProductID,ProductName,ProductNum,Result,Price) { this.ProductID = ko.observable ...
- POJ 3169 Layout(差分约束+最短路)题解
题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...