Home>Article>Java> Introduction to Java Concurrent Programming and Thread Safety Basics

Introduction to Java Concurrent Programming and Thread Safety Basics

coldplay.xixi
coldplay.xixi forward
2021-02-24 10:10:39 2462browse

Introduction to Java Concurrent Programming and Thread Safety Basics

Free learning recommendation:java basic tutorial

Thread safety basics

  • 1. Thread safety issues
  • 2. Account withdrawal case
  • 3. Synchronized code block synchronized
    • Understanding of synchronized
    • Thread safety issues of three major variables in java
    • Using synchronized on instance methods
    • Summary
    • Interview questions
  • 4. Deadlock
  • 5. How to solve thread safety issues during development
  • 6. Daemon thread
  • 7. Timer
  • 8. The third way to implement threads: implement the Callable interface
  • 9.The wait and notify methods in the Object class
  • 10.Producer and Consumer

1. Thread safety issues

2.1. Why is this important?
 In future development, our projects will all run on the server, and the server has already implemented the definition of threads, the creation of thread objects, and the startup of threads. We don't need to write any of this code.
 The most important thing is: you need to know that the program you write needs to be run in a multi-threaded environment. What you need to pay more attention to is whether the data is safe in a multi-threaded concurrent environment. (Emphasis: *****)
2.2. When will data have security issues in a multi-threaded concurrent environment?
Introduction to Java Concurrent Programming and Thread Safety Basics
Three conditions:
Condition 1: Multi-thread concurrency.
 Condition 2: There is shared data.
 Condition 3: The shared data is modified.

    ##After the above three conditions are met, there will be thread safety issues.2.3. How to solve the thread safety problem?
When there is shared data in a multi-threaded concurrent environment, and this data will be modified, there is a thread safety problem. How to solve this problem?

Threads are queued for execution. (Cannot be concurrent).
Use queued execution to solve thread safety issues. This mechanism is called: thread synchronization mechanism.
The professional term is:
Thread synchronization, which actually means that threads cannot be concurrent, and threads must be queued for execution.
How to solve the thread safety problem?Use "thread synchronization mechanism".
Thread synchronization means queuing threads. If threads are queued, part of the efficiency will be sacrificed. There is no way. Data security comes first. Only when data is safe can we talk about efficiency. Data is not secure and there is no efficiency.2.4. When it comes to thread synchronization, these two professional terms are involved:

Asynchronous programming model:
 Thread t1 and thread t2 each execute their own, t1 does not care about t2, t2 does not care about t1, no one needs to wait for anyone. This programming model is called asynchronous programming model.
  In fact, it is: multi-thread concurrency (higher efficiency.)

Synchronous programming model:

Threads t1 and thread t2 are executed in thread t1 When , you must wait for the execution of the t2 thread to end, or in other words, when the t2 thread is executing, you must wait for the execution of the t1 thread to end. A waiting relationship occurs between the two threads. This is the synchronous programming model. Less efficient. Threads are queued for execution.
Asynchronous means concurrency. Synchronization is queuing.

2. Account Withdrawal Case

Account Class

