文章目录
线程的安全性问题<-并发编程带来的问题
问题演示
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
/**
* @Description: 并发编程问题demo
* @Author: gaoxinfu
* @Date: 2020-06-13 16:02
*/
public class SynchronizedQuestionDemo {
public static Integer COUNT=0;
public static void add(){
try {
Thread.currentThread().sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
COUNT++;
}
public static void main(String[] args) throws InterruptedException {
for (int i=1;i<=100;i++){
Thread thread=new Thread(new SynchronizedQuestionThread(),"Thread"+i);
thread.start();
}
Thread.currentThread().sleep(3000);
System.out.println(COUNT);
}
}
class SynchronizedQuestionThread implements Runnable{
@Override
public void run() {
SynchronizedQuestionDemo.add();
}
}
控台输出
91
Process finished with exit code 0
运行了100次,但是结果并不是100,而是显示了91,说明我们线程在运行的过程中,对于共享资源COUNT变量并不是线程安全的
1.由于我们启动了100个线程,在执行的线程的过程中,
在每一个线程的进行add方法中的COUNT++的过程中,实际上COUNT++在JVM内存中的运算是分好几步的
所以导致有的线程没有拿到COUNT值,但是还没有进行计算,其他线程使用它拿到的COUNT值做了计算
在各个线程之间处理的过程中,最终导致了COUNT值并不是预期的100
这个就是并发线程开发的过程中的共享变量
https://blog.csdn.net/u014636209/article/details/104400529
锁的本质
解决线程安全性问题的方式
synchronized
synchronized修饰的锁的类型
对象实例锁
同一个对象实例对象
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
/**
* @Description: Synchronized对对象实例进行加锁
* @Author: gaoxinfu
* @Date: 2020-06-13 17:27
*/
public class SynchronizedInstanceDemo {
void add() {
synchronized (this) {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedInstanceDemo synchronizedInstanceDemo=new SynchronizedInstanceDemo();
Thread thread1=new Thread(()->{
synchronizedInstanceDemo.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
synchronizedInstanceDemo.add();
},"thread-2");
thread2.start();
}
}
控台输出
大家好,我是线程 = thread-1,执行需要耗时5s,请稍等
大家好,我是线程 = thread-2,执行需要耗时5s,请稍等
Process finished with exit code 0
1.由于thread-1和thread-2两个线程,执行的对象实例都是synchronizedInstanceDemo,
在执行add()方法的时候由于 synchronized (this) 中对象实例synchronizedInstanceDemo,被锁住
因此thread-2是一定会等thread-1线程执行完毕之后,才会去执行的;
不同一个对象实例对象
public static void main(String[] args) {
SynchronizedInstanceDemo synchronizedInstanceDemo=new SynchronizedInstanceDemo();
//新增对象实例synchronizedInstanceDemo1
SynchronizedInstanceDemo synchronizedInstanceDemo1=new SynchronizedInstanceDemo();
Thread thread1=new Thread(()->{
synchronizedInstanceDemo.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
//然后线程2在执行的过程中对象是synchronizedInstanceDemo1 与线程1的对象不一致
synchronizedInstanceDemo1.add();
},"thread-2");
thread2.start();
}
控台输出
大家好,我是线程 = thread-1,执行需要耗时5s,请稍等
大家好,我是线程 = thread-2,执行需要耗时5s,请稍等
Process finished with exit code 0
1.两个线程的执行,先后等待的问题,因为相互锁定的对象是不同的
静态方法锁
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-06-13 18:22
*/
public class SynchronizedStaticMethodDemo {
synchronized static void add() {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedStaticMethodDemo synchronizedStaticMethodDemo1=new SynchronizedStaticMethodDemo();
SynchronizedStaticMethodDemo synchronizedStaticMethodDemo2=new SynchronizedStaticMethodDemo();
Thread thread1=new Thread(()->{
synchronizedStaticMethodDemo1.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
synchronizedStaticMethodDemo2.add();
},"thread-2");
thread2.start();
}
}
控台输出
大家好,我是线程 = thread-1,执行需要耗时5s,请稍等
大家好,我是线程 = thread-2,执行需要耗时5s,请稍等
Process finished with exit code 0
备注说明
1.对于JVM的编译,静态方法是存储在方法区(只有一份)
所以对于SynchronizedStaticMethodDemo对象实例其本质方法add的访问都是一份
由于synchronized修饰,线程只能顺序访问
2.关于类加载信息在jvm中的
synchronizedStaticMethodDemo1.add();跟SynchronizedStaticMethodDemo.add()调用是一样的
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-06-13 18:22
*/
public class SynchronizedStaticMethodDemo {
synchronized static void add() {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread thread1=new Thread(()->{
SynchronizedStaticMethodDemo.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
SynchronizedStaticMethodDemo.add();
},"thread-2");
thread2.start();
}
}
非静态方法锁
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
import java.util.concurrent.TimeUnit;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-06-14 10:13
*/
public class SynchronizedNonStaticMethodDemo {
synchronized void add() {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SynchronizedNonStaticMethodDemo SynchronizedNonStaticMethodDemo1=new SynchronizedNonStaticMethodDemo();
SynchronizedNonStaticMethodDemo SynchronizedNonStaticMethodDemo2=new SynchronizedNonStaticMethodDemo();
Thread thread1=new Thread(()->{
SynchronizedNonStaticMethodDemo1.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
SynchronizedNonStaticMethodDemo2.add();
},"thread-2");
thread2.start();
}
}
控台输出
大家好,我是线程 = thread-1,执行需要耗时5s,请稍等
大家好,我是线程 = thread-2,执行需要耗时5s,请稍等
Process finished with exit code 0
由于是非静态方法,只是对当前对象加锁,因此上面两个线程是互不影响的
对象锁
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.synchronize;
import java.util.concurrent.TimeUnit;
/**
* @Description:对象锁
* @Author: gaoxinfu
* @Date: 2020-06-13 18:53
*/
public class SynchronizedObjectDemo {
void add() {
synchronized (SynchronizedObjectDemo.class) {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SynchronizedObjectDemo SynchronizedObjectDemo1=new SynchronizedObjectDemo();
SynchronizedObjectDemo SynchronizedObjectDemo2=new SynchronizedObjectDemo();
Thread thread1=new Thread(()->{
SynchronizedObjectDemo1.add();
},"thread-1");
thread1.start();
Thread thread2=new Thread(()->{
SynchronizedObjectDemo2.add();
},"thread-2");
thread2.start();
}
}
执行也是先将线程1执行完毕之后,再执行线程2 因为我们锁住的是整个对象,对象的所有实例的方法调用都会遵从顺序调用
大家好,我是线程 = thread-1,执行需要耗时5s,请稍等
大家好,我是线程 = thread-2,执行需要耗时5s,请稍等
Process finished with exit code 0
synchronized锁的范围
锁定代码块
void add() {
synchronized (this) {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
锁定方法
synchronized void add() {
System.out.println("大家好,我是线程 = " + Thread.currentThread().getName()+",执行需要耗时5s,请稍等");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}