Java Multithreading Implementation (i)

Source: Internet
Author: User

The purpose of multithreading

When it comes to multithreading, there is only one directory, and better utilization of CPU resources, we allow the CPU to handle multiple tasks simultaneously, shortening computation and processing time.

Before implementing multithreading, first understand some of the concepts under Multithreading:

Multithreading: Running more than one thread in a process

Parallel: Multiple CPU instances or multiple computers running a processing logic at the same time, in which case it is true that the same value is on the line.

Concurrency: Through the CPU scheduling algorithm, so that the user seems to be in parallel, but in fact, the CPU is not really at the same time, the following quoted netizens a picture:


Thread Safety: Thread safety is generally a variable or a piece of code, that is, in the case of concurrency, the code is multithreaded use, but the scheduling order of the thread does not apply the final result, we write the program only need to consider the system's memory and CPU is sufficient, in turn, Non-thread-safe even if the thread's line of precedence may cause a different result

Synchronization: Synchronization in Java refers to the use of human control program value lines, to ensure that common resources to multi-threaded access is thread-safe, general synchronization has a variety of implementations such as synchronized keyword and CAS algorithm, etc.

The state of the thread

The thread starts from the beginning to the result by following several states:

NEW: Creates a new thread that has not yet started running

RUNNABLE: Threads run in a Java virtual machine, but wait for operating system resources, such as CPU resources

BLOCKED: Thread is blocked while waiting for the monitor lock to be released into the sync method

Waiting: Thread waits, the state is the current thread waits for another thread value line a specific program

Timed_waiting: And waiting type, but the wait will specify a specific time

TERMINATED: Process Run complete

The above status can refer to the Java source code to describe the status of the process: thread.state


Transition between process states

Lock

A lock in Java is a monitor that provides the necessary support. A logical lock is a piece of data in the head of an object's memory heap, each object in the JVM has a lock (mutex) that any program can use to coordinate access to the object, and if any program wants to access the object instance, it must obtain a lock on the object (with a locale in the lock). Other threads attempting to access the object must wait until the instance that acquires the lock releases the lock (the token that alters the lock)

Locks are used to protect code fragments, and only one thread can execute the protected code at any time, and if a method uses the Synchronized keyword declaration, the object's lock will protect the entire method

Monitor

A monitor can be thought of as a special room with some data and code, but only one person (consumer) can use the room at a time, and if you want to enter the room, you must go to the lobby (Entry Set) to queue, the scheduler will use a certain standard such as FIFO, Select a consumer (thread) from the lobby to enter a special room, if the thread is suspended for some reason, it will be scheduled by the scheduler waiting Room (wait Set), and after a period of time to reassign to a special room.


Simply put: A monitor is used to monitor a thread into a special room, and he ensures that only one thread at a time can access data and code in a particular room.


Wait/notify must exist in synchronized, and these three keywords are for the same monitor

Java Multithreading Implementation (i)

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.