synchronized 翻译:(使) 同步,在时间上一致,同速进行
问题:为什么要做同步
主要是存在共享资源(临界资源)和在多线程共同操作同一个资源,导致的数据不一致的问题,所以要做线程同步。
解决问题的根本方法:
同一时刻有且只有一个线程操作共享数据,其他线程在等待该线程处理完毕后再操作共享资源。
synchronized 实现要引入互斥锁,互斥锁两大特性:互斥性,可见性。synchronized锁的不是代码,所的是对象。
根据获取锁的分类,可以分为对象锁和类锁
对象锁的两种方法:
1,同步代码块{synchronized(this),synchronized(类的实例对象),锁的是小括号里面的实例对象}
2,同步非静态方法{synchronized method},锁的是当前的实例;
类锁的两种方法:
扫描二维码关注公众号,回复:
12098898 查看本文章
1,同步代码块{synchronized(类.class),锁的是小括号()中的类对象(class对象)}
2,同步静态方法{synchronized static method},锁的是当前类的对象(clas对象)。
废话不多说代码走一波,(类锁也是一种特殊的对象锁,类锁和对象锁执行互不影响)
一共两个类,一个包含4种锁的多线程方法SyncThread,一个是调用类RunSyncDemo
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Administrator on 2019/11/16.
*/
public class SyncThread implements Runnable {
@Override
public void run() {
String threadName = Thread.currentThread().getName();
if(threadName.startsWith("A")){
sync();
}else if(threadName.startsWith("B")){
syncObject1();
}else if (threadName.startsWith("C")){
syncObject2();
}else if (threadName.startsWith("D")){
syncObject3();
}else if (threadName.startsWith("E")){
syncObject4();
}
}
/**
*
*/
private void sync(){
try {
System.out.println(Thread.currentThread().getName()+"_sync_Start:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"_sync_end:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*同步代码块{synchronized(this),synchronized(类的实例对象),锁的是小括号里面的实例对象}
*/
private void syncObject1(){
System.out.println(Thread.currentThread().getName()+"_syncObject1"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
try {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + "_syncObject1_Start:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "_syncObject1_end:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*同步非静态方法{synchronized method},锁的是当前的实例;
*/
private synchronized void syncObject2(){
System.out.println(Thread.currentThread().getName()+"_syncObject2:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
try {
System.out.println(Thread.currentThread().getName()+"_syncObject2_Start:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"_syncObject3_end:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*同步代码块{synchronized(类.class),锁的是小括号()中的类对象(class对象)}
*/
private void syncObject3(){
System.out.println(Thread.currentThread().getName()+"_syncObject3"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
try {
synchronized (SyncThread.class) {
System.out.println(Thread.currentThread().getName() + "_syncObject3_Start:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "_syncObject3_end:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*同步静态方法{synchronized static method},锁的是当前类的对象(clas对象)
*/
private static synchronized void syncObject4(){
System.out.println(Thread.currentThread().getName()+"_syncObject3:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
try {
System.out.println(Thread.currentThread().getName()+"_syncObject4_Start:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"_syncObject4_end:"+ new SimpleDateFormat("HH:mm:ss").format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class RunSyncDemo {
public static void main(String[] args) {
SyncThread syncThread =new SyncThread();
Thread thread1 = new Thread(syncThread,"A");
Thread thread2 = new Thread(syncThread,"A");
Thread thread3 = new Thread(syncThread,"B");
Thread thread4 = new Thread(syncThread,"B");
Thread thread5 = new Thread(syncThread,"C");
Thread thread6 = new Thread(syncThread,"C");
Thread thread7 = new Thread(syncThread,"D");
Thread thread8 = new Thread(syncThread,"D");
Thread thread9 = new Thread(syncThread,"E");
Thread thread10 = new Thread(syncThread,"E");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}
}
总结:
对象锁是看起来像锁的某段代码,而类锁的是整个类;个人理解如有疑问请留言讨论