synchronized
Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍然可以访问该object中的非加锁代码块。
——以上来源百度百科
一、方法内的变量为线程安全
“非线程安全”的问题存在于“实例变量”中,如果是方法内部的私有变量,则不会存在“非线程安全”问题,所得结果就是“线程安全”的了。
MyService类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
public void add(String name) {
try {
int num = 0;
if (name.equals("a")) {
num = 100;
System.out.println("a is over");
Thread.sleep(1000);
} else {
num = 200;
System.out.println("b is over");
}
System.out.println(name + " num = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类A
package com.mythread.www.day8.testSyn.ep1; public class ThreadA extends Thread {
private MyService service; public ThreadA(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.add("a");
}
}
线程类B
package com.mythread.www.day8.testSyn.ep1; public class ThreadB extends Thread {
private MyService service; public ThreadB(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.add("b");
}
}
运行类
package com.mythread.www.day8.testSyn.ep1; public class Run {
public static void main(String[] args) {
MyService service = new MyService();
ThreadA threadA = new ThreadA(service);
threadA.start();
ThreadB threadB = new ThreadB(service);
threadB.start();
}
}
结果
a is over
b is over
b num = 200
a num = 100
从运行结果来看,方法中的变量不存在非线程安全的问题,永远都是线程安全的,这事方法内部的变量是私有的特性造成的。
二、实例变量非线程安全
MyService类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
private int num = 0;
public void add(String name) {
try { if (name.equals("a")) {
num = 100;
System.out.println("a is over");
Thread.sleep(1000);
} else {
num = 200;
System.out.println("b is over");
}
System.out.println(name + " num = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类和运行类同上,运行结果
a is over
b is over
b num = 200
a num = 200
产生这个结果的原因是两个线程同事访问同一个没有同步的方法,如果两个对象同时操作对象中的实例变量,可能会造成非线程安全的问题
最简单的解决方案是在方法的前面加个synchronized同步锁
MyService类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
private int num = 0;
synchronized public void add(String name) {
try { if (name.equals("a")) {
num = 100;
System.out.println("a is over");
Thread.sleep(1000);
} else {
num = 200;
System.out.println("b is over");
}
System.out.println(name + " num = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类和运行类同上,运行结果
a is over
a num = 100
b is over
b num = 200
在两个线程访问同一个对象中的同步方法时一定是线程安全的,上面的代码由于时同步访问,所以先打印出a,然后在打印出b
改一下运行类,其他类同上
package com.mythread.www.day8.testSyn.ep1; public class Run {
public static void main(String[] args) {
MyService serviceA = new MyService();
MyService serviceB = new MyService();
ThreadA threadA = new ThreadA(serviceA);
threadA.start();
ThreadB threadB = new ThreadB(serviceB);
threadB.start();
}
}
结果
a is over
b is over
b num = 200
a num = 100
这是两个线程分别访问同一个类的两个不同实例的相同名称的同步方法,效果却是以异步的形式来执行的。
因为创建了两个业务对象,在系统中产生了两个锁,所以运行结果是异步的。
关键字synchronized所取得的锁都是对象锁,而不是把一段代码或者方法当作锁。
My Service类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
synchronized public void methodA() {
try {
System.out.println("begin methodA threadName = " + Thread.currentThread().getName());
Thread.sleep(3000);
System.out.println("end methodA time = " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
} public void methodB() {
try {
System.out.println("begin methodB threadName = " + Thread.currentThread().getName());
Thread.sleep(3000);
System.out.println("end methodB time = " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
线程类A
package com.mythread.www.day8.testSyn.ep1; public class ThreadA extends Thread {
private MyService service; public ThreadA(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.methodA();
}
}
线程类B
package com.mythread.www.day8.testSyn.ep1; public class ThreadB extends Thread {
private MyService service; public ThreadB(MyService service) {
super();
this.service = service;
} @Override
public void run() {
service.methodB();
}
}
运行类
package com.mythread.www.day8.testSyn.ep1; public class Run {
public static void main(String[] args) {
MyService service = new MyService();
ThreadA threadA = new ThreadA(service);
threadA.start();
ThreadB threadB = new ThreadB(service);
threadB.start();
}
}
结果
begin methodA threadName = Thread-1
begin methodB threadName = Thread-2
end methodB time = 1458400534384
end methodA time = 1458400534384
在My Service的methodB前面也加上关键字synchronized
My Service类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
synchronized public void methodA() {
try {
System.out.println("begin methodA threadName = " + Thread.currentThread().getName());
Thread.sleep(3000);
System.out.println("end methodA time = " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
} synchronized public void methodB() {
try {
System.out.println("begin methodB threadName = " + Thread.currentThread().getName());
Thread.sleep(3000);
System.out.println("end methodB time = " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
结果
begin methodA threadName = Thread-1
end methodA time = 1458400619034
begin methodB threadName = Thread-2
end methodB time = 1458400622035
对比上面两次代码的运行结果。
在第一次运行时,当A线程先持有My Service的同步锁时,B线程可以已异步的方式去调用My Service对象中的非synchronized方法。
在第二次运行时,当A线程先持有My Service的同步锁时,当B想调用My Service对象中的synchronized,则需要先等A释放对象锁。
所以,synchronized锁住的时对象,而不是其他的一些东西。
三、synchronized锁重入
当一个线程获得一个对象锁后,再次请求此对象锁时是可以再次获得此对象锁的
My Service类
package com.mythread.www.day8.testSyn.ep1; public class MyService {
synchronized public void methodA() {
System.out.println("methodA");
methodB();
} synchronized public void methodB() {
System.out.println("methodB");
methodC();
} synchronized public void methodC() {
System.out.println("methodC");
}
}
线程类
package com.mythread.www.day8.testSyn.ep1; public class MyThread extends Thread {
@Override
public void run() {
MyService myService = new MyService();
myService.methodA();
}
}
运行类
package com.mythread.www.day8.testSyn.ep1; public class Run {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
结果
methodA
methodB
methodC
自己还可以重新获得自己的内部锁,如果不可以的话,上面的这个Demo则会造成死锁现象
Main类
package com.weishiyao.learn.day4.testThread; public class Main {
public int i = 10;
synchronized public void operateIMainMethod() {
try {
i--;
System.out.println("main print i=" + i);
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sub类
package com.weishiyao.learn.day4.testThread; public class Sub extends Main{
synchronized public void operateISubMethod() {
try {
while (i > 0) {
i--;
System.out.println("sub print i=" + i);
Thread.sleep(100);
this.operateIMainMethod();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
线程类
package com.weishiyao.learn.day4.testThread; public class MyThread extends Thread{
@Override
public void run() {
Sub sub = new Sub();
sub.operateISubMethod();
}
}
运行类
package com.weishiyao.learn.day4.testThread; public class Run {
public static void main(String[] args) {
MyThread t = new MyThread();
t.run();
}
}
结果
sub print i=9
main print i=8
sub print i=7
main print i=6
sub print i=5
main print i=4
sub print i=3
main print i=2
sub print i=1
main print i=0
当存在父子类继承关系时,子类完全可以通过父类“可重入锁”调用父类的同步方法