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关键字的更多相关文章

  1. SQLSERVER中的ALL、PERCENT、CUBE关键字、ROLLUP关键字和GROUPING函数

    SQLSERVER中的ALL.PERCENT.CUBE关键字.ROLLUP关键字和GROUPING函数 先来创建一个测试表 USE [tempdb] GO )) GO INSERT INTO [#te ...

  2. 方法重写和方法重载;this关键字和super关键字

    1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...

  3. java中this关键字和static关键字和super关键字的用法

    this关键字 1. this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性: 2.可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数(这里面上面有个 ...

  4. SEO之网站关键词的优化 :首页,内页关键字,长尾关键字

    这篇文章主要讲的是SEO之网站关键词的优化 :首页,内页关键字,长尾关键字. 为了查找方便,小A汇总了所有SEO优化的相关教程,方便大家查找到自己想要的SEO优化技巧: SEO优化教程汇总. 网站关键 ...

  5. 构造方法,this关键字,static关键字,封装,静态变量

    1.构造方法 构造方法是一种特殊的方法,是专门用于创建/实例化对象的方法. 构造方法根据是否有参数分为两类:1.无参构造方法  2.有参构造方法 1.1无参构造方法 无参构造方法就是构造方法中没有参数 ...

  6. 《Java编程思想》读书笔记-基本规范、注释、static关键字、import关键字

    扫一扫加我的微信公众号,和我一起打好Java的基础 本文作为构建第一个Java程序的番外篇二,主要跟大家伙儿从浅层次的探讨下Java中的关键字import和static,此外为了让我们的代码可读性更强 ...

  7. Elasticsearch 关键字与SQL关键字对比总结

    由于Elasticsearch和MongoDB/Redis/Memcache一样,是非关系型数据库.而平常使用的MySql,Oracle,SQLServer 等为关系型数据库,二者有着本质的区别,Es ...

  8. python保留关键字和常用关键字

    python保留关键字和常用关键字如下: 上图是python3中的关键字,python2.7中的关键字部分会有区别,具体在自己打印输出查看: import keyword print ' '.join ...

  9. [java]final关键字、finally关键字与finalize()方法

    final关键字: final关键字通常指的是“无法改变的”,使用“无法改变”这样修饰可能出于两个原因:设计或者效率. final可以修饰变量.方法和类. 一.final变量 一个既是static又是 ...

  10. php中的self关键字和this关键字的区别和联系

    php中的self关键字和this关键字的区别和联系 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程 ...

随机推荐

  1. 程序调控和监视(Logcat,Debug)

    1.Logcat 2.效果图:实现点击Button,提示Logcat信息 (1)activity_main.xml <?xml version="1.0" encoding= ...

  2. react使用echarts

    1.安装echarts: npm install echarts --save 2.制作线性图组件,只引入echart必要的js内容 /** * Created by yongyuehuang on ...

  3. 手写一个死锁Demo

    相互持有对方锁,导致死锁 package jesse.test04; public class DeadLockSample extends Thread{ private String first; ...

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3083:Children of the Candy Corn(DFS+BFS)

    Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: ...

  6. 火车票订票API 用PHP完成火车票订票流程

    本教程用来演示聚合数据-火车票订票接口的使用流程. 配置好PHP环境,PHP版本最好大于5.5 去聚合数据-火车票订票接口申请key:http://www.juhe.cn/docs/api/id/17 ...

  7. C语言--矩阵置换

    //takePlace里的循环写错了,j循环应该是 //for (j=i;j<3;j++) //你那个写的交换了2遍,又变回原来的了.*// #include <stdio.h> ] ...

  8. [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive.

    1.滑动时候警告[Intervention] Unable to preventDefault inside passive event listener due to target being tr ...

  9. React Native Android入门实战及深入源代码分析系列(2)——React Native源代码编译

    本文为老曾原创.转载需注明出处:viewmode=contents">http://blog.csdn.net/minimicall?viewmode=contents 在上一节中,我 ...

  10. @using (Html.BeginForm()) @using (Ajax.BeginForm(new AjaxOptions() { })) 区别

    @using (Html.BeginForm()) 返回页面 也是页面 都是返回页面 只是 多了一个 data-ajax="true"