MaxYang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: Multi Thread --- ###### tags: `JAVA` [Toc] # multi thread 的使用方式 1. extend Thead 2. implement Runnable 1,2 屬於早期java的用法,沒有辦法接收回傳值,必須通過其他的方式,例如線程間通信、共享變數,現在已經少用。 在兩者間選擇,盡可能選用implement runnable, 保留繼承的彈性。 在無需回傳值及簡易的情境,採用java 1.8 lambda方式 new thread,比起其他方式更為輕便:https://github.com/ronnielin8862/java19_practice/blob/main/src/main/java/org/example/freePractice/concucrency/lock/nolock/MainFunc.java 3. callable and future https://www.cnblogs.com/qlqwjy/p/9657981.html java 1.5以後新增 方式 3: implement callable 的 class 編寫任務,透過futureTask獲取結果。 練習(此處採用exutorService方式): https://github.com/ronnielin8862/java-practice/blob/dev/src/main/java/multiThread/startMultiThread/PracticeCallable.java 4. completableFuture (c.f.) java 1.8 以後增加方式4 ,以彌補 future接收回傳值需要阻塞線程的缺點,並且增加了不同的方式處理回傳值,達成更靈活的線程操作 cf的方式需要特別留意的地方是,預設會採用全局forkjoin線程池,而forkjoin線程池預設的thread pool是cpu核心數 (在某些具有cpu超核心技術的設備上,邏輯核與實體核不同時,是以邏輯核計算)。 在應用場景具備多線程的情況,無法充分發揮多線程的優勢,尤其是當線程工作內容具有大量io的cpu等候時間的情況。或者當在某些場景包括練習的時候,使用sleep()時,也會導致其他線程無法運行,直到既有的線程完成工作,才會開啟新線程。 因此在使用cf時,建議自定義executor線程池,並帶入cf運行,才是正確的使用姿勢。 https://github.com/ronnielin8862/java19_practice/blob/main/src/main/java/org/example/freePractice/concucrency/heap/wait/HeapA.java # multi thread 的阻塞與喚醒 1. sleep 2. suspend() 和 resume() 这两个 API 是过期的,也就是不建议使用的。 不推荐使用 suspend() 去挂起线程的原因,是因为 suspend() 在导致线程暂停的同时,并不会去释放任何锁资源。其他线程都无法访问被它占用的锁。直到对应的线程执行 resume() 方法后,被挂起的线程才能继续,从而其它被阻塞在这个锁的线程才可以继续执行。 3. yield() 使得线程放弃当前分得的 CPU 时间,但是不使线程阻塞,即线程仍处于可执行状态,随时可能再次分得 CPU 时间。只是建议具有相同优先级的其它线程可以运行。 4. 线程融合 join()方法 等待其他线程终止。在当前线程中调用另一个线程的join()方法,则当前线程转入阻塞状态,直到另一个进程运行结束,当前线程再由阻塞转为就绪状态。 5. wait() 和 notify()、notifyAll() wait() 使得线程进入阻塞状态,它有两种形式,一种允许 指定以毫秒为单位的一段时间作为参数,另一种没有参数,前者当对应的 notify() 被调用或者超出指定时间时线程重新进入可执行状态。 看起来它们与 suspend() 和 resume() 方法对没有什么分别,但是事实上它们是截然不同的。区别的核心在于,这一对方法会释放占用的锁。 这一对方法必须在 synchronized 方法或块中调用。 6. 鎖: 參考 https://hackmd.io/VkwrtobKSzyP2gU6ORQPLw?both#鎖 # multi thread 之間的通信 ## 1. 共享記憶體 a. 針對共享變數給予 volatile, 確保共享為新的變數。說明: http://percent010.blogspot.com/2015/05/volatile-java.html b. 修改共享變量時使用帶有線程安全、或原子性的物件,或者自己上鎖 ## 2. 等待/通知机制 ## 3. pip JDK提供了PipedWriter、 PipedReader、 PipedOutputStream、 PipedInputStream。其中,前面两个是基于字符的,后面两个是基于字节流的 參考: https://redspider.gitbook.io/concurrent/di-yi-pian-ji-chu-pian/5#5.4-guan-dao ## 4. join 使用 concurentA.join()的線程會等待 concurentA結束才繼續往下 ## 5. sleep 要提一下的是 a. wait可以指定时间,也可以不指定;而sleep必须指定时间。 不指定時間的wait可能造成 b. wait释放cpu资源,同时释放锁;sleep释放cpu资源,但是不释放锁,所以易死锁。 c. ***wait必须放在同步块或同步方法中***,而sleep可以任意位置 參考: https://redspider.gitbook.io/concurrent/di-yi-pian-ji-chu-pian/5 # 鎖 1. synchronized 2. reentrantLock synchronized vs reentrantLock: When using synchronized can meet your needs, use synchronized first, because it is convenient to use, it will automatically release locks, and the performance will be improved after Java 6 optimization. If you want every thread to have a chance to obtain a lock, and you can predict that your system is a typical high concurrency scenario, it is more appropriate to choose ReentrantLock. This article compares the differences and disadvantages between Synchronized and ReentrantLock, and shares some of my experiences. I hope it can help you understand them. Finally, thank you for reading. https://medium.com/javarevisited/synchronized-vs-reentrantlock-how-to-choose-cfb5306080e7 # deadlock suspend() 方法和不指定期限的 wait() 方法都可能产生死锁。 Java 并不在语言级别上支持死锁的避免,我们在编程中必须小心地避免死锁。 參考: https://juejin.cn/post/6951238100945207327 # 問題紀錄 1. pip a. pip 可以一對多、多對多嗎? b. pip的阻塞與go有何不同? 發送方也會阻塞? 發送方會有capacity的彈性嗎 2. completableFuture預設的forkjoin線程池使用數量,包含其他的線程併發嗎?還是只計算使用cf的地方的線程數量? 在其他地方已經使用的線程數,會被計入此處的forkjoin嗎 3. Forkjoin 全局線程是怎麼回事,從哪來?只影響cf? a. Java 7开始引入的一种新Fork/Join线程池https://www.liaoxuefeng.com/wiki/1252599548343744/1306581226487842 5. Executor 任務被開工就減少queue or 完工才減少? 這影響觸發拒絕策略的發生情況 a. 經過測試,只要線程開工就會將task從queue中減少,因此executor的拒絕策略觸發,較為合理可用無需另外在封裝改造 https://github.com/ronnielin8862/java19_practice/blob/main/src/main/java/org/example/freePractice/concucrency/executor/TriggerMaxThreadPool.java # 細節文章 Java多執行緒(簡潔好文): https://redspider.gitbook.io/concurrent/di-yi-pian-ji-chu-pian/5 Java多執行緒:從基本概念到避坑指南: https://www.gushiciku.cn/pl/gjwJ/zh-tw

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully