今天再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锁定对象的用法的更多相关文章

  1. java中synchronized的用法详解

    记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchron ...

  2. java中synchronized的使用方法与具体解释

    Java语言的keyword.当它用来修饰一个方法或者一个代码块的时候,可以保证在同一时刻最多仅仅有一个线程运行该段代码. 一.当两个并发线程訪问同一个对象object中的这个synchronized ...

  3. Java 中 synchronized的用法详解(四种用法)

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码.本文给大家介绍java中 synchronized的用法,对本文感兴趣的朋友一起看看吧 ...

  4. java中 synchronized 的使用,确保异步执行某一段代码。

    最近看了个有关访问网络url和下载的例子,里面有几个synchronized的地方,系统学习下,以下内容很重要,记下来. Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一 ...

  5. JAVA 中 synchronized 详解

    看到一篇关于JAVA中synchronized的用法的详解,觉得不错遂转载之..... 原文地址: http://www.cnblogs.com/GnagWang/archive/2011/02/27 ...

  6. Java中synchronized 修饰在static方法和非static方法的区别

    [问题描述]关于Java中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...

  7. java中对集合对象list的几种循环访问

    java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...

  8. Java中的函数对象

    初次听说java中的函数对象可能,比较的陌生.可以类比着来理解一下,人们常说java中没有了指针,殊不知,java中的对象引用就是指针,有时候我们说一个对象往往指的就是这个对象的引用,也就是说基本上把 ...

  9. (转)java中对集合对象list的几种循环访问总结

    Java集合的Stack.Queue.Map的遍历   在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...

随机推荐

  1. “IT学子成长指导”专栏及文章目录 —贺利坚

    迂者专栏关键词 就 业 大一 大二 大三 大四 自学 职 场 专业+兴趣 研究生 硕士 规 划 考 研 大学生活 迷 茫 计算机+专业 基本功 学习方法 编程 基 础 实践 读书 前 途 成 长 社团 ...

  2. PHP Zend Email验证函数MailVal()函数的使用

    PHP Email验证 <?php /************************************************************************ *此功能检 ...

  3. C++学习笔记41:进程调度

    进程调度策略:先进先出,时间片轮转,普通调度,批调度,高优先级抢先 子进程与父进程的调度没有固定的顺序:不能假设子进程一定会在父进程之后执行,也不能假设子进程一定会在父进程之前执行: 僵尸进程 子进程 ...

  4. c# 上传图片流,php端(laravel框架)接收处理方法

    c# httppost方法 public struct PostFile { public string name; public string filename; public Stream bit ...

  5. B - 可能的路径(gcd变形)

    https://vjudge.net/contest/218366#problem/B 要不是在数学题专题里,我估计就盲目搜索了.10^18范围1s应该过不去. 再细看能感觉到是gcd的变形,但是具体 ...

  6. android fastjson 解析

    JSONObject jsonObject = JSON.parseObject(wsResponse);String recommends = jsonObject.getString(" ...

  7. Nginx代理proxy pass配置去除前缀

    使用Nginx做代理的时候,可以简单的直接把请求原封不动的转发给下一个服务. 比如,访问abc.com/appv2/a/b.html, 要求转发到localhost:8088/appv2/a/b.ht ...

  8. PAM 認 證 模 組

    作者:陳柏菁 E-mail 作用:限制哪些用户或者组可以从哪里登陆,或者可以建立/etc/nologin立即阻止一般用户登陆,限制 user可以使用的资源.例如cpu,文件,登陆数量,某些服务的登陆时 ...

  9. ASP.NET MVC 一款可预览、裁剪头像上传组件

    今天介绍一款Web上常用的头像上传组件,常用于头像上传时对用户上传的图片进行裁剪并实时预览,最终效果如下: 源代码结构: Github地址: https://github.com/FrankFan/A ...

  10. Sequel Pro for Mac(MySQL 数据库管理工具)破解版安装

    1.软件简介    Sequel Pro 是一款管理 Mysql 的工具,界面简洁易用. 2.功能特色 FULL MYSQL SUPPORT Sequel Pro is a fast, easy-to ...