白羽
2018-07-09
来源 :网络
阅读 1510
评论 0
摘要:本文将带你了解Java语言之内部锁(synchronized)中类锁和对象锁,希望本文对大家学JAVA有所帮助。
synchronized是Java提供的内部锁,里边有类锁和对象锁;在静态方法中,我们一般使用类锁,在实例方法中,我们一般使用对象锁,接下来,分析类锁和对象锁的区别和联系。
1、两个同步块使用同一个对象锁:
[java] view plain copy
1. public class Main {
2. static Main objMain = new Main();
3. public static void main(String[] args) {
4. new Thread(new Runnable() {
5.
6. @Override
7. public void run() {
8. Main.method2();
9. }
10. }).start();
11. new Thread(new Runnable() {
12.
13. @Override
14. public void run() {
15. Main obj = new Main();
16. obj.method();
17.
18. }
19. }).start();
20. }
21. public static void method2() {
22. synchronized (objMain) {
23.
24. System.out.println("method2......");
25. try {
26. Thread.sleep(2000);
27. } catch (InterruptedException e) {
28. e.printStackTrace();
29. }
30. System.out.println("method2....666..");
31. }
32. }
33.
34. public void method(){
35. synchronized (objMain) {
36. System.out.println("method...");
37. }
38. }
39.
40. }
我们在main方法中启动两个线程,分别执行method2和method方法;结果显示,当前输入在method2先占有对象锁之后,method方法内部将得不到该objMain对象锁,直到method2中执行结束。method方法中才会执行同步块。
说明:两个不同的线程在抢占同一个对象锁。
2、如果我们让两个同步块一个使用类锁一个使用对象锁:
[java] view plain copy
1. public class Main {
2. static Main objMain = new Main();
3. public static void main(String[] args) {
4. new Thread(new Runnable() {
5.
6. @Override
7. public void run() {
8. Main.method2();
9. }
10. }).start();
11. new Thread(new Runnable() {
12.
13. @Override
14. public void run() {
15. Main obj = new Main();
16. obj.method();
17.
18. }
19. }).start();
20. }
21. public static void method2() {
22. synchronized (Main.class) {
23.
24. System.out.println("method2......");
25. try {
26. Thread.sleep(2000);
27. } catch (InterruptedException e) {
28. e.printStackTrace();
29. }
30. System.out.println("method2....666..");
31. }
32. }
33.
34. public void method(){
35. synchronized (objMain) {
36. System.out.println("method...");
37. }
38. }
39.
40. }
此时,并不会出现锁互斥的现象,即不同的线程之间没有锁的争用。
3、如果我们将同步块中的锁均设置为类锁,即Main.class
[java] view plain copy
1. public class Main {
2. public static void main(String[] args) {
3. new Thread(new Runnable() {
4.
5. @Override
6. public void run() {
7. Main.method2();
8. }
9. }).start();
10. new Thread(new Runnable() {
11.
12. @Override
13. public void run() {
14. Main obj = new Main();
15. obj.method();
16.
17. }
18. }).start();
19. }
20. public static void method2() {
21. synchronized (Main.class) {
22.
23. System.out.println("method2......");
24. try {
25. Thread.sleep(2000);
26. } catch (InterruptedException e) {
27. e.printStackTrace();
28. }
29. System.out.println("method2....666..");
30. }
31. }
32.
33. public void method(){
34. synchronized (Main.class) {
35. System.out.println("method...");
36. }
37. }
38.
39. }此时,情况和1中一样,两个线程争用锁,一个没有释放锁,另一个线程的同步块将不会被执行。(因为此时两个线程使用了同一个类锁Main.class)
4、两个同步块中使用不同的类锁,比如一个为Main.class,另一个为Object.class。
此时,是两把不同的类锁,不会导致线程之间的锁争用,即自己执行自己的同步块即可。
5、同理,两个同步块中使用不同的对象锁,也不会导致锁争用,线程之间相互独立执行。
以上就是对synchronized中类锁和对象锁的理解,以及什么情况下会导致线程之间对锁发生争用。
以上就介绍了JAVA的相关知识,希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号