1.synchronized+类成员方法:(对象锁)不同对象互相不会排斥。
a.同一对象调用不同synchronized方法:相同对象synchronized方法会互相排斥。
public class Test {
public synchronized void sy() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void sy2() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final Test t1 = new Test();
final Test t2 = new Test();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
new Thread(new Runnable(){
public void run() {
t1.sy2();//t1对象
}}).start();
}
}
结果如下:
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
b.同一对象调用同一synchronized方法:相同对象会排斥。
public class Test {
public synchronized void sy() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final Test t1 = new Test();
final Test t2 = new Test();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
}
}
结果如下:
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
c.不同对象调用不同synchronized方法:不同对象不会排斥。
public class Test {
public synchronized void sy() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void sy2() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final Test t1 = new Test();
final Test t2 = new Test();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
new Thread(new Runnable(){
public void run() {
t2.sy2();//t2对象
}}).start();
}
}
结果如下:
Thread-0:0
Thread-1:0
Thread-0:1
Thread-1:1
Thread-0:2
Thread-1:2
Thread-0:3
Thread-1:3
Thread-0:4
Thread-1:4
d.不同对象调用同一synchronized方法:不同对象不会排斥。
public class Test {
public synchronized void sy() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void sy2() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final Test t1 = new Test();
final Test t2 = new Test();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
new Thread(new Runnable(){
public void run() {
t2.sy());//t2对象
}}).start();
}
}
结果如下:
Thread-0:0
Thread-1:0
Thread-1:1
Thread-0:1
Thread-0:2
Thread-1:2
Thread-0:3
Thread-1:3
Thread-1:4
Thread-0:4
2.synchronized+类静态成员方法:(类锁)所有对象会排斥。
a.(同一/不同)对象调用(同一/不同)synchronized方法:都会排斥。
public class Test {
public static synchronized void sy() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void sy2() {
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final Test t1 = new Test();
final Test t2 = new Test();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象
}}).start();
new Thread(new Runnable(){
public void run() {
t1.sy();//t1对象 a
//t1.sy2();//t1对象 b
//t2.sy();//t2对象 c
//t2.sy2();//t2对象 d
}}).start();
}
}
a,b, c, d四种情况:
Thread-0:0
Thread-0:1
Thread-0:2
Thread-0:3
Thread-0:4
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-1:4
总结:
1.多个线程调用同一对象的不同synchronized方法,同一时刻只能有一个线程得到执行,另一个线程必须等待。
2.多个线程调用不同对象的相同synchronized方法,互不影响。
3.多个线程调用(同一/不同)对象的(同一/不同)static+synchronized方法,同一时刻只能有一个线程得到执行,另一个线程必须等待。
关键:
静态方法的锁为Class类对象,非静态方法的锁为实例对象。某一时刻,只能有一个线程持有该对象(或者Class对象或者实例对象)的锁