(原)测试 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类型的遍历,还是有一 ...
随机推荐
- luogu P2934 [USACO09JAN]安全出行Safe Travel
题目链接 luogu P2934 [USACO09JAN]安全出行Safe Travel 题解 对于不在最短路树上的边(x, y) 1 | | t / \ / \ x-----y 考虑这样一种形态的图 ...
- php curl请求https 返回无结果|false|errno:35
1 SSL: certificate subject name 'WMSvc-GWAMSERVER02' does not match target host name 把curl_setopt($c ...
- BZOJ3861 : Tree
把集合看成左边的点,图中的点看成右边的点,若集合$i$不包含$j$,则连边$i->j$,得到一个二分图,等价于求这个二分图的完备匹配个数. 设$f[i][j]$表示考虑了前$i$个集合,匹配了$ ...
- 一段用c#操作datatable的代码
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...
- Android笔记(五):广播接收者(Broadcast Receiver)
Android有四大组件,分别为:Activity(活动).Service(服务).Content Provider(内容提供器).Broadcast Receiver(广播接收者). 引入广播的目的 ...
- 各种Oracle索引类型介绍
逻辑上:Single column 单行索引Concatenated 多行索引Unique 唯一索引NonUnique 非唯一索引Function-based函数索引Domain 域索引 物理上:Pa ...
- Mybatis 中遍历map 参数中的 list 和 array 属性
原文:https://blog.csdn.net/liudongdong0909/article/details/51048835 问题在项目有中遇到批量删除操作时,需要根据两个属性去删除数据,其中一 ...
- Linux C Socket TCP编程介绍及实例
转自:https://blog.csdn.net/lell3538/article/details/53335231 { printf("向服务器发送数据:%s\n",sendbu ...
- Windows IIS注册asp 此操作系统版本不支持此选项 错误解决方法
更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...
- p中不能包含div
一句话:有些块元素不可以包含另一些块元素 ,DTD中规定了块级元素是不能放在P里;P标签内包含块元素时,它会先结束自己,比如:<*p><*div>测试p包含div<*/d ...