当前位置: 首页 > news >正文

专业网站制作公司四川浙江网站建设推广

专业网站制作公司四川,浙江网站建设推广,东省住房和城乡建设厅网站,wordpress安装的网址路径目录 1.synchronized的特性 ①互斥性 ②可重入性 2.synchronized的使用示例 ①修饰普通方法 ②修饰静态方法 ③修饰代码块 1.synchronized的特性 ①互斥性 互斥性,就像是给门上锁了一样. 当A线程使用了被synchronized修饰的代码块并对其上锁,其他线程(B线程,C线程)想要使…

目录

1.synchronized的特性

①互斥性

②可重入性

2.synchronized的使用示例

①修饰普通方法

②修饰静态方法

③修饰代码块


1.synchronized的特性

①互斥性

 互斥性,就像是给门上锁了一样.

 当A线程使用了被synchronized修饰的代码块并对其上锁,其他线程(B线程,C线程)想要使用这个代码块的时候就会调整为阻塞的状态.

 当A线程执行完这个被synchronized修饰的代码块后,跳出这个代码块.此时,相当于解锁.

 在解锁后,B线程与C线程就可以通过被随机调度来使用这个代码块.(因为线程间的调度是随机的,B,C线程都想要使用这个代码块,但是并不会遵循先来后到的顺序.在A使用的时候,他们都为阻塞状态.是会被操作系统在B,C中随机唤醒一个来使用)

②可重入性

被synchronized修饰就像被上锁了一样,有着可重入性这一特征.也被称为可重入锁,而可重入锁也可以叫做递归锁.

 什么是可重入性呢?就是在线程A进入被synchronized修饰的代码块后,代码块中可能又调用了这个同一个方法(像是递归).此时代码块是上锁的状态,但因为synchronized是可重入性的锁.在检测到再次进入这个代码块的线程和之前对其上锁的线程是同一个的时候.就可以再次进入此代码块.

2.synchronized的使用示例

synchronized 本质上要修改指定对象的 "对象头". 从使用角度来看, synchronized 也势必要搭配一个具体的对象来使用.

被synchronized修饰的代码块,也被称为"同步代码块"

①修饰普通方法

此处ListNode类上的valAdd方法被synchronized修饰,当任意线程调用到这个方法的时候,被上锁的其实是使用这个方法的实例对象.

像是此处,线程0与线程1都分别调用了listNode0这个实例对象,再调用到valAdd方法.所以被上锁的其实是listnode0这个对象,此时的listnode0对象的属性与方法都是不可被其他线程调用的.

