(原)测试 Java中Synchronized锁定对象的用法
今天再android_serial_port中看到了关键字 synchronized;因为刚好在学java和android,所以就查了一下它的用法:
于是把代码中的一小段代码拿了出来,做了一下修改,测试了下,结果出现的情况:
public class syncThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*测试1*/
// Account account = new Account("zhang san", 10000.0f);
// AccountOperator accountOperator = new AccountOperator(account);
//
// final int THREAD_NUM = 5;
// Thread threads[] = new Thread[THREAD_NUM];
// for (int i = 0; i < THREAD_NUM; i ++) {
// threads[i] = new Thread(accountOperator, "Thread" + i);
// threads[i].start();
// }
/*测试2*/
Thread thread1=new Thread(new syncThreadDemo().new SendingThread("send1"),"thread1");
thread1.start();
Thread thread2=new Thread(new syncThreadDemo().new ReceiveThread("recv1"),"thread2");
thread2.start();
}
/*创建一个内部类来测试synchronized锁定一个对象*/
static Object mByteReceivedBackSemaphore = new Object();
private class SendingThread implements Runnable{
private int i=0;
private String name;
//创建一个构造函数
public SendingThread(String strName) {
// TODO Auto-generated constructor stub
this.name=strName;
}
@Override
public void run(){
while (i<10) {
synchronized (mByteReceivedBackSemaphore) {
try {
// Wait for 100ms before sending next byte, or as soon as
// the sent byte has been read back.
System.out.println(Thread.currentThread().getName()+": wait");
mByteReceivedBackSemaphore.wait(100);
i++;
System.out.println(Thread.currentThread().getName()+":"+i);
}
catch (Exception e) {
// TODO: handle exception
}
}
}
}
}
private class ReceiveThread implements Runnable{
private int j=0;
private String name;
public ReceiveThread(String strName) {
// TODO Auto-generated constructor stub
this.name=strName;
}
@Override
public void run(){
while(j<10){
synchronized(mByteReceivedBackSemaphore){
System.out.println(Thread.currentThread().getName()+": notify");
mByteReceivedBackSemaphore.notify();
j++;
System.out.println(Thread.currentThread().getName()+":"+j);
}
}
}
}
}
测试结果:
thread1: wait
thread2: notify
thread2:1
thread2: notify
thread2:2
thread2: notify
thread2:3
thread2: notify
thread2:4
thread2: notify
thread2:5
thread2: notify
thread2:6
thread2: notify
thread2:7
thread2: notify
thread2:8
thread2: notify
thread2:9
thread2: notify
thread2:10
thread1:1
thread1: wait
thread1:2
thread1: wait
thread1:3
thread1: wait
thread1:4
thread1: wait
thread1:5
thread1: wait
thread1:6
thread1: wait
thread1:7
thread1: wait
thread1:8
thread1: wait
thread1:9
thread1: wait
thread1:10
(原)测试 Java中Synchronized锁定对象的用法的更多相关文章
- java中synchronized的用法详解
记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchron ...
- java中synchronized的使用方法与具体解释
Java语言的keyword.当它用来修饰一个方法或者一个代码块的时候,可以保证在同一时刻最多仅仅有一个线程运行该段代码. 一.当两个并发线程訪问同一个对象object中的这个synchronized ...
- Java 中 synchronized的用法详解(四种用法)
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码.本文给大家介绍java中 synchronized的用法,对本文感兴趣的朋友一起看看吧 ...
- java中 synchronized 的使用,确保异步执行某一段代码。
最近看了个有关访问网络url和下载的例子,里面有几个synchronized的地方,系统学习下,以下内容很重要,记下来. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一 ...
- JAVA 中 synchronized 详解
看到一篇关于JAVA中synchronized的用法的详解,觉得不错遂转载之..... 原文地址: http://www.cnblogs.com/GnagWang/archive/2011/02/27 ...
- Java中synchronized 修饰在static方法和非static方法的区别
[问题描述]关于Java中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...
- java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...
- Java中的函数对象
初次听说java中的函数对象可能,比较的陌生.可以类比着来理解一下,人们常说java中没有了指针,殊不知,java中的对象引用就是指针,有时候我们说一个对象往往指的就是这个对象的引用,也就是说基本上把 ...
- (转)java中对集合对象list的几种循环访问总结
Java集合的Stack.Queue.Map的遍历 在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...
随机推荐
- COGS.264.数列操作(分块 单点加 区间求和)
题目链接 #include<cmath> #include<cstdio> #include<cctype> #include<algorithm> u ...
- 潭州课堂25班:Ph201805201 爬虫基础 第三课 urllib (课堂笔记)
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了url ...
- 潭州课堂25班:Ph201805201 第八课:函数基础和函数参数 (课堂笔记)
1, 函数定义 def fun(): print('测试函数') fun() #调用函数 return 运行函数返回值 def fun(): name = [1,3,4,5] return name[ ...
- centos6.9 忘记密码解决方法
若果忘记了 root 的密码,解决方法如下: 我采用的 linux 版本是 centos-6.9 , 经过亲身实践证明,该方法是 ok 的 在开机启动的时候按键盘上的“E”键会进入如下界面. 选择相应 ...
- JDBC(3)—ResultSet结果集
简介:ResultSet:结果集.封装了使用JDBC进行查询的结果.Statement只能进行更新操作,所以使用ResultSet进行查询操作. 1.调用Statement对象的executeQuer ...
- unity 打包编译记录
1.放到Plugins目录下的贴图不会打包进去 2.放到Plugins目录下的dll会自动打包,代码也会打包 3.放在Resources目录下的资源会自动打包 4.放在StreamingAssets目 ...
- 使用sshfs挂载远程服务器目录
点击访问原文 您还可以加入全栈技术交流群(QQ群号:254842154) 服务器日志查看,是开发人员和服务器运维人员在工作中经常会遇到的一件事情,只有一台服务器时,比较好办,直接登录服务器使用tail ...
- android之官方导航栏ActionBar(三)之高仿优酷首页
一.问题概述 通过上两篇文章,我们对如何使用ActionBar大致都已经有了认识.在实际应用中,我们更多的是定制ActionBar,那么就需要我们重写或者定义一些样式来修饰ActionBar,来满足具 ...
- sql server 多表关联更新 update
update a set a.KSMC = b.name from JC_KSXXB a inner join chisdb_ymyy..zd_unit_code b on a.KSDM = b.co ...
- 如何查看memcache的性能
memcache的运行状态可以方便的用 stats 命令显示.首先用telnet 127.0.0.1 11211这样的命令连接上memcache,然后直接输入stats就可以得到当前memcache的 ...