(原)测试 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类型的遍历,还是有一 ...
随机推荐
- POJ.1769.Minimizing maximizer(线段树 DP)
题目链接 /* 题意:有m个区间,问最少要多少个区间能覆盖[1,n] 注:区间要按原区间的顺序,不能用排序贪心做 设dp[i]表示最右端端点为i时的最小值 dp[e[i]]=min{dp[s[i]]~ ...
- Cesiumjs初学第一天
官网demo地址: https://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Geometry%20and%20Appearances.html&lab ...
- php 替换二维数组的 key
php 替换二维数组中的 key // 需要替换 key 的数组 $arr_old = array( '0' => array('id' => 1, 'name' => 'Carro ...
- pho文件操作
php文件操作函数 readfile - 适用于打开一个文件并读取文件的内容 echo readfile('e:/webdictionary.txt'); fopen('文件名','打开模式') - ...
- svnkit https 忽略证书认证
直接上代码 解决jdk版本问题:Security.setProperty("jdk.tls.disabledAlgorithms", ""); import j ...
- redis源码分析
我阅读的源码版本是redis-2.8.19 src目录下总共96个.h,.c文件 1. 数据结构相关源码(15个左右)字符串代码: sds.h, sds.c字典:dict.h, dict.c链表: a ...
- JSON序列——根据JSON生成事务性SQL2
JSON序列——根据JSON生成事务性SQL2 procedure TForm1.Button3Click(Sender: TObject); begin var json:string:=''+ ' ...
- RxJava2 源码解析(二)
概述 承接上一篇RxJava2 源码解析(一),本系列我们的目的: 知道源头(Observable)是如何将数据发送出去的. 知道终点(Observer)是如何接收到数据的. 何时将源头和 ...
- 浅谈压缩感知(二十三):压缩感知重构算法之压缩采样匹配追踪(CoSaMP)
主要内容: CoSaMP的算法流程 CoSaMP的MATLAB实现 一维信号的实验与结果 测量数M与重构成功概率关系的实验与结果 一.CoSaMP的算法流程 压缩采样匹配追踪(CompressiveS ...
- 【Little Demo】从简单的Tab标签到Tab图片切换
Tab标签切换效果是比较流行的一种网站页面布局,视觉表现为美观大方,通过标签展示内容.目前在各大网站都有存在这种效果.例如:淘宝的黄金位置使用Tab标签切换效果,网易新闻等. 1.简单的 Tab 标签 ...