1 未加 Synchronized 关键字

package com.didi.sec.synchronized_learn;

/**
 * Author:  heatdeath
 * Date:    2018/3/18
 * Desc:
 */
public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public void method_1() {
        try {
            System.out.println(Thread.currentThread().getName() + " begin:");
            System.out.println("begin time= " + System.currentTimeMillis());
            Thread.sleep(2000);
            System.out.println("end    end= " + System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName() + " end.");
            System.out.println("------------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_代码块

两线程以并行方式执行


2 在非 static 方法上加 Synchronized 关键字

package com.didi.sec.synchronized_learn;

/**
 * Author:  heatdeath
 * Date:    2018/3/18
 * Desc:
 */
public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public synchronized void method_1() {
        try {
            System.out.println(Thread.currentThread().getName() + " begin:");
            System.out.println("begin time= " + System.currentTimeMillis());
            Thread.sleep(2000);
            System.out.println("end    end= " + System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName() + " end.");
            System.out.println("------------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_ide_02

两线程以串行方式执行


3 在非 static 方法的内部代码块加上 synchronized(this)

public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public void method_1() {
        try {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " begin:");
                System.out.println("begin time= " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end    end= " + System.currentTimeMillis());
                System.out.println(Thread.currentThread().getName() + " end.");
                System.out.println("------------");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_ide_03


4 在 static 方法前加上 synchronized 关键字

public class SynchronizedMethod {
    public static void main(String[] args) {
//        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                TestClass_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                TestClass_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public synchronized static void method_1() {
        try {
            System.out.println(Thread.currentThread().getName() + " begin:");
            System.out.println("begin time= " + System.currentTimeMillis());
            Thread.sleep(2000);
            System.out.println("end    end= " + System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName() + " end.");
            System.out.println("------------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_System_04


5 synchronized (TestClass_1.class) static 方法

public class SynchronizedMethod {
    public static void main(String[] args) {
//        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                TestClass_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                TestClass_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public static void method_1() {
        try {
            synchronized (TestClass_1.class) {
                System.out.println(Thread.currentThread().getName() + " begin:");
                System.out.println("begin time= " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end    end= " + System.currentTimeMillis());
                System.out.println(Thread.currentThread().getName() + " end.");
                System.out.println("------------");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_System_05


6 synchronized (TestClass_1.class) 非 static 方法

public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public void method_1() {
        try {
            synchronized (TestClass_1.class) {
                System.out.println(Thread.currentThread().getName() + " begin:");
                System.out.println("begin time= " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end    end= " + System.currentTimeMillis());
                System.out.println(Thread.currentThread().getName() + " end.");
                System.out.println("------------");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_ide_06


7

public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();

        final TestClass_1 case_2 = new TestClass_1();

        new Thread() {
            @Override
            public void run() {
                case_2.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    public void method_1() {
//        System.out.println("hahahaha");
        try {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " begin:");
                System.out.println("begin time= " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end    end= " + System.currentTimeMillis());
                System.out.println(Thread.currentThread().getName() + " end.");
                System.out.println("------------");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        System.out.println("wwwwwwww");
    }
}

Synchronized 关键字中的类锁、对象锁_System_07


8

public class SynchronizedMethod {
    public static void main(String[] args) {
        final TestClass_1 case_1 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
        final TestClass_1 case_2 = new TestClass_1();
        new Thread() {
            @Override
            public void run() {
                case_1.method_1();
            }
        }.start();
    }
}

class TestClass_1 {
    private static Integer val = 0;
    public void method_1() {
        try {
            synchronized (val) {
                System.out.println(Thread.currentThread().getName() + " begin:");
                System.out.println("begin time= " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("end    end= " + System.currentTimeMillis());
                System.out.println(Thread.currentThread().getName() + " end.");
                System.out.println("------------");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Synchronized 关键字中的类锁、对象锁_代码块_08