标签:
这一章节我们来讨论游戏,synchronized持有对象锁与类锁的不同点-两种锁是并行的东西,没有交集。
1.同步持有对象锁或者类锁
package com.ray.deepintothread.ch02.topic_3; public class SynchInstance5 { public static void main(String[] args) throws InterruptedException { MyTestObjectFive myTestObjectFive = new MyTestObjectFive(); for (int i = 0; i < 2; i++) { ThreadFive threadFive = new ThreadFive(myTestObjectFive); Thread thread = new Thread(threadFive); thread.setName("" + i); thread.start(); } } } class ThreadFive implements Runnable { private MyTestObjectFive myTestObjectFive; public ThreadFive(MyTestObjectFive myTestObjectFive) { this.myTestObjectFive = myTestObjectFive; } @Override public void run() { try { if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) { myTestObjectFive.test1(); } else { myTestObjectFive.test2(); } } catch (InterruptedException e) { e.printStackTrace(); } } } class MyTestObjectFive { public synchronized void test1() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } public synchronized void test2() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } }
0
1
2
3
4
0
1
2
3
4
package com.ray.deepintothread.ch02.topic_3; public class SynchInstance6 { public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 2; i++) { ThreadSix threadSix = new ThreadSix(); Thread thread = new Thread(threadSix); thread.setName("" + i); thread.start(); } } } class ThreadSix implements Runnable { @Override public void run() { try { if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) { MyTestObjectSix.test1(); } else { MyTestObjectSix.test2(); } } catch (InterruptedException e) { e.printStackTrace(); } } } class MyTestObjectSix { public static synchronized void test1() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } public static synchronized void test2() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } }
0
1
2
3
4
0
1
2
3
4
像前面章节一样,当我们同步持有对象锁或者类锁,输出的是按照顺序的
2.当同时持有对象锁和类锁
package com.ray.deepintothread.ch02.topic_3; public class SynchInstance4 { public static void main(String[] args) throws InterruptedException { MyTestObjectFour myTestObjectThree = new MyTestObjectFour(); for (int i = 0; i < 2; i++) { ThreadFour threadFour = new ThreadFour(myTestObjectThree); Thread thread = new Thread(threadFour); thread.setName("" + i); thread.start(); } } } class ThreadFour implements Runnable { private MyTestObjectFour myTestObjectThree; public ThreadFour(MyTestObjectFour myTestObjectThree) { this.myTestObjectThree = myTestObjectThree; } @Override public void run() { try { if (Integer.parseInt(Thread.currentThread().getName()) % 2 == 0) { myTestObjectThree.test1(); } else { MyTestObjectFour.test2(); } } catch (InterruptedException e) { e.printStackTrace(); } } } class MyTestObjectFour { public synchronized void test1() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } public static synchronized void test2() throws InterruptedException { for (int i = 0; i < 5; i++) { System.out.println(i); Thread.sleep(100); } } }
0
0
1
1
2
2
3
3
4
4
从输出可以看见,两者就像平行的关系,没有交集,交替的出现。但也是有秩序的。
总结:这一章节讨论了synchronized持有对象锁与类锁的不同点
这一章节就到这里,谢谢
------------------------------------------------------------------------------------
我的github:https://github.com/raylee2015/DeepIntoThread
目录:http://blog.csdn.net/raylee2007/article/details/51204573
从头认识多线程-2.3 synchronized持有对象锁与类锁的不同点
标签:
原文地址:http://blog.csdn.net/raylee2007/article/details/51236527