JAVA multithreading and concurrent basic Interview Q & A, java Multithreading

Source: Internet
Author: User

JAVA multithreading and concurrent basic Interview Q & A, java Multithreading

Multithreading and concurrency are one of the questions that the interviewer prefers to ask during the Java technical interview. Here, most important questions are listed from the interview perspective, but you should still master the basic knowledge of Java multithreading to match the problems encountered in the future. (Proofreader Note: I strongly agree with this opinion.)

Java multithreading interview question 1. What is the difference between processes and threads?

A process is an independent (self contained) running environment, which can be considered as a program or an application. A thread is a task executed in the process. A Java Runtime Environment is a single process that contains different classes and programs. A thread can be called a lightweight process. Threads require less resources to create and reside in the process, and can share resources in the process.

2. What are the benefits of multi-threaded programming?

In multi-threaded programs, multiple threads are concurrently executed to improve program efficiency. The CPU does not enter idle state because a thread needs to wait for resources. Multiple threads share heap memory. Therefore, it is better to create multiple threads to execute some tasks than to create multiple processes. For example, Servlets is better than CGI because Servlets supports multithreading and CGI does not.

3. What is the difference between the user line and the daemon thread?

When we create a thread in a Java program, it is called a user thread. A daemon thread is executed in the background and does not stop JVM termination. When no user thread is running, the JVM closes the program and exits. The sub-thread created by a daemon thread is still a daemon thread.

4. How do we create a thread?

There are two ways to create a Thread: one is to implement the Runnable interface, and then pass it to the Thread constructor to create a Thread object; the other is to directly inherit the Thread class. To learn more, read this article on how to create a thread in Java.

5. What are the different thread lifecycles?

When we create a new thread in a Java program, its status isNew.When we call the start () method of the thread, the status is changedRunnable. The thread SchedulerRunnableThe threads in the thread pool allocate CPU time and change their statusRunning.Other thread statuses includeWaiting, BlockedAndDead. Read this article to learn more

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.