Java Thread類最終同步的void join(long time_in_ms)方法,帶有示例

線程類最終同步無效連接(long time_in_ms) (Thread Class final synchronized void join(long time_in_ms))

  • This method is available in package java.lang.Thread.join(long time_in_ms).

    軟件包java.lang.Thread.join(long time_in_ms)中提供了此方法。

  • join(long time_in_ms) method is applicable when currently executing thread wants to wait for a particular amount of time in milliseconds until completing some other thread then we should go for join(long time_in_ms) method of Thread class.

    join(long time_in_ms)方法適用于當前正在執行的線程想要以毫秒為單位的特定時間,直到完成其他線程為止,然后我們應該使用Thread類的join(long time_in_ms)方法。

  • This method is synchronized that is only one thread is allowed to operate one object.

    該方法是同步的,僅允許一個線程操作一個對象。

  • This method is not static so we cannot access this method with the class name too.

    此方法不是靜態的,因此我們也無法使用類名訪問此方法。

  • This method is final we can't override this method in child class.

    此方法是最終方法,我們不能在子類中覆蓋此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回類型為void,因此它不返回任何內容。

  • This method throws an InterruptedException so it is needed to handle exception either by try-catch or throws otherwise we will get a compile-time error.

    該方法拋出InterruptedException異常,因此需要通過try-catch或throws來處理異常,否則我們將獲得編譯時錯誤。

For example, We have two threads [t1 – PreparedExamPaper], [t2 – PrintingExamPaper] so will see what will happen.

例如,我們有兩個線程[ t1 – PreparedExamPaper],[ t2 – PrintingExamPaper],因此將看到會發生什么。

Let suppose if a thread t1 executes, t2.join(1000), then thread t1 will entered into waiting state for 1000 milliseconds until t2 completes and suppose in case if t2 couldn't complete its execution in 1000 ms so in that case, t1 will get a chance to execute and if thread t1 goes into waiting state or sleep mode then again t2 will get a chance to execute its execution for 1000 ms and the same process will repeat.

假設如果線程t1執行了t2.join(1000) ,則線程t1將進入等待狀態1000毫秒,直到t2完成為止,并假設t2無法在1000 ms內完成執行,因此在這種情況下, t1線程t1將有機會執行,并且如果線程t1進入等待狀態或睡眠模式,則t2再次有機會執行其執行1000 ms,并且將重復相同的過程。

Syntax:

句法:

    final synchronized void join(long time_in_ms){
}

Parameter(s):

參數:

When we write t2.join(2000), so this line means currently executing thread will stop its execution for 2000 milliseconds until t2 completion.

當我們編寫t2.join(2000)時 ,此行表示當前正在執行的線程將在2000毫秒內停止執行,直到t2完成。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回類型為void ,它不返回任何內容。

Java程序演示join(long time_in_ms)方法的示例 (Java program to demonstrate example of join(long time_in_ms) method)

