package cn.itcast_01_mythread.thread.testThread;

public class MyThreadWithImpliment_Synch_method implements Runnable {
int x; public MyThreadWithImpliment_Synch_method(int x) {
this.x = x;
} public synchronized void queryCurrentTime(){
for (int i = 0; i < 10; i++) {
String name = Thread.currentThread().getName();
System.out.println("to "+name + " current is " + i);
/*try {
//Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
} } @Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println("线程" + name + "的run方法被调用……");
this.queryCurrentTime();
//queryCurrentTime();
} public static void main(String[] args) {
Thread thread1 = new Thread(new MyThreadWithImpliment_Synch_method(1), "thread-1");
Thread thread2 = new Thread(new MyThreadWithImpliment_Synch_method(2), "thread-2");
thread1.start();
thread2.start();
// 注意调用run和调用start的区别,直接调用run,则都运行在main线程中
// thread1.run();
// thread2.run();
}
}

this.queryCurrentTime();//执行的结果,结果与预期的一样

线程thread-2的run方法被调用……
to thread-2 current is 0
to thread-2 current is 1
to thread-2 current is 2
to thread-2 current is 3
to thread-2 current is 4
to thread-2 current is 5
to thread-2 current is 6
to thread-2 current is 7
to thread-2 current is 8
to thread-2 current is 9
线程thread-1的run方法被调用……
to thread-1 current is 0
to thread-1 current is 1
to thread-1 current is 2
to thread-1 current is 3
to thread-1 current is 4
to thread-1 current is 5
to thread-1 current is 6
to thread-1 current is 7
to thread-1 current is 8
to thread-1 current is 9

queryCurrentTime();//执行的结果 与预期的不一样!!,好像就没有实现锁的功能

线程thread-1的run方法被调用……
线程thread-2的run方法被调用……
to thread-1 current is 0
to thread-2 current is 0
to thread-1 current is 1
to thread-2 current is 1
to thread-1 current is 2
to thread-2 current is 2
to thread-1 current is 3
to thread-2 current is 3
to thread-1 current is 4
to thread-2 current is 4
to thread-1 current is 5
to thread-2 current is 5
to thread-1 current is 6
to thread-2 current is 6
to thread-1 current is 7
to thread-2 current is 7
to thread-1 current is 8
to thread-2 current is 8
to thread-1 current is 9
to thread-2 current is 9

看疯狂java上说的,对于synchronized修饰的实例方法(非static方法)而言,无须显示指定同步监视器,同步方法监视器是this也就是调用该方法的对象。

所以我的理解是对于synchronized是站在对象的级别的,不是站在方法的级别的,如果直接在同一个类不通过this关键字调这个方法那么同步锁就没有加上同时代码不会报任何错误

synchronized和lock都是多线程的时候调用的,如果在javaweb中使用这些,而线程是有容器(tomcat)产生的不是我们自己产生的,这中做法很难测试,比如单例模式

比较好的做法是在我们自己写代码的时候结合业务需要建立多线程,然后用synchronized或lock

ps:用的是jdk1.8

最新文章

  1. openssl用法详解
  2. linux sed 添加 删除 一行
  3. Scanner演示
  4. iOS开发——设备篇&amp;ios8文件路径
  5. IP地址,子网掩码、默认网关,DNS理论解释
  6. Zkui安装
  7. Linux - 简明Shell编程15 - 调试(Debug)
  8. 扩展Python模块系列(五)----异常和错误处理
  9. linux用户、组、权限问题
  10. 最长k可重区间集问题&amp;&amp;最长k可重线段集问题
  11. c#之枚举,结构体
  12. cv2.SIFT() AttributeError: &#39;module&#39; object has no attribute &#39;SIFT&#39; OpenCV Python can&#39;t use SURF, SIFT
  13. Xcode6中添加pch文件
  14. mysql 数据库简介
  15. 【译】第八篇 SQL Server代理使用外部程序
  16. Hive SQL的编译过程
  17. TJSONTableObject跨平台解析JSON
  18. Linux 进程管理 进程优先级管理
  19. winform 中 MessageBox 用法大全
  20. bzero()函数

热门文章

  1. iOS开发 Swift开发数独游戏(五)显示游戏答案
  2. 【java】File的使用:将字符串写出到本地文件,大小0kb的原因
  3. 说一下集成 diagram-viewer 的心路历程 5.22.0
  4. Virtualbox环境中安装Oracle 11gr2 RAC(ASM)
  5. redis深入研究
  6. .NET中的CTS、CLS、CLR
  7. 一起來玩鳥 Starling Framework(7)MovieClip
  8. 解读Spark Streaming RDD的全生命周期
  9. Linq-语句之Select/Distinct和Count/Sum/Min/Max/Avg
  10. Accessing Report Server using Report Server Web Service - Microsoft SQL Server 2008R2