Java multithreading Foundation

Fengx 2021-05-04 14:45:14
java multithreading foundation


Basics

process

  • 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

multithreading

  • 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 .
版权声明
本文为[Fengx]所创,转载请带上原文链接,感谢
https://javamana.com/2021/05/20210504144300939d.html

  1. ASP调用SDK微信分享好友、朋友圈
  2. ASP calls SDK wechat to share friends and circle of friends
  3. SpringCloud(六)Bus消息总线
  4. 详解JavaScript中的正则表达式
  5. Springcloud (6) bus message bus
  6. Explain regular expressions in JavaScript
  7. Java 响应式关系数据库连接了解一下
  8. Java14它真的来了, 真是尾气都吃不到了
  9. 视频:使用Docker搭建RabbitMQ环境
  10. Java responsive relational database connection
  11. Java14 it's really coming. I can't eat the exhaust
  12. Video: building rabbitmq environment with docker
  13. SpringCloud(六)Bus消息总线
  14. 详解JavaScript中的正则表达式
  15. Springcloud (6) bus message bus
  16. Explain regular expressions in JavaScript
  17. Docker实战:用docker-compose搭建Laravel开发环境
  18. Docker: building laravel development environment with docker compose
  19. 求助,JAVA如何获取系统当前所有进程
  20. 有人用过JMeter或用HttpUnit写过测试吗????
  21. Help, Java how to get all the current processes of the system
  22. Has anyone ever used JMeter or written tests in httpUnit????
  23. Living in a mountain village in late spring
  24. Partridge day, spring of HKUST
  25. JavaScript异步编程4——Promise错误处理
  26. 海康摄像SDK开发笔记(一):海康威视网络摄像头SDK介绍与模块功能
  27. JavaScript asynchronous programming 4 -- promise error handling
  28. Haikang video SDK development notes (1): introduction and module functions of Hikvision webcam SDK
  29. JOP:用于FPGA的嵌入式实时系统中的Java优化处理器内核
  30. Spring Boot源码:使用MongoDB MongoTemplate公开REST在几分钟内实现CRUD功能
  31. Spring Boot应用程序事件教程 - reflectoring
  32. 带有Resilience4j断路器的Spring云网关 - rome
  33. 经验分享:Apache Kafka的缺点与陷阱 - Emil Koutanov
  34. 通过Spring Boot Webflux实现Reactor Kafka
  35. 从Java 8升级到Java 11应该注意的问题
  36. Jop: Java optimized processor core for FPGA embedded real time system
  37. Spring boot source code: use mongodb mongotemplate to open rest to realize crud function in a few minutes
  38. Spring boot application event tutorial - reflecting
  39. Spring cloud gateway with resilience4j circuit breaker - ROM
  40. Experience sharing: shortcomings and pitfalls of Apache Kafka - Emil koutanov
  41. Realization of reactor Kafka through spring boot Webflux
  42. RPC框架设计----Socket与I/0模型
  43. Problems in upgrading from Java 8 to Java 11
  44. RPC framework design -- socket and I / 0 model
  45. RPC框架设计----I/0模型
  46. RPC framework design: I / 0 model
  47. RPC框架设计----NIO编程缓冲区Buffer
  48. RPC框架设计----NIO编程缓冲区Buffer
  49. RPC framework design -- NiO programming buffer
  50. RPC framework design -- NiO programming buffer
  51. Java多线程基础
  52. Java multithreading Foundation
  53. 码农飞升记-00-Java发展历程
  54. Development history of coder-00-java
  55. 码农飞升记-00-Java发展历程
  56. Development history of coder-00-java
  57. Spring and Autumn Moon
  58. Node.js与Spring Boot比较? - Ryan Gleason
  59. Spring WebFlux的明显陷阱 - ŁukaszKyć
  60. Spring创始人Rod大叔对YAML的真实想法