1.作用在普通方法上
在这种情况下是对象锁,下面可以看到同一时刻只有一个线程能进入demo对象的increase()方法。要进入synchronized修饰的普通方法,就要获取当前对象(demo)的锁。这种方式仅适用于单例模式。
Demo类:
package sychronize_usage;
public class Demo {
volatile int count;
public synchronized void increase()
{
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类及运行结果:
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo=new Demo();
Thread t1 =new Thread(()->{
demo.increase();
},"t1");
Thread t2 =new Thread(()->{
demo.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:5
t2 count:6
t2 count:7
t2 count:8
t2 count:9
*/
即使是不同的两个方法,被synchronize修饰同时也只能有一个线程进入其中一个,因为一个对象只有一把锁。
在Demo类中添加decrease()方法
package sychronize_usage;
public class Demo {
volatile int count;
public synchronized void decrease() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + --count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void increase()
{
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类中t1执行increase()方法t2执行decrease()方法,运行结果如下:
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo=new Demo();
Thread t1 =new Thread(()->{
demo.increase();
},"t1");
Thread t2 =new Thread(()->{
demo.decrease();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:4
t2 count:3
t2 count:2
t2 count:1
t2 count:0
*/
如果这样写,同步就会失效,t1线程进入的是demo1对象的increase()方法,而t2线程进入的是demo2对象的increase()方法。这也说明,synchronize作用在普通方法上是对象锁:
Demo类不变,Test类中及运行结果
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo2.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t2 count:0
t1 count:1
t2 count:1
t2 count:2
t1 count:2
t2 count:3
t1 count:3
t1 count:4
t2 count:4
*/
2.作用在静态方法上
作用在静态方法上是类锁,修改Demo类的increase()方法为静态方法,要进入Demo类的increase()方法就要获取Demo类的锁,尽管new了两个对象,demo1和demo2,但是线程依然同步。
Demo类
package sychronize_usage;
public class Demo {
volatile static int count;
public synchronized static void increase() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类及运行结果:
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo2.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:5
t2 count:6
t2 count:7
t2 count:8
t2 count:9
*/
即使是两个不同的静态方法,被synchronize修饰同一时刻也只有一个线程进入其中一个方法,因为一个类只有一把锁:
Demo类:
package sychronize_usage;
public class Demo {
volatile static int count;
public synchronized static void increase() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized static void decrease() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + --count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类及运行结果:
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo2.decrease();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:4
t2 count:3
t2 count:2
t2 count:1
t2 count:0
*/
3.作用在代码块上
作用在代码块上,要指定作用范围synchronize(xxx){},xxx可以是this(当前对象),也可以是其他对象,要执行代码块中的代码必须获得传入对象的锁。
3.1 synchronize(this),和修饰普通方法一样是对象锁,如果要同步,就不能new多个实例。
Demo类
package sychronize_usage;
public class Demo {
volatile static int count;
public void increase() {
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Test类及运行结果
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo1.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:5
t2 count:6
t2 count:7
t2 count:8
t2 count:9
*/
也可以传入其他对象,要进入此代码块,必须获得传入对象的锁。
Demo类
package sychronize_usage;
public class Demo {
volatile static int count;
Object object;
public Demo(Object obj){
object=obj;
}
public void increase() {
synchronized (object) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Test类及运行结果
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Object o=new Object();
Demo demo1=new Demo(o);
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo1.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:5
t2 count:6
t2 count:7
t2 count:8
t2 count:9
*/
3.2 synchronize(Demo.class),要进入代码块,必须获取该类的锁(每个类只有一个class对象)
new 了两个对象,但是可以看出同时只有一个线程能进入同步代码块。
Demo类
package sychronize_usage;
public class Demo {
volatile static int count;
public void increase() {
synchronized (Demo.class) {//this.getClass()
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Test类及运行结果
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo2.increase();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:5
t2 count:6
t2 count:7
t2 count:8
t2 count:9
*/
总结
对象锁:修饰普通方法、修饰代码块传入的对象为this和其他非class对象。
类锁:修饰静态方法、修饰代码块传入的对象为xxx.class。
补充
1.若锁住的是同一个对象,一个线程在访问对象的同步方法时,另一个线程访问对象的同步代码块也会被阻塞,始终记住一点一个对象只有一把锁。
2.synchronize修饰静态方法和synchronize(xxx.class)其实是一回事,要获取的锁是同一个锁,都是class对象的锁。下面验证
Demo类中increase()中的代码块用synchronize(Demo.class)修饰,decrease()方法改成静态方法并用synchronize修饰
package sychronize_usage;
public class Demo {
volatile static int count;
public void increase() {
synchronized (Demo.class) {//this.getClass()
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + count++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized static void decrease() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " count:" + --count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Test类及运行结果:
package sychronize_usage;
public class Test{
public static void main(String[] args) {
Demo demo1=new Demo();
Demo demo2=new Demo();
Thread t1 =new Thread(()->{
demo1.increase();
},"t1");
Thread t2 =new Thread(()->{
demo2.decrease();
},"t2");
t1.start();
t2.start();
}
}
/*
t1 count:0
t1 count:1
t1 count:2
t1 count:3
t1 count:4
t2 count:4
t2 count:3
t2 count:2
t2 count:1
t2 count:0
*/
3.类锁和对象锁互不干扰。因为他们是不同对象的锁,类锁是class对象锁,对象锁是普通对象的锁。