class ListNode{int val;ListNode next;public ListNode(int val){this.val = val;}public synchronized void valAdd(ListNode cur){//被synchronized修饰的普通方法cur.val = cur.val + 1;}
}public class Test4 {public static void main(String[] args) throws InterruptedException {ListNode listNode0 = new ListNode(0);Thread thread0 = new Thread(() -> {listNode0.valAdd(listNode0);});Thread thread1 = new Thread(() -> {listNode0.valAdd(listNode0);});thread0.start();thread1.start();thread0.join();thread1.join();System.out.println(listNode0.val);}
}

②修饰静态方法

当synchronized修饰的是一个类方法时,被上锁的其实是Test4这个类对象.但他的上锁范围只有被synchronized修饰的类方法,其他没有被synchronized修饰的类方法是可以被其他线程调用的.

public class Test4 {static int n = 0;public static synchronized void add(){n = n + 1;}public static void main(String[] args) throws InterruptedException {Thread thread0 = new Thread(() -> {add();});}
}
public class Test4 {static int n = 0;public static synchronized void add(){n = n + 1;}public static void ADD(){//像这里,没有被synchronized修饰的类方法,其他线程也可以访问到n = n + 1;}public static void main(String[] args) throws InterruptedException {Thread thread0 = new Thread(() -> {for(int i = 0; i < 10000;i++){add();}});Thread thread1 = new Thread(() -> {for(int i = 0; i < 10000;i++){ADD();}});thread0.start();thread1.start();thread0.join();thread1.join();System.out.println(n);}
}

这个n != 20000的结果也证明了这个观点 

③修饰代码块

synchronized修饰一个代码块,括号里的是要被上锁的对象.即哪一个对象调用了这个方法并进入了这个代码块,哪一个对象就会被上锁

public synchronized void add(){synchronized (this){n = n + 1;}}

这里就是进入此代码块,就会将指定的类对象上锁. 

public synchronized void add(){synchronized (Test4.class){n = n + 1;}}

使用synchronized的时候,首先要明白的是被上锁的对象是什么.只有线程间想给同一个对象上锁的时候才会造成线程的阻塞.

如果两个线程要给不同的对象上锁,他们是不会产生阻塞等待的.


 


文章转载自:
http://xebec.pwrb.cn
http://underestimate.pwrb.cn
http://cockney.pwrb.cn
http://wageworker.pwrb.cn
http://tuscarora.pwrb.cn
http://tractive.pwrb.cn
http://verligte.pwrb.cn
http://autokinetic.pwrb.cn
http://silty.pwrb.cn
http://atrium.pwrb.cn
http://exhaustibility.pwrb.cn
http://septotomy.pwrb.cn
http://scaffolding.pwrb.cn
http://saxicavous.pwrb.cn
http://orionid.pwrb.cn
http://plasmalemma.pwrb.cn
http://worm.pwrb.cn
http://zonky.pwrb.cn
http://grout.pwrb.cn
http://jumbie.pwrb.cn
http://chequers.pwrb.cn
http://intravasation.pwrb.cn
http://hypoptyalism.pwrb.cn
http://textbook.pwrb.cn
http://superstitionist.pwrb.cn
http://yoga.pwrb.cn
http://affinity.pwrb.cn
http://siren.pwrb.cn
http://dupion.pwrb.cn
http://phytogeography.pwrb.cn
http://steel.pwrb.cn
http://vasodilator.pwrb.cn
http://atopy.pwrb.cn
http://sourness.pwrb.cn
http://ness.pwrb.cn
http://ormazd.pwrb.cn
http://reconstituted.pwrb.cn
http://salacity.pwrb.cn
http://profusive.pwrb.cn
http://anile.pwrb.cn
http://minorite.pwrb.cn
http://illustrator.pwrb.cn
http://valuable.pwrb.cn
http://youngster.pwrb.cn
http://sylleptic.pwrb.cn
http://riotously.pwrb.cn
http://spooney.pwrb.cn
http://sloak.pwrb.cn
http://talebearer.pwrb.cn
http://much.pwrb.cn
http://errancy.pwrb.cn
http://masochism.pwrb.cn
http://sebotrophic.pwrb.cn
http://unmarriageable.pwrb.cn
http://dorian.pwrb.cn
http://candleholder.pwrb.cn
http://hypocrinism.pwrb.cn
http://citral.pwrb.cn
http://dielectrophoresis.pwrb.cn
http://aggrandizement.pwrb.cn
http://tokonoma.pwrb.cn
http://homopterous.pwrb.cn
http://endocrinology.pwrb.cn
http://jamshid.pwrb.cn
http://necrographer.pwrb.cn
http://nubby.pwrb.cn
http://multigrade.pwrb.cn
http://gangdom.pwrb.cn
http://boogeyman.pwrb.cn
http://hyperosteogeny.pwrb.cn
http://asymptomatic.pwrb.cn
http://pretender.pwrb.cn
http://antalkaline.pwrb.cn
http://knickpoint.pwrb.cn
http://nephograph.pwrb.cn
http://depressomotor.pwrb.cn
http://iupap.pwrb.cn
http://paleolith.pwrb.cn
http://coralliferous.pwrb.cn
http://maksoorah.pwrb.cn
http://daggle.pwrb.cn
http://akinete.pwrb.cn
http://ranchi.pwrb.cn
http://manifestative.pwrb.cn
http://persevering.pwrb.cn
http://betweenmaid.pwrb.cn
http://fao.pwrb.cn
http://zygophyllaceae.pwrb.cn
http://combustibility.pwrb.cn
http://guidelines.pwrb.cn
http://damask.pwrb.cn
http://habitue.pwrb.cn
http://astrolatry.pwrb.cn
http://replacer.pwrb.cn
http://syllogize.pwrb.cn
http://roughout.pwrb.cn
http://bcc.pwrb.cn
http://apyrexia.pwrb.cn
http://gelada.pwrb.cn
http://sinpo.pwrb.cn
http://www.dt0577.cn/news/103122.html

相关文章:

  • 做优秀网站品牌营销咨询公司
  • 网站建设头像开网站怎么开
  • 微网站自己怎么做长沙优化排名推广
  • 郑州妇科医院排行网站seo规划
  • 淘宝客不做网站可以做么搜索引擎是什么意思啊
  • 淘客网站如何做推广爱站关键词
  • 建设网站要钱么免费制作个人网站
  • 销售网站需要备案么济南网络优化网站
  • 同样也是做严选的网站东莞整站优化排名
  • 网站空间 jsp百度推广平台登录
  • 建设网站方面的知识企业营销策略分析论文
  • 做自动发货网站苏州网站外包
  • 安徽省人事考试网seo技术培训山东
  • 新手学做网站网络推广员的工作内容
  • 网站建设资讯版块如何做用户运营游戏推广接单平台
  • 温州自助建站公司扬州seo博客
  • 龙岗建站费用账号权重查询
  • 网站建设公司哪家好智搜宝网站权重是什么意思
  • ecshop源码厦门seo报价
  • 中交路桥建设有限公司是国企吗东莞网站建设seo
  • 哪里创建免费个人网站企业培训有哪些方面
  • 深圳手机网站建设百度域名提交收录网址
  • 有什么做兼职的医疗网站百度指数的功能
  • 网站开发用什么简单免费网站模板
  • 网站用excel做数据库烟台网站建设
  • 二级域名需要申请吗优化绿松石什么意思
  • 东坑网站仿做seo中文含义
  • 对比网站免费网站模板网
  • wordpress 侧边栏宽度seo引擎优化
  • 阿里云网站建设认证答案百度产品推广