当synchronized关键字和this关键字
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
当synchronized关键字和this关键字的更多相关文章
- SQLSERVER中的ALL、PERCENT、CUBE关键字、ROLLUP关键字和GROUPING函数
SQLSERVER中的ALL.PERCENT.CUBE关键字.ROLLUP关键字和GROUPING函数 先来创建一个测试表 USE [tempdb] GO )) GO INSERT INTO [#te ...
- 方法重写和方法重载;this关键字和super关键字
1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...
- java中this关键字和static关键字和super关键字的用法
this关键字 1. this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性: 2.可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数(这里面上面有个 ...
- SEO之网站关键词的优化 :首页,内页关键字,长尾关键字
这篇文章主要讲的是SEO之网站关键词的优化 :首页,内页关键字,长尾关键字. 为了查找方便,小A汇总了所有SEO优化的相关教程,方便大家查找到自己想要的SEO优化技巧: SEO优化教程汇总. 网站关键 ...
- 构造方法,this关键字,static关键字,封装,静态变量
1.构造方法 构造方法是一种特殊的方法,是专门用于创建/实例化对象的方法. 构造方法根据是否有参数分为两类:1.无参构造方法 2.有参构造方法 1.1无参构造方法 无参构造方法就是构造方法中没有参数 ...
- 《Java编程思想》读书笔记-基本规范、注释、static关键字、import关键字
扫一扫加我的微信公众号,和我一起打好Java的基础 本文作为构建第一个Java程序的番外篇二,主要跟大家伙儿从浅层次的探讨下Java中的关键字import和static,此外为了让我们的代码可读性更强 ...
- Elasticsearch 关键字与SQL关键字对比总结
由于Elasticsearch和MongoDB/Redis/Memcache一样,是非关系型数据库.而平常使用的MySql,Oracle,SQLServer 等为关系型数据库,二者有着本质的区别,Es ...
- python保留关键字和常用关键字
python保留关键字和常用关键字如下: 上图是python3中的关键字,python2.7中的关键字部分会有区别,具体在自己打印输出查看: import keyword print ' '.join ...
- [java]final关键字、finally关键字与finalize()方法
final关键字: final关键字通常指的是“无法改变的”,使用“无法改变”这样修饰可能出于两个原因:设计或者效率. final可以修饰变量.方法和类. 一.final变量 一个既是static又是 ...
- php中的self关键字和this关键字的区别和联系
php中的self关键字和this关键字的区别和联系 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程 ...
随机推荐
- Windows 无法验证此设备所需的驱动程序的数字签名。某软件或硬件最近有所更改,可能安装了签名错误或损毁的文件,或者安装的文件可能是来路不明的恶意软件。(代码52)
由未签名驱动导致的键鼠装无法使用的问题 usb 问题失效. 要是win 10的话 导致的结果就是 无线键鼠套装无法使用. 解决办法是 1.按下shift 按键 点击重启按钮 重启后 2.疑难解答-- ...
- xargs: How To Control and Use Command Line Arguments
参考: http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists-utility/ http://linux ...
- Cesium 事件
1.相机事件(移动开始.移动结束等等) viewer.scene.camera.moveEnd.addEventListener(function(){ }): 2.鼠标事件(单击.移动.右键等) v ...
- Android Studio打包:“APP_NAME" IS NOT TRANSLATED IN ZH, ZH_CN……..解决办法
开始用Android Studio更新到2.0稳定版,调试的时候没啥问题,在打包的时候出现了"app_name" is not translated in zh, zh_CN….. ...
- 北京极科极客科技有限公司 http://www.hiwifi.com/
北京极科极客科技有限公司 http://www.hiwifi.com/ 产品:hiwifi 199元.
- 数组中的push()和pop()方法
push()方法可以接受任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度. pop()方法是从数组末尾移除最后一项,减小数组的length值,然后返回移除的项. var arr = [ ...
- Kafka server.properties配置,集群部署
server.properties中所有配置参数说明(解释) broker.id =0每一个broker在集群中的唯一表示,要求是正数.当该服务器的IP地址发生改变时,broker.id没有变化,则不 ...
- Docker解析及轻量级PaaS平台演练(一)--Docker简介与安装
Container技术: 传统的虚拟化技术: 通过对硬件层模拟,从而实现了能够在一套硬件上面运行多个操作系统,因为通过硬件虚拟化,使得操作系统认为在它之下就是硬件层 但是实际情况是这样的:虚拟机中的O ...
- Robomongo与MongoDB的故事
Robomongo,Mongo可视化工具 哇唔,事实上她是三(阴险脸). 你看你看,界面清新,让人家心旷神怡(害羞).谁还想win+R+mongo呀呀呀?! 哎呀呀,继续···说正事. 在这里···借 ...
- ES标准中的相等比较算法 SameValue SameValueZero
1.相等比较算法 The Abstract Equality Comparison Algorithm (==) The Strict Equality Comparison Algorithm (= ...