Home >Java >JavaInterview questions >2020 New Java Interview Questions - Multithreading (1)

2020 New Java Interview Questions - Multithreading (1)

王林
王林forward
2020-04-25 17:54:422231browse

2020 New Java Interview Questions - Multithreading (1)

1. What is the difference between parallelism and concurrency?

Parallel refers to two or more events occurring at the same time; concurrency refers to two or more events occurring at the same time interval.

Parallelism is multiple events on different entities, and concurrency is multiple events on the same entity.

Parallelism is the "simultaneous" processing of multiple tasks on one processor, and concurrency is the simultaneous processing of multiple tasks on multiple processors. Such as hadoop distributed cluster.

So the goal of concurrent programming is to make full use of each core of the processor to achieve the highest processing performance.

(Recommended related video tutorials: java video)

2. What is the difference between threads and processes?

In short, a process is the basic unit for program running and resource allocation. A program has at least one process, and a process has at least one thread. The process has an independent memory unit during execution, and multiple threads share memory resources, reducing the number of switching times and making it more efficient. A thread is an entity of a process, the basic unit of CPU scheduling and dispatch, and a basic unit that is smaller than a program and can run independently. Multiple threads in the same process can execute concurrently.

3. What is the daemon thread?

The daemon thread (daemon thread) is a service thread. To be precise, it serves other threads.

4. What are the ways to create a thread?

(1) Inherit the Thread class to create a thread class

Define a subclass of the Thread class and override the run method of the class. The method body of the run method represents what the thread wants to complete. Task. Therefore, the run() method is called the execution body.

Create an instance of the Thread subclass, that is, create a thread object.

Call the start() method of the thread object to start the thread.

(Recommended tutorial: java introductory program)

(2) Create a thread class through the Runnable interface

Define the implementation class of the runnable interface, and re- Write the run() method of this interface. The method body of the run() method is also the thread execution body of the thread.

Create an instance of the Runnable implementation class, and use this instance as the target of Thread to create a Thread object. This Thread object is the real thread object.

Call the start() method of the thread object to start the thread.

(3) Create a thread through Callable and Future

Create an implementation class of the Callable interface and implement the call() method. The call() method will serve as the thread execution body and have a return value .

Create an instance of the Callable implementation class and use the FutureTask class to wrap the Callable object. The FutureTask object encapsulates the return value of the call() method of the Callable object.

Use the FutureTask object as the target of the Thread object to create and start a new thread.

Call the get() method of the FutureTask object to obtain the return value after the execution of the child thread ends.

5. What is the difference between runnable and callable?

This is a bit of a deep question, and it also shows the breadth of knowledge a Java programmer can acquire.

The return value of the run() method in the Runnable interface is void, and what it does is purely to execute the code in the run() method;

The call() in the Callable interface ) method has a return value and is a generic type. It can be used to obtain the results of asynchronous execution in conjunction with Future and FutureTask.

If you want to know more java interview questions, please pay attention to the java Advanced Interview Questions column.

The above is the detailed content of 2020 New Java Interview Questions - Multithreading (1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete