demo1
public class Test1 {
public static void main ( String[ ] args) throws InterruptedException {
He him = new He ( ) ;
new Thread ( ( ) - > {
him. listenMusic ( ) ;
} ) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He {
public synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public synchronized void listenMusic ( ) {
System. out. println ( "listenMusic" ) ;
}
}
demo2
package com. company. lock8;
import java. util. concurrent. TimeUnit;
public class Test2 {
public static void main ( String[ ] args) throws InterruptedException {
He2 him = new He2 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He2 {
public synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo3
public class Test3 {
public static void main ( String[ ] args) throws InterruptedException {
He3 him = new He3 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He3 {
public void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo4
public class Test4 {
public static void main ( String[ ] args) throws InterruptedException {
He4 him = new He4 ( ) ;
He4 him_2 = new He4 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him_2. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He4 {
public synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo5
public class Test5 {
public static void main ( String[ ] args) throws InterruptedException {
He5 him = new He5 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He5 {
public static synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public static synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo6
public class Test6 {
public static void main ( String[ ] args) throws InterruptedException {
He6 him = new He6 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He6 {
public synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public static synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo7
public class Test7 {
public static void main ( String[ ] args) throws InterruptedException {
He7 him = new He7 ( ) ;
He7 him_2 = new He7 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him_2. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He7 {
public static synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public static synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
demo8
public class Test8 {
public static void main ( String[ ] args) throws InterruptedException {
He8 him = new He8 ( ) ;
He8 him_2 = new He8 ( ) ;
new Thread ( ( ) - > {
try {
him. listenMusic ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
) . start ( ) ;
TimeUnit. SECONDS. sleep ( 1 ) ;
new Thread ( ( ) - > {
him_2. palyGame ( ) ;
} ) . start ( ) ;
}
}
class He8 {
public synchronized void palyGame ( ) {
System. out. println ( "palyGame" ) ;
}
public static synchronized void listenMusic ( ) throws InterruptedException {
TimeUnit. SECONDS. sleep ( 5 ) ;
System. out. println ( "listenMusic" ) ;
}
}
总结与注意
static synchronized锁的是类对象本身,是模板。而synchronizeds锁的是实例对象。 当一个线程的同步方法或同步代码块(不管是静态同步方法还是普通同步方法)执行时,它总先要获得锁,才能执行,如果公共资源,也就是这个锁还被其他同步方法或同步代码块霸占着,没有释放,那么这个同步方法必须等待这个锁被释放才能执行。