Java Multithreaded notes

Source: Internet
Author: User

first, a simple rough explanation of some thread-related concepts1. Parallel and concurrent parallelism: Multiple CPU instances or multiple machines executing a processing logic at the same time are true at the same time.  Concurrency: Through the CPU scheduling algorithm, let the user appear to execute simultaneously, actually from the CPU operation level is not real at the same time.  2. Resource sharing multiple threads invoke a resource, which is the same or multiple resources.  3. Thread safety in the case of concurrency, the code is multithreaded, and the thread's scheduling order does not affect the final result, which is thread-safe. 3. Synchronous synchronization in Java refers to the use of human control and scheduling to ensure that multi-threaded access to shared resources becomes thread-safe. For example, using@synchronized。

Java threads have v basic state

new State (new): when the thread object pair is created, it enters the new state, such as thread t = new MyThread ();

ready State (Runnable): when the Start () method (T.start ()) of the thread object is called, the thread enters the ready state. A thread that is in a ready state simply means that the thread is ready to wait for CPU scheduling to execute, not to say that the T.start () thread executes immediately;

Run State (Running): When the CPU starts scheduling a thread in the ready state, the thread is actually executing, which is going into the running state. Note: The ready state is the only entry into the running state, that is, the thread must first be in a ready state if it wants to execute in a running state;

blocking State (Blocked): A thread that is in the running state temporarily abandons the use of the CPU for some reason, stops execution, and then enters a blocking state until it enters the ready state, and is then given the opportunity to be called again by the CPU to enter the running state. Depending on the cause of the blockage, the blocking state can be divided into three different types:

1. Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter the waiting blocking state;

2. Synchronization blocking--the thread acquires the synchronized synchronization lock failure (because the lock is occupied by another thread), it will enter the synchronization blocking state;

3. Other blocking-the thread will go into a blocking state by calling the thread's sleep () or join () or making an I/O request. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state.

Dead State (Dead): The thread finishes executing or exits the run () method due to an exception, the thread ends the life cycle     Second, the implementation of the Runnable interface compared to inheriting the thread class has the following benefits:1. To avoid the limitations of single inheritance, a class can implement multiple interfaces 2. Suitable for resource sharing.
Use examples to illustrate resource sharing: Runnable interface: Testing: Results: Significant runnable for resource sharing

Thread Parent class:

  

Test:

  

Result: resource sharing not implemented

  

Summarize:

(resource sharing)Java multithreaded access to shared resources by: (1). If each thread executes the same code, you can use the same Runnable object, which has that shared data in the Runnable object. (2). If the code executed by each thread is different, it is necessary to use the unused Runnable object, which is implemented in two ways: 1. Encapsulates the shared resource in another object, and then passes the object to each Runnable object (in Java, the object (    Objects that are generated as arguments) are passed as parameters, and each thread is assigned to the object to complete the operation of the shared data, so that mutual exclusion and communication of each operation against that data is easily achieved. 2. Use these runnable objects as internal classes in a class, share the data as member variables in the external class, and assign each thread to the external class for the operation of the shared data, in order to achieve mutual exclusion and communication for each operation on the shared data.      These methods for external classes are called by each Runnable object that is an inner class. In short, several pieces of code to synchronize mutually exclusive are best placed in several separate methods, which are placed in a class, which makes it easier to achieve mutual exclusion and communication between them. (3). In a simple and extreme way, define a static variable in any class, and the static variable will be shared by all threads(synchronous). Synchronous method/Synchronous code blockNote: Synchronization is a high-overhead operation, so you should minimize the content of synchronization. It is usually not necessary to synchronize the entire method, using the synchronized code block to synchronize the key code.

  

Java Multithreaded notes

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.