Java multithreading Foundation

Fengx、 2021-05-04 13:20:11
java multithreading foundation



  • A process includes memory space allocated by the operating system , Contains one or more threads . A thread cannot exist independently , It has to be part of the process .
  • A process runs all the time , It can't be finished until all non waiting threads are finished .

The life cycle of a thread

  • New state

    • A newly generated thread starts its life cycle from a new state . It remains in this state until the program start This thread .
  • Ready state

    • When a thread is waiting for another thread to execute a task , The thread is ready . When another thread signals the ready thread , It is time to switch to the running state again .
  • Running state

    • When a thread in a new state is start in the future , The thread becomes runnable , A thread in this state is considered to start executing its task .
  • Blocked state ( Sleep state )

    • Because a thread runs out of time slices , The thread goes to sleep from the running state . When an interval expires or a waiting event occurs , The thread in this state switches to the running state .
  • Death state ( Termination status )

    • A running thread completes a task or other termination condition occurs , The thread switches to the termination state .

Three ways to create thread

  • Realization Runnable Interface ( The most simple )

  • Inherit Thread Class itself

    • Method

      • start()
      • run()
  • adopt Callable and Future Create thread

Common thread methods

  • sleep

    • The current thread is paused
  • join

    • Join to the current thread
  • setPriority

    • Thread priority
  • yield

    • A temporary suspension
  • setDaemon

    • The guardian thread

Synchronization problem

  • Multiple threads modify one data at the same time

    • Data becomes dirty data
  • Also known as Concurrency( Concurrent ) problem

  • solve the problem

    • synchronized Sentence block

      • The current thread owns the object , Until it's released
      • By synchronized Embellished , Called thread safe classes
    • Lock

    • chart

Common thread safety classes

  • Map

    • HashMap

      • Non-thread safety
      • It can store null
    • HashTable

      • Thread safety
      • Cannot be stored null
  • character string

    • StringBuilder

      • Non-thread safety
      • Single thread speed is fast
    • StringBuffer

      • Thread safety
      • Multithreaded data security
  • List

    • ArrayList

      • Non-thread safety
    • Vector

      • Thread safety

Multithreaded programming

  • Multithreading can satisfy programmers to write very efficient programs to make full use of CPU Purpose , because CPU We can keep our free time to a minimum .

  • Main concepts

    • Thread synchronization

      • Mutually exclusive synchronization

        • Only one thread is allowed to access the code segment at a point in time
      • Conditional synchronization

        • Through conditional variables and three operations to achieve : wait for , Signals and radio
      • Sync keywords

        • synchronized
    • thread deadlock

      • chart
    • Thread interaction

      • wait

        • Give possession to this Thread waiting for , And release possession temporarily
      • notify

        • Notice waiting this The thread wakes up , Can continue to possess
      • notifyAll()

        • Notify all waiting threads
    • Thread control : Hang up 、 Stop and resume

    • Thread pool

      • Mode of production and consumption

        • The object is a task by task
      • The created thread loops


  • The key to the effective use of multithreading is to understand that programs are executed concurrently rather than serially . for example : There are two subsystems in the program that need to be executed concurrently , This is where multithreading is needed .
  • Through the use of multithreading , Can write very efficient programs . But please note , If you create too many threads , The efficiency of program execution is actually reduced , Not promotion .
  • please remember , Context switching overhead is also important , If you create too many threads ,CPU It will take more time to switch context than to execute program .

  1. Java 11已经不再完全免费,不要陷入Oracle的Java 11陷阱
  2. Vue.js比jQuery更容易学习
  3. 启动/删除Docker容器时出现问题 - 如何修复
  4. eclipse run on server时出现了错误信息.求急!!
  5. 请教高手一个关于lunce的问题 Cannot rename ...\
  6. Java 11 is no longer completely free. Don't fall into the Java 11 trap of Oracle
  7. Vue. JS is easier to learn than jQuery
  8. Problem starting / deleting docker container - how to fix it
  9. There is an error message in eclipse run on server!!
  10. Ask a question about lunce: cannot rename... \
  11. 从零搭建Spring Boot脚手架(2):集成mybatis
  12. 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper
  13. 只知道java反射,宁知道内省吗?
  14. Build spring boot scaffold from scratch (2): integrate mybatis
  15. Build spring boot scaffold from scratch (4): handwritten mybatis general mapper
  16. Do you prefer introspection to reflection?
  17. ASP调用SDK微信分享好友、朋友圈
  18. ASP calls SDK wechat to share friends and circle of friends
  19. BAT 必问的 MySQL 面试题你都会吗?
  20. Do you know all the MySQL interview questions that bat must ask?
  21. ASP调用SDK微信分享好友、朋友圈
  22. ASP calls SDK wechat to share friends and circle of friends
  23. SpringCloud(六)Bus消息总线
  24. 详解JavaScript中的正则表达式
  25. Springcloud (6) bus message bus
  26. Explain regular expressions in JavaScript
  27. Java 响应式关系数据库连接了解一下
  28. Java14它真的来了, 真是尾气都吃不到了
  29. 视频:使用Docker搭建RabbitMQ环境
  30. Java responsive relational database connection
  31. Java14 it's really coming. I can't eat the exhaust
  32. Video: building rabbitmq environment with docker
  33. SpringCloud(六)Bus消息总线
  34. 详解JavaScript中的正则表达式
  35. Springcloud (6) bus message bus
  36. Explain regular expressions in JavaScript
  37. Docker实战:用docker-compose搭建Laravel开发环境
  38. Docker: building laravel development environment with docker compose
  39. 求助,JAVA如何获取系统当前所有进程
  40. 有人用过JMeter或用HttpUnit写过测试吗????
  41. Help, Java how to get all the current processes of the system
  42. Has anyone ever used JMeter or written tests in httpUnit????
  43. Living in a mountain village in late spring
  44. Partridge day, spring of HKUST
  45. JavaScript异步编程4——Promise错误处理
  46. 海康摄像SDK开发笔记(一):海康威视网络摄像头SDK介绍与模块功能
  47. JavaScript asynchronous programming 4 -- promise error handling
  48. Haikang video SDK development notes (1): introduction and module functions of Hikvision webcam SDK
  49. JOP:用于FPGA的嵌入式实时系统中的Java优化处理器内核
  50. Spring Boot源码:使用MongoDB MongoTemplate公开REST在几分钟内实现CRUD功能
  51. Spring Boot应用程序事件教程 - reflectoring
  52. 带有Resilience4j断路器的Spring云网关 - rome
  53. 经验分享:Apache Kafka的缺点与陷阱 - Emil Koutanov
  54. 通过Spring Boot Webflux实现Reactor Kafka
  55. 从Java 8升级到Java 11应该注意的问题
  56. Jop: Java optimized processor core for FPGA embedded real time system
  57. Spring boot source code: use mongodb mongotemplate to open rest to realize crud function in a few minutes
  58. Spring boot application event tutorial - reflecting
  59. Spring cloud gateway with resilience4j circuit breaker - ROM
  60. Experience sharing: shortcomings and pitfalls of Apache Kafka - Emil koutanov