java-multithreaded Articles

Source: Internet
Author: User

1. Thread and Process Concepts

1.1. Process: Has a separate memory space, each independently executed program is called a process
? 1.2. Thread: A thread is an execution path inside a program, and a Java Virtual machine allows an application to run multiple execution threads concurrently
? 1.3. The difference between threads and processes
Each process has a separate code and data space (process context), and the switching overhead between processes is large
Threads: Threads in the same process share code and data space with little overhead for thread switching
Multi-process: multiple tasks (Programs) can be run concurrently in the operating system
Multithreading: Executing multiple execution paths concurrently in the same application

2. Creating and starting a thread

First: Inherit the thread class, overriding the Run method

Second: Implementing the Runnable interface, overriding the Run method

3. Comparison of two ways to create
? 3.1 using the Runnable interface
Can also inherit from other classes;
Maintain the consistency of the program style.
? 3.2 Direct inheritance of the thread class
Can no longer inherit from other classes;
Simple to write, can manipulate threads directly
? When you create a thread by implementing the Runnable interface, you can provide shared data for multiple threads of the same program code.

4. Thread Summary
4.1.Java threads are implemented through the Java.lang.Thread class.
? 4.2. When the program starts running, the JVM generates a thread (the main thread), and the Main method (the main method) runs on that thread.
? 4.3. You can create a new thread by creating an instance of the thread.
A. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the Method run () is called the thread body .
B. Start a thread by calling the start () method of the thread class. The thread enters the runnable (operational) state, and it goes to the thread

Scheduler registers this thread.
C. Calling the start () method does not necessarily execute the thread immediately, as stated above, it simply enters runnable instead of running.
? Note that you do not call the thread's run () method directly in the program.

5. Basic use of threading methods

java-multithreaded Articles

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.