5 questions per day seven (Java threads)

Source: Internet
Author: User

What is a thread?

A thread is the smallest unit that the operating system can perform operations on, which is included in the process and is the actual operating unit of the process.
Programmers can use it for multiprocessor programming, and you can speed up operations-intensive tasks using multithreading.
For example, if a thread takes 100 milliseconds to complete a task, it takes 10 milliseconds to complete the task with 10 threads.
Java provides excellent support for multithreading at the language level, and it is also a good selling point.

What is the difference between a thread and a process?

A thread is a subset of processes, a process can have many threads, and each thread performs different tasks in parallel.
Different processes use different memory spaces, and all threads share the same amount of memory space.
Don't confuse it with stack memory, each thread has a separate stack of memory to store local data.

How do I implement threads in Java?

There are two ways to speak at the language level.
An instance of the Java.lang.Thread class is a thread but it needs to invoke the Java.lang.Runnable interface to execute, since the thread class itself is the runnable interface of the call so you can inherit the Java.lang.Thread class
or call the Runnable interface directly to override the run () method to implement the thread.

With runnable or thread?

The question is the follow-up, and we all know that we can implement threads by inheriting the thread class or calling the Runnable interface, and the question is, what better way? Under what circumstances should it be used?
This question is easy to answer if you know that Java does not support multiple inheritance of classes, but allows you to invoke multiple interfaces. So if you're going to inherit other classes, of course it's OK to call the Runnable interface.

What is the difference between the start () and run () methods in the Thread class?

This question is often asked, but it is still possible to differentiate from the way in which the participants understand the Java threading model.
The start () method is used to start the newly created thread, and start () calls the run () method internally, which is not the same as calling the run () method directly.
When you call the run () method, only the original thread is called, no new thread is started, and the start () method starts the new thread.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

5 questions per day seven (Java threads)

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.