標籤:returns may any tween 優先順序 筆記 start 無限 code
資料來源於網路,僅供參考學習。 1、A Java program ends when all its threads finish (more specifically, when all its non-daemon threads finish). If the initial thread (the one that executes the main() method) ends, the rest of the threads will continue with their execution until they finish. If one of the threads use the System.exit() instruction to end the execution of the program, all the threads end their execution.譯:當所有的線程執行完的時候,Java程式結束(更確切地說,是所有非守護線程執行完的時候)。當初始線程(執行main方法的線程)結束的時,其他線程仍然會執行知道執行結束。但是,如果有一個線程執行了System.exit()這個方法,所有的線程都將結束。 2、Creating an object of the Thread class doesn‘t create a new execution thread. Also, calling the run() method of a class that implements the Runnable interface doesn‘t create a new execution thread. Only calling the start() method creates a new execution thread.譯:建立Thread類的對象不會建立一個新的執行線程。同樣,調用實現Runnable介面的類的run方法也不會建立新的執行線程。只有調用了start方法才會建立新的執行線程。 3、The Thread class saves some information attributes that can help us to identify a thread,know its status, or control its priority. These attributes are:#ID: This attribute stores a unique identifier for each Thread.#Name: This attribute store the name of Thread.#Priority: This attribute stores the priority of the Thread objects. Threads can have a priority between one and 10, where one is the lowest priority and 10 is the highest one. It‘s not recommended to change the priority of the threads, but it‘s a possibility that you can use if you want.#Status: This attribute stores the status of Thread. In Java, Thread can be in one of these six states: new, runnable, blocked, waiting, time waiting,or terminated.譯:Thread類儲存了一些特性資訊,可以協助我們識別一個線程,瞭解一個線程的狀態,控制其優先順序別。這些特性是:#ID:這個特性儲存了每一個線程的唯一標誌。#NAME:這個特性儲存了線程的名稱。#Priority:這個特性儲存了線程的優先順序別。線程的優先順序從0到10,0是最低優先順序,10是最高優先順序。不推薦改變線程優先順序,但是如果你需要使用的話,也可以使用。#Status:這個特性儲存了線程的狀態。Java中線程可以處於下列六個狀態之一:new、runnable、blocked、waiting、time waiting、teminated. 注意:Thread類並沒有提供setId、setState這兩個方法。 4、Another possibility is to use the sleep() method of an element of the TimeUnit enumeration. This method uses the sleep() method of the Thread class to put the current thread to sleep, but it receives the parameter in the unit that it represents and converts it to milliseconds.譯:另一種可以使用sleep的方法是使用TimeUnit的枚舉元素。這種方法使用Thread的sleep方法讓現在處於執行狀態的線程休眠,接收一個參數代表指定的單位,然後把它轉化成相應的毫秒數。 5、Waiting for the finalization of a threadIn some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.譯:等待一個線程的終結有些情形下,我們必須等待一個線程的終結。例如,我們有一個應用程式在繼續完成後續操作前需要對其需要的一些資源進行初始化。我們可以把初始化的任務交給一個線程去做。在執行後續操作前等待這個線程的終結。為了這個目標,我們可以使用Thread的join方法。當我們使用Thread的對象調用這個方法時,調用加入線程的join方法的線程將會暫停,直到加入線程執行完畢才會執行。 6、Java provides two additional forms of the join() method: join (long milliseconds) join (long milliseconds, long nanos) In the first version of the join() method, instead of waiting indefinitely for the finalization of the thread called, the calling thread waits for the milliseconds specified as a parameter of the method. For example, if the object thread1 has the code, thread2.join(1000), the thread thread1 suspends its execution until one of these two conditions is true: thread2 finishes its execution 1000 milliseconds have been passedWhen one of these two conditions is true, the join() method returns.The second version of the join() method is similar to the first one, but receives the numberof milliseconds and the number of nanoseconds as parameters.譯:Java提供另外兩種形式的join方法:join (long milliseconds) join (long milliseconds, long nanos)第一個版本的join方法,調用線程不是無限等待加入的線程,而是方法參數指定的毫秒數。例如:如果對象thread1調用的這樣的代碼--thread2.join(1000),這個時候thread1就會中止執行,知道下面兩個條件中的一個條件是true:thread2執行結束;1000毫秒已經流逝當兩者條件之一滿足時,join方法返回。第二個版本的join方法與第一個版本類似,它接收毫秒數和nono描述作為參數。 7、Creating and running a daemon thread .Java has a special kind of thread called daemon thread. These kind of threads have very low priority and normally only executes when no other thread of the same program is running.When daemon threads are the only threads running in a program, the JVM ends the program finishing these threads. With these characteristics, the daemon threads are normally used as service providers for normal (also called user) threads running in the same program. They usually have an infinite loop that waits for the service request or performs the tasks of the thread. They can‘t do important jobs because we don‘t know when they are going to have CPU time and they can finish any time if there aren‘t any other threads running. A typical example of these kind of threads is the Java garbage collector.譯:建立並運行一個守護線程Java有一種特殊的線程,叫做:守護線程。這種線程的優先順序別比較低,通常只有在同一個應用程式中其他線程不執行的時候才會執行。當一個程式中只有守護線程在啟動並執行時候,JVM在執行完這些守護線程就會中止程式的運行。基於這些特徵,守護線程通常被運行在程式中的其他線程用作服務提供者。他們通常有一個無限迴圈,以等待服務要求或者執行線程任務。他們通常不做重要的事,因為我們不知道他們何時可以擁有CPU時間並且如果沒有其他線程執行的話他們可能隨時結束。這種線程的一個經典例子是:Java的記憶體回收行程。
Java 多線程筆記