java并发(二):初探syncronized
2024-09-03 11:23:56
参考博客
Java多线程系列--“基础篇”04之 synchronized关键字
synchronized基本规则
第一条 | 当线程访问A对象的synchronized方法和同步块的时候,其他线程无法访问A对象的synchronized方法和同步块 |
第二条 | 当线程访问A对象的synchronized方法和同步块的时候,其他线程可以访问A对象的非synchronized方法和同步块 |
第三条 | 当线程访问A对象的synchronized方法和同步块的时候,其他线程不可以访问A对象其他synchronizedd方法和同步块 |
第三条基本原则测试
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SysDemo3 {
public static void main(String[] args) {
final Count count =new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodB();
}
},"t2");
t1.start();
t2.start();
}
}
@Slf4j
class Count {
int countsize =5;
public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
}
}
public synchronized void synMethodB(){
int count =0;
while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
}
}
}
测试结果
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 0
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 1
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 2
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 3
2019-07-25 13:40:44,482 [t1] INFO Count - synMethodA, Current thread is : t1, count = 4
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 0
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 1
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 2
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 3
2019-07-25 13:40:44,482 [t2] INFO Count - synMethodB, Current thread is : t2, count = 4
说明
每个对象都有一把同步锁,同步锁是依赖对象存在的。上面的结果说明当A对象的同步方法或者同步块被调用的时候,这把锁
就在使用者中,其他的使用者调用A对象的同步方法或者同步块的时候,是不会获取到锁的。
实例锁与全局锁
实例锁是锁在对象上
全局锁是锁在类上,static syncronized方法或者同步块
不同的线程调用一个类的不同的static syncronized方法。
不同的线程调用一个类的不同的static syncronized方法:x.classSyn1()与y.classSyn2(),x不释放,y是无法运行静态同步方法的。
测试
import lombok.extern.slf4j.Slf4j;
import static java.lang.Thread.sleep;
/*
全局锁的使用,类的static syncronized方法或代码块被线程调用,
其他线程没有拥有全局锁,无法调用其他的同步方法和同步块
*/
@Slf4j
public class SysDemo5 {
static class Count {
static int countsize =5;
public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void synMethodB(){
int count =0;
while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void cSynMethodA(){
int count =0;
while(count<countsize){
log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void cSynMethodB(){
int count =0;
while(count<countsize){
log.info("class synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodB();
}
},"t2");
t1.start();
t2.start();
}
}
测试结果
2019-07-25 17:31:24,358 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 0
2019-07-25 17:31:24,857 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 1
2019-07-25 17:31:25,356 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 2
2019-07-25 17:31:25,855 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 3
2019-07-25 17:31:26,354 [t1] INFO SysDemo5 - class synMethodA, Current thread is : t1, count = 4
2019-07-25 17:31:26,853 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 0
2019-07-25 17:31:27,351 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 1
2019-07-25 17:31:27,850 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 2
2019-07-25 17:31:28,349 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 3
2019-07-25 17:31:28,847 [t2] INFO SysDemo5 - class synMethodB, Current thread is : t2, count = 4
x.classSyn1与x.sysn1
对象的同步方法和类的同步方法互不影响
测试
import lombok.extern.slf4j.Slf4j;
import static java.lang.Thread.sleep;
@Slf4j
public class SysDemo4 {
static class Count {
static int countsize =5;
public synchronized void synMethodA(){
int count =0;
while(count<countsize){
log.info("synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void synMethodB(){
int count =0;
while(count<countsize){
log.info("synMethodB, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void cSynMethodA(){
int count =0;
while(count<countsize){
log.info("class synMethodA, Current thread is : {}, count = {}",Thread.currentThread().getName(),count++);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final Count count =new Count();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
count.synMethodA();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Count.cSynMethodA();
}
},"t2");
t1.start();
t2.start();
}
}
测试结果
2019-07-25 19:04:53,184 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 0
2019-07-25 19:04:53,199 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 0
2019-07-25 19:04:53,683 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 1
2019-07-25 19:04:53,699 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 1
2019-07-25 19:04:54,182 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 2
2019-07-25 19:04:54,198 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 2
2019-07-25 19:04:54,682 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 3
2019-07-25 19:04:54,697 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 3
2019-07-25 19:04:55,181 [t2] INFO SysDemo4 - class synMethodA, Current thread is : t2, count = 4
2019-07-25 19:04:55,197 [t1] INFO SysDemo4 - synMethodA, Current thread is : t1, count = 4
Process finished with exit code 0
最新文章
- Android SQLite调试
- mysql 单表排序,相同值排序
- A:手把手教Wordpress仿站(基础)
- Unity关于一个UGUI优化效率的方法
- Process Kill Technology &;&; Process Protection Against In Linux
- HTML表格与列表
- 话说C语言const用法
- Spring配置文件web.xml关于拦截
- php explode 用法详解
- dede修改移动文档的js
- 【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
- [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …
- 多重集组合数 (DP)
- awk命令的用法实战
- 取消Eclipse控制台显示行数的限制
- 洛谷 P1433 吃奶酪【DFS】+剪枝
- UILabel部分文字可点击
- TortoiseGit需要重复填写用户名和密码的问题
- 算法初探:Tensorflow及PAI平台的使用
- Nginx 502 Bad Gateway 解决方法
热门文章
- set的使用-Hdu 2094
- python中os.path.abspath与os.path.realpath 区别
- Plastic Sprayers Manufacturer - The Basic Components Of A Spray Bottle
- ubuntu apache 通过端口新建多个站点
- Ubuntu各个版本的镜像下载地址
- Spring Boot 缓存应用 Ehcache 入门教程
- 【转载】Eclipse 最常用快捷键 (动画讲解),最简单的一些快捷键
- 20200213springboot日记
- 【JS 移动端】获取设置页面大小
- vue.js 第九课