Java Threading Small Questions and Answers

Source: Internet
Author: User

Issue 1: Process-to-thread relationships

Process: A process refers to a program that is running. To be exact, when a program goes into memory, it becomes a process, and the process is a program that is in the process of running and has a certain independent function.
Thread: A thread is an execution unit in a process that is responsible for the execution of a program in the current process, with at least one thread in a process. There can be multiple threads in a process, and this application can also be called a multithreaded program.
In short: After a program runs at least one process, a process can contain multiple threads

2. Multithreading two ways to implement

① Create thread mode one inherits the thread class
Steps to create a thread:
1 defines a class that inherits the thread.
2 override the Run method.
3 Creating a Subclass object is creating a thread object.
4 calls the Start method, opens the thread and lets the thread execute it, and tells the JVM to call the Run method.

② Creating threads-Implementing the Runnable interface
1, the definition class implements the Runnable interface.
2. Overwrite the Run method in the interface.
3. Create the object of the thread class
4. Pass the subclass object of the Runnable interface as a parameter to the constructor of the thread class.
5. Call the Start method of the thread class to open the thread.

Think: What is the difference between a thread object calling the Run method and calling the Start method?
The thread object calls the Run method without opening the thread. The method is called only by the object. The thread object calls start to open the thread and lets the JVM call the Run method to execute in the open thread

Think: Why do we inherit the thread class and call its Start method to open the thread?
Inherit the thread class: Because the thread class is used to describe threads, a thread should have functionality. So why not just create the thread class object? The following code:

Thread t1 = new Thread();t1.start();//这样做没有错,但是该start调用的是Thread类中的run方法,而这个run方法没有做什么事情,更重要的是这个run方法中并没有定义我们需要让线程执行的代码。
Question 3: The difference between two ways of implementing multithreading

Why do----need to set a class to implement the Runnable interface? What is the difference between inheriting the thread class and implementing the Runnable interface?
Implementing the Runnable interface avoids the single inheritance limitations of inheriting the thread class (You can't let my class inherit only the other classes). Override the Run method in the Runnable interface to define the thread task code in the Run method.
Creates an object of the thread class, and only objects that create the thread class can create threads. The thread task has been encapsulated into the Runnable interface's Run method, and the Run method belongs to the subclass object of the Runnable interface, so the subclass object is passed as a parameter to the thread's constructor, so that the thread object can be created with the task of defining the thread to run.

Question 4: The thread pool principle

The thread pool, in fact, is a container that accommodates multiple threads, where threads can be reused, eliminating the need to create thread objects frequently (new thread ();), without having to repeatedly create threads and consume too much resources.

------Explain in detail why you should use the thread pool?
In Java, it is quite expensive to create a new thread if each request arrives. In practice, the time spent creating and destroying threads and the amount of system resources consumed are quite large, and may even be more than the time and resources required to process actual user requests. In addition to the overhead of creating and destroying threads, the active thread also consumes system resources. If you create too many threads in a single JVM, you may cause your system to be running out of resources due to excessive memory consumption or "over-switching". To prevent resource shortages, there are ways to limit the number of requests processed at any given moment, minimizing the number of threads that are created and destroyed, especially if some of the resources are expensive to create and destroy, and use existing objects to service them as much as possible.
Thread pooling is primarily used to address thread life-cycle overhead and resource-poor issues. By reusing threads for multiple tasks, the overhead of thread creation is distributed across multiple tasks, and the delay caused by thread creation is eliminated because the thread already exists when the request arrives. In this way, you can immediately service the request and use the application to respond faster. In addition, the lack of resources can be prevented by adjusting the number of threads in the thread appropriately.

Question 5: The thread's life cycle

① new thread: After creating a thread in the Java language using the new operator, the thread is simply an empty object that has some characteristics of the class thread, but at this point the system does not allocate resources for it, and the thread is in the created state.
When a thread is in the creation state, various properties, such as thread priority (setpriority), thread name (setName), and thread type (Setdaemon), can be set through the method of the thread class.

② ready State (Runnable): After starting a thread with the start () method, the system allocates the required resources, except the CPU, to the thread in a ready state.
③ running State (Running): The Java runtime system selects a thread in the ready state by dispatching it to occupy the CPU and turn it into a running state. At this point, the system actually executes the thread's run () method.
④ Blocking and waking threads
Blocking state (Blocked): A running thread enters a blocking state when it cannot continue running for some reason. These reasons include:

(a) When a method of blocking type such as sleep () of a thread object is executed, the thread object is placed in a block set and waits for a timeout to automatically wake up.

(b) When multiple threads attempt to enter a synchronization region, the thread that fails to enter the synchronization area is placed in the lock set until the lock in that synchronization area is acquired and ready.

(c) When a thread executes the wait () method of an object, the thread is placed in the waiting set of the object, knowing that the Notify () method that executed the object waits () The execution of the/notify () method requires the thread to obtain the lock on the object first.

⑤ Death State (Dead): The thread enters the dead state after the run () method executes. In addition, if the thread executes the interrupt () or stop () method, it will also enter the dead state in an abnormally exited manner.

Java Threading Small Questions and Answers

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.