JAVA creates multiple threads and java Multithreading

Source: Internet
Author: User

JAVA creates multiple threads and java Multithreading

First, what is the difference between a thread and a process?

Process: A running program is called a process, which is responsible for the division of memory space. From a macro perspective, windows executes multiple programs at the same time.

From a micro perspective, the CPU is the program to be executed during fast switching. Essentially, only one program is executing on a time slice.

Thread: The thread is responsible for code execution, that is, the execution path in the process.

Multithreading: multiple threads in a process execute different tasks at the same time.

Question 1: The thread is responsible for code execution. Why can't we use a thread for code execution?

A: When the JVM is running, a main thread is automatically created to execute the code in the main method.

Question 2: How many threads does a JAVA program need?

A: At least two threads are required. One is the main thread responsible for the code of the main method, and the other is the garbage collector's thread responsible for garbage collection.

Advantages of multithreading: 1. solves the problem that a program can execute multiple tasks.

2. multithreading does not improve the efficiency, but increases the resource utilization rate.

Disadvantages of multithreading:

1. Increased the CPU burden

2. thread security problems

3. deadlock

How can we create multiple threads?

Method 1: Inherit the Thread class, override the run () method, and write the task code of the custom Thread in the run method. Create an object inherited from the Thread class and call the start () method to enable the Thread.

  

Method 2: implement the Runnable interface and implement the run () method. Write the task code of the custom thread in the run () method. Create a class object that implements the Runnable interface, pass the object as a real parameter to the Thread method to create a Thread object, and then call the start () method to enable the Thread.

Q: Why rewrite the run method and the run method?

A: Each thread has its own task code. The main thread task code created by jvm is all the code in the main method. The task code of the custom thread is written in the run method, the custom thread is responsible for the code in the run method.

Note: The run method cannot be called directly. Directly calling the run method is equivalent to calling a common method without opening a new thread.

Question 1: Is the Runnable implementation Class A thread object?
The Runnable implementation class object is not a thread object, but an object that implements the Runnable interface.
Only threads or sub-classes of threads can be Thread object threads with the start method to enable threads.
Question 2: why should we pass the Runnable implementation Class Object to the Thread object as a real parameter? What is the role?
The Thread class uses the target variable to record the t1 object.
Source code: public void run (){
If (target! = Null ){
Target. run (); // use the run method of the Runnable implementation class as the run method of the thread object
}
}
We recommend that you use the second method to create a custom thread: Because java is implemented with a single inheritance

Common thread methods:

Thread (String name) name of the initialization Thread
GetName () returns the thread name
SetName (String name) sets the thread object name

The number of milliseconds that the sleep () thread sleeps in. It is a static method. If the thread executes the sleep method, the thread sleep.
Why can't sleep throw an exception and only capture it?
A: The subclass is rewriting the run () method of the Thread. The exception type thrown by the subclass during method rewriting must be smaller than or equal to the exception type of the parent class.
Run () in Thread does not throw an exception, so the subclass cannot throw an exception. Therefore, exceptions cannot be thrown directly, so the capture method is used.
CurrentThread () returns the object of the thread in which the current CPU is being executed.

GetPriority () returns the priority of the current thread object. The default thread priority is 5. The larger the priority number, the higher the execution probability.
SetPriority (int newPriority) sets the thread priority. Although the thread priority is set, the specific implementation depends on the implementation of the underlying operating system (the maximum priority is 10, the minimum 1, the default value is 5 ).

 

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.