博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(原)测试 Java中Synchronized锁定对象的用法
阅读量:5992 次
发布时间:2019-06-20

本文共 3395 字,大约阅读时间需要 11 分钟。

今天再android_serial_port中看到了关键字 synchronized;因为刚好在学java和android,所以就查了一下它的用法:

于是把代码中的一小段代码拿了出来,做了一下修改,测试了下,结果出现的情况:

1 public class syncThreadDemo { 2  3     public static void main(String[] args) { 4         // TODO Auto-generated method stub 5         /*测试1*/ 6 //        Account account = new Account("zhang san", 10000.0f); 7 //        AccountOperator accountOperator = new AccountOperator(account); 8 // 9 //        final int THREAD_NUM = 5;10 //        Thread threads[] = new Thread[THREAD_NUM];11 //        for (int i = 0; i < THREAD_NUM; i ++) {12 //           threads[i] = new Thread(accountOperator, "Thread" + i);13 //           threads[i].start();14 //        }15         16         /*测试2*/17         18         Thread thread1=new Thread(new syncThreadDemo().new SendingThread("send1"),"thread1");19         thread1.start();20         Thread thread2=new Thread(new syncThreadDemo().new ReceiveThread("recv1"),"thread2");21         thread2.start();22         23     }24     /*创建一个内部类来测试synchronized锁定一个对象*/25     static Object mByteReceivedBackSemaphore = new Object();26 27     private class SendingThread implements Runnable{28         private int i=0;29         private String name;30         //创建一个构造函数31         public SendingThread(String strName) {32             // TODO Auto-generated constructor stub33             this.name=strName;34         }35         36         @Override37         public void run(){38             while (i<10) {39                 synchronized (mByteReceivedBackSemaphore) {40                     try {41                         // Wait for 100ms before sending next byte, or as soon as42                         // the sent byte has been read back.43                         System.out.println(Thread.currentThread().getName()+": wait");44                         mByteReceivedBackSemaphore.wait(100);45                         i++;46                         System.out.println(Thread.currentThread().getName()+":"+i);47                     } 48                     catch (Exception e) {49                         // TODO: handle exception50                     }    51                 }52             }53         }54      }55 56      private class ReceiveThread implements Runnable{57         private int j=0;58         private String name;59         60         public ReceiveThread(String strName) {61             // TODO Auto-generated constructor stub62             this.name=strName;63         }64         65         @Override66         public void run(){67             while(j<10){68                 synchronized(mByteReceivedBackSemaphore){69                     System.out.println(Thread.currentThread().getName()+": notify");70                     mByteReceivedBackSemaphore.notify();    71                     j++;72                     System.out.println(Thread.currentThread().getName()+":"+j);73                 }74             }75         }76      }77 }
View Code

测试结果:

thread1: waitthread2: notifythread2:1thread2: notifythread2:2thread2: notifythread2:3thread2: notifythread2:4thread2: notifythread2:5thread2: notifythread2:6thread2: notifythread2:7thread2: notifythread2:8thread2: notifythread2:9thread2: notifythread2:10thread1:1thread1: waitthread1:2thread1: waitthread1:3thread1: waitthread1:4thread1: waitthread1:5thread1: waitthread1:6thread1: waitthread1:7thread1: waitthread1:8thread1: waitthread1:9thread1: waitthread1:10
View Code

 

转载地址:http://artlx.baihongyu.com/

你可能感兴趣的文章
本人整理的前端规范
查看>>
oracle数据库服务器(内存减小导致Oracle启动不了,解决方法)
查看>>
Unknown system variable 'language'
查看>>
linux远程桌面vnc服务配置详细教程
查看>>
怎么能让while循环变成死循环,无限的输出
查看>>
查看LINUX操作系统相关信息命令
查看>>
Python学习:paramiko模块(1)SSH登陆
查看>>
Windows Thin PC中文化
查看>>
javascript 实现垂直展开压缩动画
查看>>
SQL Server中的Merge关键字
查看>>
对于公司组建Hadoop基础框架的推荐分析
查看>>
我的友情链接
查看>>
c# 构造函数初探
查看>>
VMware vSphere6.0(一)----环境介绍
查看>>
xDAIS VISA API
查看>>
信仰为何?
查看>>
邮件组织维护的列表
查看>>
dot1x之PEAP和EAP-TLS----一
查看>>
【深入浅出MyBatis系列四】强大的动态SQL
查看>>
我的友情链接
查看>>