Initial solution of threads in Java

Source: Internet
Author: User

The difference between a thread and a process:
A process is essentially an executing program that has separate code and data space for each process. Process-based multitasking is characterized by allowing your computer to run two or more programs at the same time. For example, your computer can fetion and other applications while it is running QQ. In a process-based multitasking process, the program is the smallest unit of code that the dispatcher dispatches.

a thread (thread-based) is a sequential control flow within a program. Can be seen as a lightweight process that shares code and data space with a thread. in short, you listen to music with a cool dog, and you can also search for your favorite songs.


Priority of the thread:
Java prioritizes each thread to determine how the thread should be treated when compared to other threads. The priority of a thread is to decide when to switch from one running thread to another (that is, context translation)
Conversion rules:

1. Threads can automatically discard control. In the case where I/O is not determined, sleep or congestion is accomplished by explicit concessions. Under this assumption, all other threads are instrumented and the highest priority thread ready to run is granted to the CPU.

2. Threads can be preempted by high-priority threads. In this case, the low-priority thread does not abandon itself, and the processor is simply accounted for-whatever it is doing-the processor is occupied by a high-priority thread. Basically, once a high-priority thread is running, it executes. This is called multi-tasking with priority.


creation of threads:
First: Implementing the Runnable Interface

Second: Define THEAD subclasses to override their run methods

The choice of these two methods is best used when we do not need to overload other methods of the Thead class. Because the thread class defines several methods that can be overloaded by derived classes. For all methods, the only one that must be overloaded is the run () method. This is exactly the same approach that is required to implement the Runnable interface. So you do not need to overload other methods to implement only the interface.

Methods for controlling Threads:

Unlike other languages, Java is built to support multi-threaded, multithreading is a large-scale software development in the future often used. With basic knowledge, a lot of hands-on code to deepen the understanding of this knowledge.


Initial solution of threads in Java

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.