sychronized是java多线程非常关键的一个知识点,这篇博客将从synchronized几个用法以及代码来学习。
sychronized的作用是能够保证同一时间只有一个线程来运行这块代码,达到并发效果,如果没有保证并发的话,在多线程编码中就会产生致命问题,比如经典的i++,这也是数据库并发中经典的案例,i++并不是原子操作,分为三步,取数,操作,写数,参考这段代码,可以运行一下看下结果
public class showUnsafe1 implements Runnable{
static int i=0;
@Override
public void run() {
for(int j=0;j<10000;j++){
i++;
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new showUnsafe1());
Thread thread2 = new Thread(new showUnsafe1());
thread1.start(); // 启动thread1,在合适的时刻运行
thread2.start(); // 启动thread2,在合适的时刻运行
thread1.join(); // 让主线程等待thread1运行完
thread2.join(); // 让主线程等待thread2运行完
System.out.println(i);
}
}
一、synchronized四种用法
synchronized为啥这么神奇,无它,加锁而已,不少八股文喜欢分为两种锁,一种是对象锁,一种是类锁,还可以分为方法锁,代码块锁,静态锁,class锁,我们通过代码学习他们如何使用
1.对象锁:方法锁
方法锁是用synchronized修饰的一个类方法,作用方法即是方法作用域,除了这个方法要同步,其余不需要
public class SynchronizedObjectMethod implements Runnable{
private static SynchronizedObjectMethod instance=new SynchronizedObjectMethod();
public synchronized void method(){
System.out.println("我是对象锁的方法修饰符形式。我叫"+Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行结束");
}
@Override
public void run() {
method();
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while(thread1.isAlive()||thread2.isAlive()){
}
System.out.println("finish");
}
}
2. 对象锁:代码块形式
代码块锁就是常用的同步方法块,synchronized锁住的是它里面的对象,作用域就是synchonized{}里面的代码
public class SynchronizedObjectCodeBlock implements Runnable{
private static SynchronizedObjectCodeBlock instance=new SynchronizedObjectCodeBlock();
Object lock1=new Object();
@Override
public void run() {
synchronized (lock1){
System.out.println("我是对象锁的代码块形式。我叫"+Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行结束");
}
}
public static void main(String[] args){
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while(thread1.isAlive()||thread2.isAlive()){
}
System.out.println("finish");
}
}
3. 类锁:class形式
class形式说的是synchronized()括号里使用的锁是class对象,所谓class对象指得是java文件对应的一个java.lang.class对象,所有该类生成的对象共有这个class对象 类加载机制,所以这个锁锁住了这个类生成的所有对象
public class SynchronizedClassClass implements Runnable{
private static SynchronizedClassClass instance1=new SynchronizedClassClass();
private static SynchronizedClassClass instance2=new SynchronizedClassClass();
public void method(){
synchronized (SynchronizedClassClass.class){
System.out.println("我是类锁的形式之一:修饰.class。我叫"+Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行结束");
}
}
@Override
public void run() {
method();
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance1);