Tag:returns may any tween Priority notes start infinite code
Data from the network, only for reference learning. 1, A Java program ends if all it 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 would continue with their EXE Cution 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 EXE Cution: When all the threads have finished executing, the Java program ends (rather, all the non-daemon threads are finished executing). When the initial thread (the thread that executes the main method) ends, the other threads still execute until the end of execution is known. However, if one of the threads executes the System.exit () method, all threads will end. 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. The object that creates the thread class does not create a new execution thread. Similarly, calling the run method of a class that implements the Runnable interface does not create a new execution thread. The new execution thread is created only if the Start method is called. 3, the Thread class saves some information attributes that can help us to identify a thread,know its status, or control it priority. These attributes is: #ID: This attribute stores a unique identifier for each Thread. #Name: This attribute store the Name o F Thread. #Priority: This attribute stores the "the" thread objects. Threads can has a priority between one and, where one is the lowest priority and are The highest&nbs P;one. It's not recommended to change the priority of the threads, but it's a possibility that you can with if you want. #Stat Us: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.: thre The ad class holds some feature information that can help us identify a thread, understand the state of a thread, and control its priority level. These features are: #ID: This feature stores a unique flag for each thread. #NAME: This feature holds the name of the thread. #Priority: This feature preserves the priority level of the thread. Thread priority from 0 to 10,0 is the lowest priority, and 10 is the highest priority. Changing the thread priority is not recommended, but it can be used if you need to use it. #Status: This feature preserves the state of the thread. Java threads can be in one of the following six states: New, runnable, blocked, waiting, time waiting, teminated. Note: The thread class does not provide SetID, SetState these two methods. 4, another possibility isTo use the sleep () method of a 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 Param Eter in the unit it represents and converts it to milliseconds. Another way to use sleep is to use the Timeunit enumeration element. This method uses the thread's sleep method to hibernate the currently executing threads, receiving a parameter representing the specified unit, and then converting it to the corresponding number of milliseconds. 5, waiting for the finalization of a threadin some situations, we'll have all the to-wait for the finalization of a thread. For example, we may have a program that would begin initializing the resources it needs before proceeding with THE&NB Sp;rest of the execution. We can run the initialization tasks as threads and wait for their finalization before continuing with the rest of the P Rogram. 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&NBSP ; called finishes its execution.: Waiting for a threadEnd in some cases, we have to wait for the end of a thread. For example, we have an application that needs to initialize some of the resources it needs before continuing with its subsequent operations. We can give the initialized task to a thread to do it. Wait for the end of this thread before performing subsequent operations. For this purpose, we can use the thread's join method. When we call this method with the thread's object, the thread that calls the Join method that joins the thread will pause until the join thread finishes executing. 6, Java provides, additional forms of the join () method: join (long milliseconds) Join (Long Milliseco NDS, 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 have the code, Thread2.join (+), the thread thread1 suspends its execution until One of these, conditions is true: thread2 finishes its execution 1000 milliseconds has been passedwhen one O F These 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 provides two other forms of join method: Join (long milliseconds) join (long milliseconds, long Nanos) The first version of the Join method, and the calling thread is not an infinite waiting thread to join. Instead, the method parameter specifies the number of milliseconds. For example: If the object Thread1 calls such code--thread2.join (1000), this time thread1 will abort execution, knowing that one of the following two conditions is true:thread2 execution end; 1000 milliseconds has passed. When one of the two conditions is met, the Join method returns. The second version of the Join method is similar to the first version, which receives the number of milliseconds and the Nono description as a parameter. 7, Creating, and running a daemon thread. Java has a special kind of thread called daemon thread. These kind of threads has very low priority and normally only executes if no other thread of the same program is Runnin G.when Daemon Threads is the only threads running in a program, and the JVM ends the program finishing these threads. With these characteristics, the daemon threads is normally used as service providers for normal (also called user) thread s running in the same program. They usually has an infinite loop, 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 is going to has CPU time and they can finish anyTime if there aren ' t any other threads running. A typical example of these kind of threads is the Java garbage collector. To create and run a daemon thread Java has a special thread called the daemon thread. This thread has a lower priority level and is typically executed only when other threads in the same application do not execute. When only the daemon is running in a program, the JVM will abort the program when it finishes executing the daemon threads. Based on these characteristics, the daemon thread is typically used by other threads running in the program as service providers. They usually have an infinite loop to wait for a service request or to perform a thread task. They don't usually do important things because we don't know when they can have CPU time and they may end up at any time if no other thread executes. A classic example of this thread is the Java garbage collector.
Java Multithreaded notes