1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class B { synchronized public static void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } synchronized public void mC(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class A { public static void test() { synchronized(A.class) { System.out.println("smlz"); } } public void test2() { synchronized(this) { System.out.println("smlz"); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class B { synchronized public static void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.print(value); } }
public void mC(String value) { synchronized(B.class) { for(int i = 0; i < 1000; ++i){ System.out.print(value); } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class C { synchronized public void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.print(value); } }
public void mC(String value) { synchronized(this) { for(int i = 0; i < 1000; ++i) { System.out.print(value); } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class B { synchronized public void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } public void mC(String value) throws InterruptedException { synchronized(this) { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } } }
public class ApplicationDemo { public static void main(String[] args) { B b = new B(); Thread thread_1 = new Thread(new Runnable() { @Override public void run() { try { b.mB("1"); } catch(InterruptedException e) { e.printStackTrace(); } } }); Thread thread_2 = new Thread(new Runnable() { @Override public void run() { try { b.mC("2"); } catch(InterrutedException e) { e.printStackTrace(); } } }); thread_1.start(); thread_2.start(); } }
|
2、使用类锁进行测试,同样1和2是分开的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public class B { synchronized public static void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } public static void mC(String value) throws InterruptedException { synchronized(B.class) { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } } }
public class ApplicationDemo { public static void main(String[] args) { Thread thread_1 = new Thread(new Runnable() { @Override public void run() { try { B.mB("1"); } catch(InterruptedException e) { e.printStackTrace(); } } }); Thread thread_2 = new Thread(new Runnable() { @Override public void run() { try { B.mC("2"); } catch(InterruptedException e) { e.printStackTrace(); } } }); thread_1.start(); thread_2.start(); } }
|
3、但如果类锁和对象锁同时存在,则无法保证同步,即1和2将交替打出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class B { synchronized public static void mB(String value) throws InterruptedException { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } public void mC(String value) throws InterruptedException { synchronized(this) { for(int i = 0; i < 1000; ++i) { System.out.println(value); } } } }
public class ApplicationDemo { public static void main(String[] args) { Thread thread_1 = new Thread(new Runnable() { @Override public void run() { try { B.mB("1"); } catch(InterruptedException e) { e.printStackTrace(); } } }); B b = new B(); Thread thread_2 = new Thread(new Runnable() { @Override public void run() { try { b.mC("2"); } catch(InterruptedException e) { e.printStackTrace(); } } }); thread_1.start(); thread_2.start(); } }
|
(仅个人整理,非原创)
(感谢大佬原创,原文链接)