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