Java multithreaded Programming Core Technology reading notes 1__ programming

Source: Internet
Author: User
Tags thread class
Chapter One: Java Multithreading skills


Before we learn the threads, we need to be clear. the concept of the process: process is the basis of operating system structure, is the system of resource allocation and scheduling of independent units. Windows has a running word, and a browser program is called a process. Threads: Threads are part of a process, a process consists of one or more threads, threads can be understood as a stand-alone task in the process, such as QQ in the runtime, there are many subtasks running at the same time, and then, such as friends video threads, download threads, transmission data threads, these different tasks or functions can be run at the same time.
Advantages of multiple threads: Can make full use of the computer CPU resources. Compared to a single task environment, multithreading can switch between multiple thread tasks, avoiding the CPU occupied by a task in a single task environment waiting for the event it expects and not running for a long time.


There are two ways to implement threads in Java: One is to inherit the thread class, the other is to implement the Runnable interface, both of which require the implementation of the Run method, and the execution of the thread begins with the execution of the Run method. Because Java is a single inheritance, in general, there are more implementations of RUNNABLEA interfaces in use because inheritance can be implemented at the same time. The thread class also implements the Runnable interface. Enabling a thread requires calling the thread's start () method, which is the only way to start the thread.
Execution of Threads: Thread execution is unknown, meaning we don't know when the thread starts and when it ends. When the thread is started, the execution of the thread is out of our control, and each thread executes the boot once, that is, the start () method can only be invoked once.
Construction Method: Thread (), thread (Runnable target); This shows that we can pass another thread through the thread's construction method, so that we can call another thread inside the thread.

non-thread safe: A run-time error can occur when there is a shared object between multiple threads.

CurrentThread ()Method: This method returns information about which thread the code snippet is being invoked, such as CurrentThread (). GetName () returns the current thread name. isAlive ()method to interpret whether a thread is active. Sleep (Long Mills)Thread to suspend execution for a certain amount of time. getId ()Gets the unique identification of the thread. Stop ()method to force the stop thread. But it's out of date. This method has the potential to make an unexpected error happen to the program.
The thread is usually stopped, using the interrupt () method, but this method does not really stop the execution of the thread, but adds a stop tag to the thread, which requires an interpretation to stop it.
interrupted ()method to test if the thread has been interrupted when the method is invoked, this method returns True, and this method unlocks the state. Therefore, false is returned when the method is called the second time; isinterrupted ()Method tests whether the thread is interrupted but does not clear the interrupt state.
If you want to stop a thread: We first call the interrupt () method, and then use the isinterrupted () method to determine if the thread is broken, and if the interrupt is returned with the return method.
Stopping a thread in slumber causes the thread to throw an exception and clear the stop state.
The suspend () method pauses the thread; This method may cause the thread to suspend and hold the public object permanently, causing the other threads to fail to get the object to execute. Resume () method to restore thread execution;

yield () methodThe effect is to discard the current thread's CPU resources. For other threads to execute, but the time to give up is not sure it may just give up, and immediately get CPU time slices.


Java threads have a 10-level priority, 1 to 10, the higher the number of priority, can be set by SetPriority (), the higher the priority of the thread has more access to the CPU to execute.






















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.