/*  We will use Thread class methods so we are importing 
the package but it is not mandate because 
it is imported by default
*/
import java.lang.Thread;
class MyThread extends Thread {
//Override run() method of Thread class 
public void run() {
for (int i = 0; i < 5; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
System.out.println("Thread Ended :" + Thread.currentThread().getName());
}
}
class MainThread1 {
public static void main(String[] args) throws Exception {
MyThread mt = new MyThread();
mt.start();
/* Note -1*/
mt.join(1000);
for (int j = 0; j < 2; ++j)
System.out.println("Thread started:" + Thread.currentThread().getName());
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}

Note1 : Here, we have written /*mt.join(1000)*/ means currently executing thread [main] will give a chance to another thread named [MyThread mt] for 1000 ms and then after main thread will get a chance to execute and if main thread goes into waiting for state or sleep mode then again MyThread will get a chance for 1000 ms and this repeats until complete execution of MyThread.

注意1:這里,我們已經編寫了/*mt.join(1000)*/,表示當前正在執行的線程[main]將給另一個名為[ MyThread mt ]的線程一個機會,持續1000 ms,然后在主線程執行之后,有機會執行如果主線程進入等待狀態或睡眠模式,則MyThread再次有機會獲得1000毫秒的機會,并重復執行直到MyThread完全執行為止。

Output

輸出量

E:\Programs>javac MainThread1.java
E:\Programs>java MainThread1
Thread started:Thread-0
Thread started:Thread-0
Thread started:main
Thread started:main
Thread ended:main
Thread started:Thread-0
Thread started:Thread-0
Thread started:Thread-0
Thread Ended :Thread-0

翻譯自: https://www.includehelp.com/java/thread-class-final-synchronized-void-join-long-time_in_ms-method-with-example.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540444.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540444.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540444.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

RYU控制器安裝`

2019獨角獸企業重金招聘Python工程師標準>>> 同樣是參考了http://linton.tw/2014/02/11/note-how-to-set-up-ryu-controller-with-gui-component/的內容。 1. 由于Ubuntu中自帶有Python&#xff0c;因此直接開始安裝pip apt-get install python-pip apt-get i…

[轉載] mac開發者,你不得不知道的環境變更設置方法(如Java的環境變更 source命令 )

參考鏈接&#xff1a; 設置Java環境 Mac是基于Unix的&#xff0c;所有先來幾個常識與命令&#xff1a; Unix中雙引號單引號反引號(" )的區別 Unix中雙引號起到“弱引用”的作用:被引用的字符大部分被按照字符字面的意思解釋執行&#xff0c;除了了$,\,字符除外。 [因…

人形機器人正在美國史密森尼博物館中擔任導游的工作

Te article has been removed, please visit IncludeHelps home page for more articles 該文章已被刪除&#xff0c;請訪問IncludeHelp的主頁以獲取更多文章翻譯自: https://www.includehelp.com/News/a-humanoid-robot-is-doing-the-job-of-a-guide-in-the-smithsonian-museu…

normalizr API

APInormalizedenormalizeschemaArrayEntityObjectUnionValuesnormalize(data, schema)Normalizes input data per the schema definition provided. 根據提供的schema定義規范化輸入數據。data: required Input JSON (or plain JS object) data that needs normalization.schem…

[轉載] 【Java】基礎06:HelloWorld入門程序

參考鏈接&#xff1a; 從Hello World示例開始Java編程 HelloWorld它的中文意思是&#xff1a;“你好&#xff0c;世界”。 仿佛代表著計算機對世界說出來的第一句話&#xff0c;因為它簡潔實用&#xff0c;所以被作為入門程序廣泛使用。 Java程序開發三步驟&#xff1a;編…

[轉載] Java中的命名參數

參考鏈接&#xff1a; Java命名約定 創建具有許多參數的方法是一個主要的缺點。 每當需要創建這樣的方法時&#xff0c;就在空氣中聞一聞&#xff1a;這是代碼的味道。 強化單元測試&#xff0c;然后進行重構。 沒有借口&#xff0c;沒有屁股。 重構&#xff01; 使用構建器模…

[轉載] JVM(一):JVM體系結構詳解

參考鏈接&#xff1a; JVM如何工作–JVM體系結構 JVM簡介 JVM是Java程序得以運行的平臺&#xff0c;也是Java程序可以跨平臺的底層支撐&#xff0c;從整體上來看&#xff0c;JVM的主要功能可以分為加載和執行兩大塊。其中類加載器負責.class文件的尋址與加載&#xff0…

數據庫連接池的設計思路及java實現

2019獨角獸企業重金招聘Python工程師標準>>> connectionPool.DBConnectionManager [java] view plain copy package connectionPool; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; i…

[轉載] java虛擬機 jvm 出入java棧 棧空間內存分配

參考鏈接&#xff1a; Java虛擬機(JVM)堆棧區域 java棧空間是一塊線程私有的內存空間&#xff0c;java堆和程序數據密切相關&#xff0c;那么java棧就是和線程執行密切相關。線程最基本的執行行為就是函數的調用。每次函數調用其實是通過java棧傳遞數據的。 數據結構中的棧的…

SVN命令行更新代碼

命令列表 svn help查看幫助信息 Available subcommands: add auth blame (praise, annotate, ann) cat changeli…

[轉載] Java中Runtime的使用

參考鏈接&#xff1a; Java中的JVM的關閉掛鉤 1 JDK中Runtime的定義 http://blog.csdn.net/lysnow_oss/archive/2007/05/12/1606349.aspx <轉載> 那就首先說點Runtime類吧&#xff0c;他是一個與JVM運行時環境有關的類&#xff0c;這個類是Singleton的。我…

窄帶物聯網(NB-IoT)初步了解

哪有什么天生如此&#xff0c;只是我們天天堅持。既然總有人要贏的話&#xff0c;為什么不能是我呢&#xff1f;[TOC] 什么是NB-Iot? 基于蜂窩的窄帶物聯網&#xff08;Narrow Band Internet of Things, NB-IoT&#xff09;成為萬物互聯網絡的一個重要分支。NB-IoT構建于蜂窩網…

ai人工智能_人工智能能力問答中的人工智能不確定性

ai人工智能1) Which of the following is true with respect to uncertainty in AI systems? Uncertainty arises when we are not 100 percent confident in our decisionsWhenever uncertainty arises, there is needs to be an estimation taken for getting to any conclu…

[轉載] 弄懂JDK、JRE和JVM到底是什么

參考鏈接&#xff1a; JDK JRE和JVM之間的區別 首先是JDK JDK(Java Development Kit) 是 Java 語言的軟件開發工具包(SDK)。 在JDK的安裝目錄下有一個jre目錄&#xff0c;里面有兩個文件夾bin和lib&#xff0c;在這里可以認為bin里的就是jvm&#xff0c;lib中則是jvm工作所需要…

mcq 隊列_人工智能搜索問題能力問題解答(MCQ)

mcq 隊列1) The main Aim of the AI system is to provide a solution for real-life problems by acting and thinking humanly. Whenever an agent is confronted by a problem, what is the first step that it follows towards searching a solution to the problem? Sear…

JavaOne大事紀:IBM談OpenJ9和Open Liberty

JavaOne大會以IBM陳述其最近對開源社區的貢獻作為開場&#xff1a;OpenJ9、Open Liberty和MicroProfile。IBM杰出工程師John Duimovich做了“IBM和Java&#xff1a;助力下一代創新”的開場演講。\\讀者可以回看演講視頻。\\Duimovich說IBM之所以致力于推動Java生態系統的創新&a…

[轉載] JVM中對象的回收過程

參考鏈接&#xff1a; JVM是否創建Main類(具有main()的類)的對象 當我們的程序開啟運行之后就&#xff0c;就會在我們的java堆中不斷的產生新的對象&#xff0c;而這是需要占用我們的存儲空間的&#xff0c;因為創建一個新的對象需要分配對應的內存空間&#xff0c;顯然我的內…

c語言格式對齊填充_C ++中類的大小 課堂上的填充和對齊| 派生類的大小

c語言格式對齊填充Prerequisite: 先決條件&#xff1a; sizeof() operator in C/C C / C 中的sizeof()運算符 Size of struct in C C中的struct大小 We know that a struct size is not only the summation of all the data members, rather its the minimum sum guaranteed. …

ELK系列~對fluentd參數的理解

這段時候一直在研究ELK框架&#xff0c;主要集成在對fluentd和nxlog的研究上&#xff0c;國內文章不多&#xff0c;主要看了一下官方的API&#xff0c;配合自己的理解&#xff0c;總結了一下&#xff0c;希望可以幫到剛入行的朋友們&#xff01; Fluentd&#xff08;日志收集與…

[轉載] Java中的50個關鍵字

參考鏈接&#xff1a; Java平臺如何獨立 Java中的50個關鍵字 關鍵字也稱為保留字&#xff0c;是指java語言中規定了特定含義的標示符。對于保留字&#xff0c;用戶只能按照系統規定的方式使用&#xff0c;不能自行定義。Java中有50個常用關鍵字&#xff1a; 與數據類型相關…