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

佛山网站建设制作公司河南 网站建设

佛山网站建设制作公司,河南 网站建设,三牛网络推广,深圳媒体网络推广有哪些synchronized可以对两种对象加锁#xff1a;实例对象和类对象。下边先说对类对象加锁的代码#xff1a; 第1是修饰static方法#xff0c;第2种是直接锁类的class对象#xff1b; /*** title: SynchronizedStaticDemo1* description: synchronized 对类加锁1* author: * d…synchronized可以对两种对象加锁实例对象和类对象。下边先说对类对象加锁的代码 第1是修饰static方法第2种是直接锁类的class对象 /*** title: SynchronizedStaticDemo1* description: synchronized 对类加锁1* author: * date: 2023/11/18 19:29*/ public class SynchronizedStaticDemo1 {private static int data 0;public static void main(String[] args) {Thread thread1 new Thread(线程1){Overridepublic void run() {for (int i 0; i 10; i) {increment();}}};thread1.start();Thread thread2 new Thread(线程2){Overridepublic void run() {for (int i 0; i 10; i) {increment();}}};thread2.start();}public synchronized static void increment(){SynchronizedStaticDemo1.data ;System.out.println(SynchronizedStaticDemo1.data Thread.currentThread().getName());} } /*** title: SynchronizedStaticDemo2* description: synchronized 对类加锁2* author: * date: 2023/11/18 19:29*/ public class SynchronizedStaticDemo2 {private static int data 0;public static void main(String[] args) {Thread thread1 new Thread(线程1){Overridepublic void run() {for (int i 0; i 10; i) {synchronized (SynchronizedStaticDemo2.class){data ;System.out.println(data Thread.currentThread().getName());}}}};thread1.start();Thread thread2 new Thread(线程2){Overridepublic void run() {for (int i 0; i 10; i) {synchronized (SynchronizedStaticDemo2.class){data ;System.out.println(data Thread.currentThread().getName());}}}};thread2.start();} }synchronized对某个实例对象加锁如下三种方法 /*** title: SynchronizedInstanceDemo1* description: synchronized修饰实例对象* author: * date: 2023/11/18 19:29*/ public class SynchronizedInstanceDemo1 {private static int data 0;public static void main(String[] args) {SynchronizedInstanceDemo1 demo new SynchronizedInstanceDemo1();Thread thread1 new Thread(线程1){Overridepublic void run() {for (int i 0; i 10; i) {demo.increment();}}};thread1.start();Thread thread2 new Thread(线程2){Overridepublic void run() {for (int i 0; i 10; i) {demo.increment();}}};thread2.start();}public synchronized void increment(){SynchronizedInstanceDemo1.data ;System.out.println(SynchronizedInstanceDemo1.data Thread.currentThread().getName());} } /*** title: SynchronizedInstanceDemo2* description: synchronized修饰实例对象* author: * date: 2023/11/18 19:29*/ public class SynchronizedInstanceDemo2 {private static int data 0;public static void main(String[] args) {SynchronizedInstanceDemo2 demo new SynchronizedInstanceDemo2();Thread thread1 new Thread(线程1){Overridepublic void run() {for (int i 0; i 10; i) {synchronized (demo){data ;System.out.println(data Thread.currentThread().getName());}}}};thread1.start();Thread thread2 new Thread(线程2){Overridepublic void run() {for (int i 0; i 10; i) {synchronized (demo){data ;System.out.println(data Thread.currentThread().getName());}}}};thread2.start();} } /*** title: SynchronizedInstanceDemo3* description: synchronized修饰实例对象* author: * date: 2023/11/18 19:29*/ public class SynchronizedInstanceDemo3 {private static int data 0;public static void main(String[] args) {SynchronizedInstanceDemo3 demo new SynchronizedInstanceDemo3();Thread thread1 new Thread(线程1){Overridepublic void run() {for (int i 0; i 10; i) {demo.increment();}}};thread1.start();Thread thread2 new Thread(线程2){Overridepublic void run() {for (int i 0; i 10; i) {demo.increment();}}};thread2.start();}public void increment(){synchronized (this){data ;System.out.println(data Thread.currentThread().getName());}} }
http://www.sadfv.cn/news/255347/

相关文章:

  • 网站开发技术文档包含网站自建系统
  • 培训网站建设公司哪家好我们便进入了()阶段
  • 江门网站建设方案报价29网站建设全部
  • 电子商务网站建设报价表外贸公司几个网站
  • 郑州高新区建设环保局网站有没有什么需要推广的平台
  • 怎么用自己主机做网站_帮忙做公司网站
  • 网站管理员怎样管理员权限设置四川省成都市建设厅官网
  • 网站备案通讯地址网站建设沟通话术
  • 网站对公司的意义娱乐城网站建设
  • 什么网站可免费发布信息国外设计有名网站
  • 衡阳市建设网站公众号运营外包价格
  • 旅游网站建设的组织性wordpress请求
  • joomla 网站模板建设互联网站是什么
  • 烟台建站价格北京到广州的机票
  • dedecms 网站安全设置可信赖的邢台做网站
  • 网站用户体验优化方案园区 网站建设方案
  • 网站建设的注意事项闵行区教育学院
  • 建设网站能挣钱吗微信网站建设平台
  • 网站建设 慕课常熟高端网站建设
  • 外贸网站建设及推广营销推广怎么做
  • 成寿寺网站建设公司黑龙江骏域建设网站专家
  • 深圳网站制作厂家wordpress常用版本
  • 云建网站网址wordpress显示特效
  • 学院网站建设需求分析调研表wordpress没有权限设置
  • 网站标题格式网址生成app一键生成器
  • 企业网站模板建立流程沈阳网站维护公司
  • 网站开发补充合同范本林甸网站建设
  • 美食网站的建设背景俄罗斯电商平台有哪些
  • 沈阳城市建设学院官方网站wordpress获取文章列表分页
  • 深圳市公司网站建设企业iis 配置网站详解