package ThreadSafe;public class Account { //账号 private String actno; //余额 private double balance; public Account(String actno, double balance) { super(); this.actno = actno; this.balance = balance; } public String getActno() { return actno; } public void setActno(String actno) { this.actno = actno; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } //取款的方法 public void withdraw(double money){ //t1和t2并发执行这个方法(t1和t2是两个栈 ,两个栈操作堆中同一个对象) //取款之前的余额 double before=this.getBalance(); //取款之后的余额 double after=before-money; //模拟一下网络延迟,会出现问题 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //更新余额 //思考:t1执行到这里了,但还没有来得及执行这行代码,t2线程进来withdraw方法了,此时一定出问题 this.setBalance(after); }}AccountThread类public class AccountThread extends Thread{ //两个线程必须共享一个账户对象 private Account act; //通过构造方法传递过来账户对象 public AccountThread(Account act) { this.act = act; } @Override public void run() { //假设取款5000 double money=5000; //多线程执行这个方法 act.withdraw(money); System.out.println(Thread.currentThread().getName()+"账户"+act.getActno()+"取款成功"+act.getBalance()); }}
Test Class

public class Test { public static void main(String[] args) { //创建账户对象 Account act=new Account("act-001",10000); //创建两个线程 Thread t1=new AccountThread(act); Thread t2=new AccountThread(act); t1.setName("t1"); t2.setName("t2"); //启动两个线程执行 t1.start(); t2.start(); }}

Introduction to Java Concurrent Programming and Thread Safety Basics3. Synchronized code block synchronized

Understanding of synchronized

//The following lines of code must be queued by threads and cannot Concurrency//

After one thread finishes executing all the code here, another thread can come in


/*The syntax of the thread synchronization mechanism is
synchronized( ){
//Thread synchronization code block.
}
The data passed in parentheses after synchronized is very critical.
This data must be shared by multiple threads to achieve multi-thread queuing.

What is written in ()?It depends on which threads you want to synchronizeAssume that there are 5 threads in t1, t2, t3, t4, and t5
You only want t1 t2 t3 to be queued, but t4 t5 does not need to be queued, what should I do
You must write an object shared by t1 t2 t3 in (), but this object is not shared by t4 t5
The shared object here is the account object
The account object is shared, and this is the account The object
sometimes does not have to be this, as long as it is the object shared by multiple threads

synchronized(this){ double before=this.getBalance(); double after=before-money; try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setBalance(after); }

Introduction to Java Concurrent Programming and Thread Safety Basics
在java语言中,任何对象都有一把锁,其实这把锁就是一个标记,(只是把它叫做锁)
100个对象,100个锁,1个对象1把锁。
以上代码的执行原理是什么呢?
1.假设t1和t2线程并发,开始执行以上代码的时候,肯定有一个先一个后,
2.假设t1先执行了,遇到了synchronized,这个时候自动找后面共享对象的对象锁,找到之后,并占有这把锁,然后执行同步代码块中的程序,在程序执行过程中一直都占有这把锁,直到同步代码块执行结束,这把锁才会释放。
3.假设t1已经占有这把锁,此时t2也遇到synchronized关键字,也会去占有后面共享对象的这把锁,结果这把锁被t1占有,t2只能在同步代码块外边等待t1的结束,直到t1把同步代码块执行结束了,t1会归还这把锁,此时t2终于等到这把锁,然后t2占有这把锁之后,进入同步代码块执行程序。
这样就达到了线程排队执行
这里需要注意的是:这个共享对象一定要选好了,这个共享对象一定是你需要排队执行的这些线程对象所共享的。
//对象
Object obj=new Object(); //实例变量(Account对象是多线程共享的,Account对象中的实例变量obj也是共享的)
synchronized(obj){}

Introduction to Java Concurrent Programming and Thread Safety Basics
括号里边只要是共享对象就行。
Object obj2=new Object(); //局部变量
synchronized(obj2){}
这样写就不安全了,因为obj2是局部变量,不是共享对象。
synchronized(“abc”){}
这样写时可以的。存在字符串常量池中
写"abc"的话所有线程都会同步

而如果是写synchronized(this){}的话,我们创建了一个新的对象act2可不用共享对象。
所以最好是写synchronized(this){},比如你要取款,要让其他取别的账户的人也要等吗?不应该,只有同时对你这1个账户取款的时候,需要等待,别人取钱的时候,需要从其他账户中取钱,就不需要等待。

java中有三大变量的线程安全问题

实例变量,在堆中
静态变量,在方法区
局部变量,在栈中

以上三大变量
局部变量永远不会存在线程安全问题
因为局部变量不共享(一个线程一个栈)
局部变量在栈中,所以局部变量永远都不会共享
实例变量在堆中,堆只有1个。
静态变量在方法区中,方法区只有1个

堆和方法区都是多线程共享的,所以可能存在线程安全问题

局部变量+常量:不会有线程安全问题。
成员变量:可能会有线程安全问题

同步代码块越小,效率越高。

//多线程执行这个方法//synchronized(this)//这里的this是AccountThread对象,这个对象不共享。synchronized(act){ act.withdraw(money); System.out.println(Thread.currentThread().getName()+"账户"+act.getActno()+"取款成功,余额:"+act.getBalance());}

在实例方法上使用synchronized

在实例方法上可以使用synchronized吗?可以的。
synchronized出现在实例方法上,一定锁的是this
没得挑,只能是this,不能是其他的对象了
所以这种方式不灵活。
另外还有一个缺点:synchronized出现在实例方法上,表示整个方法体都需要同步
可能会扩大同步的范围,导致程序的执行效率降低。所以这种方式不常用
synchronized使用在实例方法上有什么优点?
代码写的少了,节俭了
如果共享的对象是this,并且需要同步的代码是整个方法体,建议使用这种方式
StringBuffer就是在每个方法上加了synchronized关键字

使用局部变量的话,最好使用StringBuilder
因为局部变量不存在线程安全问题,选择StringBuilder,StringBuffer效率比较低。
ArrayList是非线程安全的。
Vector是线程安全的。
HashMap HashSet是非线程安全的。
Hashtable是线程安全的

总结

synchronized有三种写法:

第一种:同步代码块
    灵活
      synchronized(线程共享对象){
        同步代码块;
      }
  第二种:在实例方法上使用synchronized
      表示共享对象一定是this
      并且同步代码块是整个方法体。
  第三种:在静态方法上使用synchronized
      表示找类锁。
      类锁永远只有1把。
      就算创建了100个对象,那类锁也只有一把。

对象锁:1个对象1把锁,100个对象100把锁。
类锁:100个对象,也可能只是1把类锁。

面试题

//面试题:doother方法的执行需不需要等待dosome方法的结束。
//不需要,因为doother方法没有synchronized

public class exam01 { public static void main(String[] args) { MyClass mc=new MyClass(); Thread t1=new MyThread(mc); Thread t2=new MyThread(mc); t1.setName("t1"); t2.setName("t2"); t1.start(); try { Thread.sleep(1000); //这个睡眠的作用是:为了保证t1线程先执行 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } t2.start(); }}class MyThread extends Thread{ private MyClass mc; public MyThread(MyClass mc) { super(); this.mc = mc; } public void run(){ if(Thread.currentThread().getName().equals("t1")){ mc.dosome(); } if(Thread.currentThread().getName().equals("t2")){ mc.doOther(); } } }class MyClass{ public synchronized void dosome(){ System.out.println("doSome begin"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("doSome end"); } public void doOther(){ System.out.println("doOther begin"); System.out.println("doOther end"); }}

Introduction to Java Concurrent Programming and Thread Safety Basics
当在doother上面加了synchronized呢
//面试题:doother方法的执行需不需要等待dosome方法的结束。
//需要,因为doother方法没有synchronized

public synchronized void doOther(){ System.out.println("doOther begin"); System.out.println("doOther end"); }

//面试题:doother方法的执行需不需要等待dosome方法的结束。
//不用排队,谁也不用管谁

MyClass mc1=new MyClass();MyClass mc2=new MyClass();Thread t1=new MyThread(mc1);Thread t2=new MyThread(mc2);

//面试题:doother方法的执行需不需要等待dosome方法的结束。
//需要,因为静态方法是类锁,类锁不管创建了几个对象,类锁只有一把

MyClass mc1=new MyClass();MyClass mc2=new MyClass();Thread t1=new MyThread(mc1);Thread t2=new MyThread(mc2);public synchronized static void dosome(){ System.out.println("doSome begin"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("doSome end"); } public synchronized static void doOther(){ System.out.println("doOther begin"); System.out.println("doOther end"); }

这种锁叫排他锁:

4.死锁

Introduction to Java Concurrent Programming and Thread Safety Basics
synchronized在开发中最好不要嵌套使用,一不小心就可能导致死锁。
死锁代码要会写。
一般面试官要求你会写
只有会写的,才会在以后的开发中注意这个事儿
因为死锁很难调试。

public class DeadLock { public static void main(String[] args) { Object o1=new Object(); Object o2=new Object(); //t1线程和t2线程共享o1,o2 Thread t1=new MyThread1(o1,o2); Thread t2=new MyThread2(o1,o2); t1.start(); t2.start(); }}class MyThread1 extends Thread{ Object o1; Object o2; public MyThread1(Object o1,Object o2){ this.o1=o1; this.o2=o2; } @Override public void run() { synchronized (o1) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o2) { } } }}class MyThread2 extends Thread{ Object o1; Object o2; public MyThread2(Object o1,Object o2){ this.o1=o1; this.o2=o2; } @Override public void run() { synchronized (o2) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (o1) { } } }}

5.开发中应该怎么解决线程安全问题

聊一聊,我们以后开发中应该怎么解决线程安全问题?
  是一上来就选择线程同步吗?synchronized
  不是,synchronized会让程序的执行效率降低,用户体验不好。
  系统的用户吞吐量降低。用户体验差。在不得已的情况下再选择
线程同步机制。
第一种方案:尽量使用局部变量代替“实例变量和静态变量”。
第二种方案:如果必须是实例变量,那么可以考虑创建多个对象,这样实例变量的内存就不共享了。(一个线程对应1个对象,100个线程对应100个对象,对象不共享,就没有数据安全问题了。)
第三种方案:如果不能使用局部变量,对象也不能创建多个,这个时候就只能选择synchronized了。线程同步机制
线程这块还有那些内容呢?列举一下
7.1、守护线程
7.2、定时器
7.3、实现线程的第三种方式:FutureTask方式,实现Callable接口。(JDK8新特性。)
7.4、关于Object类中的wait和notify方法。(生产者和消费者模式!)

6.守护线程

守护线程
java语言中线程分为两大类:
  一类是:用户线程
  一类是:守护线程(后台线程)

其中具有代表性的就是:垃圾回收线程(守护线程)。
  守护线程的特点:
一般守护线程是一个死循环,所有的用户线程只要结束,
守护线程自动结束。
注意:主线程main方法是一个用户线程。
  守护线程用在什么地方呢?
  每天00:00的时候系统数据自动备份。
  这个需要使用到定时器,并且我们可以将定时器设置为守护线程。
  一直在那里看着,没到00:00的时候就备份一次。所有的用户线程如果结束了,守护线程自动退出,没有必要进行数据备份了。

package testThread;/* 实现守护线程 * */ public class ThreadTest13 { public static void main(String[] args) { // TODO Auto-generated method stub Thread t=new BakDataThread(); t.setName("备份数据的线程"); //启动之前,将线程设置为守护线程 t.setDaemon(true); t.start(); //主线程:主线程是用户线程 for(int i=0;i"+i); try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } } }}class BakDataThread extends Thread{ @Override public void run() { int i=0; //即使是死循环,但由于该线程是守护者,当用户线程结束,守护线程自动终止 while(true){ System.out.println(Thread.currentThread().getName()+"---->"+(++i)); try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } } }}

7.定时器

定时器的作用:
  间隔特定的时间,执行特定的程序。
  每周要进行银行账户的总账操作。
  每天要进行数据的备份操作。
  在实际的开发中,每隔多久执行一段特定的程序,这种需求是很常见的,
那么在java中其实可以采用多种方式实现:
  可以使用sleep方法,睡眠,设置睡眠时间,没到这个时间点醒来,执行任务。这种方式是最原始的定时器。(比较low)
  在java的类库中已经写好了一个定时器:java.util.Timer,可以直接拿来用。不过,这种方式在目前的开发中也很少用,因为现在有很多高级框架都是支持定时任务的。

  在实际的开发中,目前使用较多的是Spring框架中提供的SpringTask框架,这个框架只要进行简单的配置,就可以完成定时器的任务。

8.实现线程的第三种方式:实现Callable接口

实现线程的第三种方式:实现Callable接口。(JDK8新特性。)
这种方式实现的线程可以获取线程的返回值
之前讲解的那两种方式是无法获取线程返回值的,因为run方法返回void。
思考:
系统委派一个线程去执行一个任务,该线程执行完任务之后,可能会有一个执行结果,我们怎么能拿到这个执行结果呢
使用第三种方式:实现Callable接口方式

public class ThreadTest14 { public static void main(String[] args) throws Exception, ExecutionException { //第一步:创建一个未来任务类对象 //参数非常重要,需要给一个callable接口的实现类对象 FutureTask task=new FutureTask(new Callable(){ @Override //call方法相当于是run方法,只不过这个有返回值,线程执行一个任务,执行之后可能会有一个执行结果。 public Object call() throws Exception { System.out.println("call method begin"); Thread.sleep(1000); System.out.println("call method begin"); int a=100; int b=200; return a+b; //自动装箱 } }); //创建线程对象 Thread t=new Thread(task); //启动线程 t.start(); //这里是main方法,这是在主线程中 //在线程中,怎么获取t线程的执行结果 //get方法的执行会导致当前线程阻塞 Object obj=task.get(); System.out.println("线程执行结果"+obj); //main方法这里的程序要想执行必须等待get()方法的结束 //而get方法可能需要很久。因为get()方法是为了拿另一个线程的执行结果。 //另一个线程的执行是需要时间的 System.out.println("hello,world"); }}

这种方式的优点:可以获取到线程的执行结果
这种方式的缺点:效率比较低,在获取t线程执行结果的时候,当前线程受阻塞,效率较低。

9.Object类中的wait和notify方法

(生产者和消费者模式!)
  第一:wait和notify方法不是线程对象的方法,是java中任何一个java对象都有的方法,因为这两个方式是Object类中自带的。
  wait方法和notify方法不是通过线程对象调用,

  不是这样的:t.wait(),也不是这样的:t.notify()…不对。
  第二:wait()方法作用?
    Object o = new Object();
    o.wait();
  表示:
让正在o对象上活动的线程进入等待状态,无期限等待,直到被唤醒为止。
  o.wait();方法的调用,会让“当前线程(正在o对象上活动的线程)”进入等待状态
  第三:notify()方法作用?
    Object o = new Object();
    o.notify();
表示:
唤醒正在o对象上等待的线程
还有一个notifyAll()方法:
  这个方法是唤醒o对象上处于等待的所有线程。

Introduction to Java Concurrent Programming and Thread Safety Basics

10.生产者和消费者

Introduction to Java Concurrent Programming and Thread Safety Basics
1.使用wait方法和notify方法实现生产者和消费者模式
2.什么是生产者和消费者模式?
生产线程负责生产,消费线程负责消费
生产线程和消费线程要达到均衡
这是一种特殊的业务需求,在这种特殊的情况下需要使用wait方法和notify方法
3.wait和notify方法不是线程对象的方法,是普通java对象都有的方法
4.wait方法和notify方法是建立在线程同步的基础之上。因为多线程要同时操作一个仓库,有线程安全问题
5.wait方法作用:o.wait()让正在o对象上活动的线程t进入等待状态,并且释放掉t线程之前占有的o对象的锁
6.notify方法的作用:o.notify()让正在o对象上等待的线程唤醒,只是5通知,不会释放o对象上之前占有的锁
7.模拟这样一个需求:
仓库我们采用list集合
list集合中假设只能存储1个元素
1个元素就表示仓库满了
如果list集合中的元素个数是0,就表示仓库空了。
保证list集合中永远都是最多存储1个元素
必须做到这种效果,生产1个消费1个。

public class ThreadTest15 { public static void main(String[] args) { //创建一个仓库独享,共享的 List list=new ArrayList(); //创建两个线程对象 //生产者线程 Thread t1=new Thread(new Producer(list)); //消费者线程 Thread t2=new Thread(new Consumer(list)); t1.setName("生产者线程"); t2.setName("消费者线程"); t1.start(); t2.start(); }}//生产线程class Producer implements Runnable{ //仓库 private List list; public Producer(List list) { this.list = list; } public void run() { //一直生产 while(true){ //给仓库对象list加锁 synchronized (list) { if(list.size()>0){ //大于0说明仓库中已经有1个元素了 //当前线程进入等待状态,并且释放list集合的锁 try { list.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //程序能够执行到这里说明仓库是空的,可以生产 Object obj =new Object(); list.add(obj); System.out.println(Thread.currentThread().getName()+"---->"+obj); //唤醒消费者进行消费 list.notifyAll(); } } }}//消费线程class Consumer implements Runnable{ //仓库 private List list; public Consumer(List list) { this.list = list; } public void run() { //一直消费 while(true){ //给仓库对象list加锁 synchronized (list) { if(list.size()==0){ //仓里已经空了 try { //仓库已经空了 //消费者线程等待,并释放掉list集合锁 list.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Object obj=list.remove(0); System.out.println(Thread.currentThread().getName()+"---->"+obj); //唤醒生产者生产 list.notifyAll(); } }}

相关学习推荐:java基础

The above is the detailed content of Introduction to Java Concurrent Programming and Thread Safety Basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete