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

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();
        }
    }
}

这里写图片描述

两线程以并行方式执行


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();
        }
    }
}

这里写图片描述

两线程以串行方式执行


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();
        }
    }
}

这里写图片描述


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();
        }
    }
}

这里写图片描述


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();
        }
    }
}

这里写图片描述


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();
        }
    }
}

这里写图片描述


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");
    }
}

这里写图片描述


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();
        }
    }
}

这里写图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值