synchronized是java多线程很常用的关键字,可用它对线程jia加锁,在一个线程执行的时候,阻塞其他线程的访问,可用于类,变量和方法上,synchronized作用的是对象,如果使用不当的话,synchronized锁并不会生效,举个例子
package com.qingwei.springboot.juc;
/**
* Created by kongwc on 2020/3/18.
*/
public class SynchronizedDemo {
public static void main(String[] args) {
Demo demo = new Demo(1);
for(int i=0; i<5;i++){
Thread thread = new Thread(demo);
thread.start();
}
}
static class Demo implements Runnable{
Integer i = 1;
Demo(Integer i){
this.i = i;
}
@Override
public void run() {
synchronized (i){
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
}
如上的代码,理想的结果应该是输出2,3,4,5,6结果输出
此时可以将代码sh稍作修改
package com.qingwei.springboot.juc;
/**
* Created by kongwc on 2020/3/18.
*/
public class SynchronizedDemo {
public static void main(String[] args) {
Demo demo = new Demo(1);
for(int i=0; i<5;i++){
Thread thread = new Thread(demo);
thread.start();
}
}
static class Demo implements Runnable{
private Object object = new Object();
Integer i = 1;
Demo(Integer i){
this.i = i;
}
@Override
public void run() {
synchronized (object){
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
}
至于第一次为什么会有问题,是因为i++的时候,实际是执行Integer.valueof(i) + 1
而这个方法返回的是yi一个new Integer(),说明重新生成了一个对象,而synchronized锁住的仍然是原来的对象,所以如果shiy使用synchronized加锁,必须保证锁住的对象是不变的