Drill down into Java multi-line Cheng

Source: Internet
Author: User
Tags thread stop throw exception

Involves 1. Thread Start (start) 2. Thread pauses (suspend () and resume ()) 3. Thread stops (interrupt with exception stop, interrupt stops with sleep, stop (), return) 4. Thread Priority ( SetPriority ()) 5. Thread-Safety-related issues (sync lock, daemon thread) I. Threads and processes     (1) a process is a basic operating unit managed by the OS (the process of viewing the Windows Task Manager is better understood)     (2) threads can be understood as subtasks that run independently in a process     Single threaded and multi-threaded, the former is characterized by queued execution, that is, synchronization, CPU utilization significantly reduced, the latter is in different tasks to switch back and forth, switching speed, so that it appears to be running many different kinds of tasks at the same time, multithreading is equal to asynchronous. Two. Using multithreading     (1) Multi-threaded implementation (two kinds):            A. Inherit the thread class             B. Implement Runnab Le interface             There is a polymorphic relationship between them, and viewing the source code shows that the thread class actually implements the Runnable interface, which means that the threads created in both ways are of the same nature at work. , which means that when using the Runnable interface, the constructor thread (Runnable target) can not only pass in the object of the Runnable interface, but also pass in an object of the thread class .         Note:            A. Thread execution is the method by which an object calls start, rather than invoking the overridden run method directly, calling the Run method is not asynchronous, not synchronous.             B. Thread calls are random, that is, the execution is independent of the order in which the code is executed, and the order of execution of the code is independent of the order of the start () method call three. Instance variables and thread safety:      (1). Do not share data             to perform its own, non-impact       (2). Sharing data in the context of   & nbsp         Multiple threads can access the same variable, but this is followed by a non-thread-safe problem (with a read in the databaseDirty data similar), this time to consider locking, using the Synchronized keyword, the purpose of queuing to call the Run method, the lock code is called "Mutex" or "critical section". Four. CurrentThread () method, returns the information that the code snippet is being called by five: The IsAlive () method determines whether the current thread is active, noting that the Thread.CurrentThread (). IsAlive () and this.isalive () may differ in the results of six. The sleep () method allows the current " Executing thread "hibernate (suspend execution) seven. GetId () method, gets the unique identity of the thread eight. Stop threads       (1). Use the exit flag to cause the thread to exit normally, that is, when the Run method completes, the thread terminates       (2). Use the Stop method to forcibly terminate a thread, which is a method of invalidating expiration       (3). Use the interrupt method in a thread break, but this method does not terminate an executing You also need to add a judgment to complete the thread stop .     1): Determine whether the thread stops,            this.interrupted (): Tests if the front thread is already in the interrupt state, after execution has the ability to clear the status flag to False, which is called two consecutive times, if the first time is true, the second time is flase.            This.isinterrupted (): Tests whether thread object is already in the interrupt state, but does not clear the status flag    true Stop thread:(1). Exception method (the most common method):After calling interrupt () in the main method, the class's Run method is so judgmental (note using Try{}carch () {} capture):
            if (this.interrupted ()) {                T Hrow new Interruptedexception ();            }        ( 2). Stop:  in slumber           that is, make thread sleep long enough in the Run method, using the Iinterrupt () method in the main method, which throws a sleep interrupted exception         (3). Violence stop (stop using):            using the Stop () method, Throws a Java.lang.ThreadDeath exception, but does not need to display the catch, and using this will release the lock, causing problems with shared data .        (4). Use return to stop the thread             The first method is similar to changing the throw exception to return; Yes, but it is recommended to use the first. Nine: Pause thread:        (1). The suspend () method pauses the thread .        (2). The resume () method resumes the paused thread         Both are obsolete methods, not recommended, 1). Causes a public synchronization object to be exclusive, so that other threads cannot access the public synchronization object. 2) in a different step, it is easy to appear because the thread pauses two and the data is out of sync. 10: Yield () method: Discards the current CPU resource, Give him other tasks to take up the CPU execution time. 11. Thread priority (1~10):        (1). Threads can prioritize, higher-priority threads get more CPU resources, That is, the CPU prioritizes the tasks in the higher-priority thread objects. &nbsP       Use the SetPriority () method to set the priority         (2). The priority of a thread is inherited, which means that a thread initiates a B thread, and the B thread has the same priority as a.         (3).rule: The CPU is maximizing execution resources to higher priority threadsA. High-priority threads are always executed mostly first, but not for high-priority threads.
B. When the priority gap is large, who executes first and the code's call order is irrelevant
c. Do not take the priority of threads and the order of running results as a measure, and the higher priority threads are not necessarily the tasks in the restricted Universal run () method every time, that is, the priority of the thread is independent of the print order.
12. Daemon Thread.The daemon thread is a special thread, and the daemon thread is automatically destroyed when there is no non-daemon thread in the process, and the typical daemon thread is the garbage collection thread (garbage collector (GC))
use Setdaemon (true) to set a thread as the daemon thread.

Drill down into Java multi-line Cheng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.