当synchronized关键字和this关键字

Posted 牵牛花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当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关键字调这个方法那么同步锁就没有加上同时代码不会报任何错误

ps:用的是jdk1.8

以上是关于当synchronized关键字和this关键字的主要内容,如果未能解决你的问题,请参考以下文章

synchronized修饰普通方法和静态方法

Java synchronized关键字学习二

Java基础-synchronized关键字的用法(转载)

JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this

java 多线程 Synchronized方法和方法块 synchronized(this)和synchronized(object)的理解

synchronized 加在java方法前面是啥作用