synchronized(this)区别于synchronized(myclass-class)

(1) synchronized (myclass-class) for a static method

static void myMethod() {
  synchronized(MyClass.class) {
    //code
  }
}

 

Note: The static method can only be synchronized (MyClass.class), can not be synchronized (this). idea with synchronized (this) being given.

 

Equivalent to:

static synchronized void myMethod() {
  //code
}

 

synchronized (MyClass.class) Test Examples, since the method is static, the class may be invoked, it may also be invoked instance:

Four threads, two calls by way of example, two classes by calling

 

public  class MyClass { 

    public  static  void myMethod () {
         the try {
             the synchronized (MyClass. class ) { 
                String name = Thread.currentThread () getName ();. 
                System.out.println (name + "threads are beginning the task." );
                 for ( int I = 0; I <. 5; I ++ ) { 
                    the Thread.sleep ( 1000 ); 
                    System.out.println (name + "task is being executed." ); 
                } 
                System.out.println (name+ "线程任务执行完毕。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread1();
        thread1.setName("thread1");
        Thread thread2 = new Thread2();
        thread2.setName("thread2");
        Thread thread3 = new Thread3();
        thread3.setName("thread3");
        Thread thread4 = new Thread4();
        thread4.setName("thread4");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}

class Thread1 extends Thread {
    @Override
    public void run() {
        MyClass.myMethod();
    }
}

class Thread2 extends Thread {
    @Override
    public void run() {
        MyClass.myMethod();
    }
}

class Thread3 extends Thread {
    @Override
    public void run() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}

class Thread4 extends Thread {
    @Override
    public void run() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}

 

Output:

thread2 thread is beginning the task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 threaded task is finished. 
thread4 thread is beginning the task. 
thread4 is performing a task. 
thread4 is performing a task. 
thread4 is performing a task. 
thread4 is performing a task. 
thread4 is performing a task. 
thread4 threaded task is finished. 
thread3 thread is beginning the task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 threaded task is finished. 
thread1 thread is beginning the task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 threaded task is finished.

 

As it can be seen, in a static method

synchronized(MyClass.class) {

    //code

}

It can only be accessed by a thread.

 

(2) synchronized (myclass-class) Examples of the method

This is the only time a call by creating an instance.

demo test code:

public  class MyClass { 

    public  void myMethod () {
         the try {
             the synchronized (MyClass. class ) { 
                String name = Thread.currentThread () getName ();. 
                System.out.println (name + "threads are beginning the task." );
                 for ( int I = 0; I <. 5; I ++ ) { 
                    the Thread.sleep ( 1000 ); 
                    System.out.println (name + "task is being executed." ); 
                } 
                System.out.println (name + "the task is completed thread . ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final MyClass myClass = new MyClass();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                myClass.myMethod();
            }
        });
        thread1.setName("thread1");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                myClass.myMethod();
            }
        });
        thread2.setName("thread2");
        Thread3 thread3 = new Thread3();
        thread3.setName("thread3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Thread3 extends Thread {
    @Override
    public void run() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}

 

thread1 thread is beginning the task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 threaded task is finished. 
thread3 thread is beginning the task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 threaded task is finished. 
thread2 thread is beginning the task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 threaded task is finished.

 

Examples here are three, two and thread2 share a thread1, thread3 their open an example. It found that only one thread access. Others must wait. As in the case with (1).

 

(3) synchronized (this) is located in an instance method. (Static methods can not use this)

 

Test demo: is the (2) in the synchronized (MyClass.class) into synchronized (this)

public  class MyClass { 

    public  void myMethod () {
         the try {
             the synchronized ( the this ) { 
                String name = Thread.currentThread () getName ();. 
                System.out.println (name + "threads are beginning the task." );
                 for ( int 0 = I; I <. 5; I ++ {) 
                    the Thread.sleep ( 1000 ;) 
                    System.out.println (name + "task is being executed." ); 
                } 
                System.out.println (name + "thread task is completed." );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final MyClass myClass = new MyClass();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                myClass.myMethod();
            }
        });
        thread1.setName("thread1");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                myClass.myMethod();
            }
        });
        thread2.setName("thread2");
        Thread3 thread3 = new Thread3();
        thread3.setName("thread3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Thread3 extends Thread {
    @Override
    public void run() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}

 

Output 1:

thread2 thread is beginning the task. 
thread3 thread is beginning the task. 
thread2 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread2 is performing a task. 
thread3 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread3 threaded task is finished. 
thread2 is performing a task. 
thread2 threaded task is finished. 
thread1 thread is beginning the task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 is performing a task. 
thread1 threaded task is finished.

 

Output 2

thread3 thread is beginning the task. 
thread1 thread is beginning the task. 
thread1 is performing a task. 
thread3 is performing a task. 
thread1 is performing a task. 
thread3 is performing a task. 
thread1 is performing a task. 
thread3 is performing a task. 
thread3 is performing a task. 
thread1 is performing a task. 
thread3 is performing a task. 
thread3 threaded task is finished. 
thread1 is performing a task. 
thread1 threaded task is finished. 
thread2 thread is beginning the task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 is performing a task. 
thread2 threaded task is finished.

 

It can be seen thread1 and thread2 are pinned, one after the other. thread3 unaffected.

 

to sum up:

1. synchronized (myclass-class) can be used in a static method or instance method, it can be called by the name of the class or instance, but only one thread will perform, other waiting.

2. synchronized (this) only in an instance method invocation, this time if it is the same instance, it will contain each other, if it is different instances, it will not affect each other.

Guess you like

Origin www.cnblogs.com/chenmz1995/p/12111467.html