synchronized加锁使用对象简介说明
下文笔者讲述synchronized加锁对象简介说明,如下所示
synchronized加锁对象说明
加锁对象1:
修饰普通方法
加锁同步代码块
synchronized(this)锁定同步代码块
加锁变量
加锁类
加锁静态方法
例:synchronized加锁示例
修饰普通方法(锁住当前实例对象)
同一个实例调用会阻塞
不同实例调用不会阻塞
public class SynchronizedTest {
//锁住了本类的实例对象
public synchronized void test1() {
try {
logger.info(Thread.currentThread().getName() + " test1 进入了同步方法");
Thread.sleep(5000);
logger.info(Thread.currentThread().getName() + " test1 休眠结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest st = new SynchronizedTest();
SynchronizedTest st2 = new SynchronizedTest();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test1();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st2.test1();
}).start();
}
}
同步代码块传参this(锁住的是当前实例对象)
同一个实例调用会阻塞
不同实例调用不会阻塞
public class SynchronizedTest {
//锁住了本类的实例对象
public void test2() {
synchronized (this) {
try {
logger.info(Thread.currentThread().getName() + " test2 进入了同步块");
Thread.sleep(5000);
logger.info(Thread.currentThread().getName() + " test2 休眠结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedTest st = new SynchronizedTest();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test2();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test2();
}).start();
}
}
同步代码块传参变量对象(锁住变量对象)
同一个属性对象才会实现同步
public class SynchronizedTest {
public Integer lockObject;
public SynchronizedTest(Integer lockObject) {
this.lockObject = lockObject;
}
//锁住了实例中的成员变量
public void test3() {
synchronized (lockObject) {
try {
logger.info(Thread.currentThread().getName() + " test3 进入了同步块");
Thread.sleep(5000);
logger.info(Thread.currentThread().getName() + " test3 休眠结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedTest st = new SynchronizedTest(127);
SynchronizedTest st2 = new SynchronizedTest(127);
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test3();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st2.test3();
}).start();
}
}
同步代码块传参class对象(全局锁)
所有调用该方法的线程都会实现同步
public class SynchronizedTest {
//全局锁,类是全局唯一的
public void test4() {
synchronized (SynchronizedTest.class) {
try {
logger.info(Thread.currentThread().getName() + " test4 进入了同步块");
Thread.sleep(5000);
logger.info(Thread.currentThread().getName() + " test4 休眠结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedTest st = new SynchronizedTest();
SynchronizedTest st2 = new SynchronizedTest();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test4();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st2.test4();
}).start();
}
}
修饰静态方法(全局锁)
所有调用该方法的线程都会实现同步
public class SynchronizedTest {
//全局锁,静态方法全局唯一的
public synchronized static void test5() {
try {
logger.info(Thread.currentThread().getName() + " test5 进入同步方法");
Thread.sleep(5000);
logger.info(Thread.currentThread().getName() + " test5 休眠结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedTest st = new SynchronizedTest();
SynchronizedTest st2 = new SynchronizedTest();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st.test5();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
st2.test5();
}).start();
new Thread(() -> {
logger.info(Thread.currentThread().getName() + " test 准备进入");
SynchronizedTest.test5();
}).start();
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


