当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的高级编程 ...
随机推荐
- Problem C: 零起点学算法18——3个数比较大小
#include<stdio.h> int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,& ...
- Java高级架构师(一)第11节:Mybatis的分页实现
- iOS开发之指定UIView的某几个角为圆角
我们知道, 如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架).而若要指定某几个角(小于4)为圆角 ...
- iOS中设置backBarButtonItem的title和action
一. 设置title 在需要显示该返回键的前一个Controller中设置: 1: navigationItem.backBarButtonItem = UIBarButtonItem(title: ...
- ZooKeeper服务器是用Java创建的,它在JVM上运行。
ZooKeeper服务器是用Java创建的,它在JVM上运行. 创建配置文件 使用命令 vi conf/zoo.cfg 和所有以下参数设置为起点,打开名为 conf/zoo.cfg 的配置文件. $ ...
- [SVN] svn在linux下的使用(svn命令行)ubuntu 删除 新增 添加 提交 状态查询 恢复
转载自:http://www.cnblogs.com/xulb597/archive/2012/07/18/2597311.html 合并步骤:(1)先切换到分支:(2)svn merge trunk ...
- Linux下sqlite3编程
---------------------------------------------------------------------------------------------------- ...
- 面试题:判断两个字符串是否互为回环变位(Circular Rotaion)
题干: 如果字符串 s 中的字符循环移动任意位置之后能够得到另一个字符串 t,那么 s 就被称为 t 的回环变位(circular rotation). 例如,ACTGACG 就是 TGACG ...
- HBase 写优化之 BulkLoad 实现数据快速入库
在第一次建立Hbase表的时候,我们可能需要往里面一次性导入大量的初始化数据.我们很自然地想到将数据一条条插入到Hbase中,或者通过MR方式等.但是这些方式不是慢就是在导入的过程的占用Region资 ...
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...