First solve the Java thread, solve the Java thread

Source: Internet
Author: User

First solve the Java thread, solve the Java thread
Differences between threads and processes:
A process is essentially an execution program. Each process has its own code and data space. A process-based multitasking feature allows your computer to run two or more programs at the same time. For example, your computer can run other applications such as fetion while running QQ. In process-based multi-task processing, a program is the smallest unit of code assigned by the scheduler.

Thread-based is the sequential control flow within a program. It can be viewed as a lightweight process. The same thread shares code and data space. Simply put, you can use a cool dog to listen to music and search for your favorite songs.


Thread priority:
Java assigns priority to each thread to determine how to treat the thread when comparing it with other threads. The thread priority is used to determine when to switch from one running thread to another (that is, context conversion)
Conversion rules:

1. The thread can automatically discard the control. When I/O is not determined, sleep or congestion is accomplished with a clear concession. Under this assumption, all other threads are detected, and the highest priority thread to be run is granted the CPU.

2. Threads can be preemptible by high-priority threads. In this case, a low-priority thread does not take the initiative to give up, but the processor is occupied first-no matter what it is doing-the processor is occupied by a high-priority thread. Basically, once a high-priority thread needs to run, it will execute. This is called multi-task processing with priority.


Thread creation:
First: implement the Runnable interface

Second: Define Thead subclass to override its run Method

The first method is recommended when we do not need to overload other methods of Thead class. Because the Thread class defines multiple methods that can be reloaded by the derived class. The only method that must be overloaded is the run () method. This is exactly the same method required to implement the Runnable interface. Therefore, you only need to implement the interface when you do not need to reload other methods.

Thread control method:

Different from other languages, Java supports multiple threads. multithreading is often used in large and medium-sized software development in the future. With basic knowledge, you can write more code to deepen your understanding of this knowledge.


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.