2.4. synchronized同步代码块
2.4.1. 同步代码块的使用方法
1)使用方法说明:
在多个线程共同访问的代码放在synchronized(){ 代码块 } 中的代码块位置处。
2)样例程序:
public class UsingMethod {
public static void main(String[] args) {
Service service = new Service();
UsingMethodThread1 thread1 = new UsingMethodThread1(service);
thread1.setName("A");
UsingMethodThread2 thread2 = new UsingMethodThread2(service);
thread2.setName("B");
thread1.start();
thread2.start();
}
}
class Service{
public void foo(){
try {
synchronized (this){
System.out.println("线程" + Thread.currentThread().getName() + "开始");
Thread.sleep(2000);
System.out.println("线程" + Thread.currentThread().getName() + "结束");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class UsingMethodThread1 extends Thread{
private Service service;
public UsingMethodThread1(Service service){
this.service = service;
}
@Override
public void run(){
this.service.foo();
}
}
class UsingMethodThread2 extends Thread{
private Service service;
public UsingMethodThread2(Service service){
this.service = service;
}
@Override
public void run(){
this.service.foo();
}
}
2.4.2. 锁对象
2.4.2.1. this作对象锁
1)功能描述:
在多线程中使用同步synchronized(this)同步代码块时,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对这个object的其他synchronized(this)同步的访问会被阻塞,说明synchronized使用的对象锁是同一个。
2)样例程序
public class Synchronism {
public static void main(String[] args) throws InterruptedException {
SynchronismService service = new SynchronismService();
SynchronismThreadA threadA = new SynchronismThreadA(service);
threadA.setName("A");
threadA.start();
Thread.sleep(1000);
SynchronismThreadB threadB = new SynchronismThreadB(service);
threadB.setName("B");
threadB.start();
}
}
class SynchronismService{
public void foo1(){
try{
synchronized (this){
System.out.println("线程" + Thread.currentThread().getName() + "中foo1启动");
Thread.sleep(1000);
System.out.println("线程" + Thread.currentThread().getName() + "中foo1结束");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void foo2(){
synchronized (this){
System.out.println("线程" + Thread.currentThread().getName() + "中foo2启动");
System.out.println("线程" + Thread.currentThread().getName() + "中foo2结束");
}
}
}
class SynchronismThreadA extends Thread{
private SynchronismService service;
public SynchronismThreadA(SynchronismService service){
this.service = service;
}
@Override
public void run(){
this.service.foo1();
}
}
class SynchronismThreadB extends Thread{
private SynchronismService service;
public SynchronismThreadB(SynchronismService service){
this.service = service;
}
@Override
public void run(){
this.service.foo2();
}
}
2.4.2.2. 当前对象作对象锁
1)基本描述
Synchronized同步代码块和synchronized同步方法一样,锁定的都是当前对象。
2)样例程序
public class LockObject {
public static void main(String[] args) throws InterruptedException {
LockObjectService service = new LockObjectService();
LockObjectThreadA threadA = new LockObjectThreadA(service);
threadA.setName("A");
threadA.start();
Thread.sleep(1000);
LockObjectThreadB threadB = new LockObjectThreadB(service);
threadB.setName("B");
threadB.start();
}
}
class LockObjectService{
public void foo1(){
try{
synchronized (this){
System.out.println("线程" + Thread.currentThread().getName() + "中foo1启动");
Thread.sleep(1000);
System.out.println("线程" + Thread.currentThread().getName() + "中foo1结束");
}
}catch (InterruptedException e){