今天再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. 打包maven后出现jar包丢失

    http://blog.csdn.net/asdfsfsdgdfgh/article/details/51373222

  2. Shell中的>/dev/null 2>&1 与 2>&1 >/dev/null 与&>/dev/null 的区别

    默认情况下,总是有三个文件处于打开状态,标准输入(键盘输入).标准输出(输出到屏幕).标准错误(也是输出到屏幕),它们分别对应的文件描述符是0,1,2 .那么我们来看看下面的几种重定向方法的区别: & ...

  3. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

    报错: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlace ...

  4. __getattr__和__setattt__使用

    # coding:utf-8 """ __setattr__(self, name, value),如果要给name赋值,调用此方法 __getattr__(self, ...

  5. 封装PHP验证码方法

    /** * 生成验证码 * @param integer $width 图片宽度 * @param integer $height 图片高度 * @param string $type 验证码类型 * ...

  6. ios webview下纯JS实现长按

    app,其中有长按LI列表弹出菜单,只要清楚五个方法就行:ontouchstart.ontouchmove.ontouchend.setTimeout.clearTimeout 1.首先在我们按下手指 ...

  7. 微信小程序 scroll-view 实现锚点跳转

    在微信小程序中,使用 scroll-view 实现长页面的标记跳转,官方文档中没有例子演示,锚点标记主要是使用<scroll-view> 的 scroll-into-view 属性. 实现 ...

  8. 解决IDEA授权报错

    今天打开电脑,猛然发现IDEA授权失效,然后重新用账号密码登陆,发现被拒绝,各种百度百思不得其解,抱着试试的态度,点击了重置密码 https://account.jetbrains.com/forgo ...

  9. 教育单元测试mock框架优化之路(中)

    转载:https://sq.163yun.com/blog/article/169564470918451200 三.间接依赖的bean的mock替换 对于前面提供的@Mock,@Spy+@Injec ...

  10. 一起来给iOS 11找bug: 苹果还是乔布斯时代的细节控吗?

    众所周知,前几天苹果在位于苹果公园的Steve Jobs剧院召开了一年一度的新品发布会,正式揭幕了全屏的iPhoneX, 随后又把iOS 11推送给了测试员(Beta Tester)(正式版将于几周后 ...