Home  >  Article  >  Java  >  What are the characteristics and usage of threads in Java

What are the characteristics and usage of threads in Java

王林
王林forward
2023-05-20 12:07:291462browse

A process can contain several threads. Threads are components of the process. Each thread must rely on the process to which it belongs. Threads have private stacks, program counters, and local variables, but cannot own system resources. It shares all resources of the process with other threads of the parent process.

Characteristics of threads:

Threads can complete certain tasks, share the shared variables and part of the environment of the parent process with other threads, and cooperate with each other to complete tasks.

Threads run independently and do not know whether other threads exist in the process.

The execution of threads is preemptive, that is to say, the currently executing thread may be suspended at any time in order to run another thread.

A thread can create or cancel another thread, and multiple threads in a process can execute concurrently.

2: Creation and use of threads

Java uses the Thread class to represent threads. All thread objects must be instances of Thread or its subclasses. The role of each thread is to complete certain tasks. , in fact, it is to execute a program flow (a piece of code that is executed sequentially)

Option 1: Inherit the Thread class and create a thread class

Steps:

1. Define a subclass of the Thread class and rewrite the Run method of the class. The method body of the run method represents the tasks that the thread needs to complete

2. Create an instance of the Thread class, that is, create Thread object

3. Call the start method of the thread to start the thread

What are the characteristics and usage of threads in Java

What are the characteristics and usage of threads in Java

Conclusion: If you use inherited Thread To create a thread class as a subclass of the class, multiple threads cannot share the instance variables of the thread class, such as the variable i

mentioned above. Solution 2: Implement the Runnable interface

1: Define the implementation class of the Runnable interface and rewrite its Run method. The run method is also the execution body of the thread!

2: 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!

3: Call the start method to start the thread

What are the characteristics and usage of threads in Java

What are the characteristics and usage of threads in Java

Conclusion: Creating multiple threads using the Ruunable interface can Share the instance variables of the thread class. This is because in this way, the Runnable object created by the program is only the target of the thread, and multiple threads can share a target, so multiple threads can share an instance variable

Implementing multi-threading through Runnable is actually packaging run into a thread execution body, but currently Java cannot wrap any method into a thread execution body

Option 3:Use callable and future to create threads

Starting from Java5, Java provides the Callable interface. The Callable interface provides a call() method that can be used as a thread execution body. It looks very similar to Runnable, but the call() method is more powerful-mdash;— The call() method can have a return value, and the call() method can throw an exception

Java5 provides the Future interface to represent the call of the Callable interface ( ) method's return value, and provides a FutureTask implementation class for the Future interface, which implements the Future interface and also implements the Runnable interface - which can be used as the target of Thread.

Implementation steps:

1: Create an implementation class of the Callable interface and implement the call method. The call method will become the thread execution body, and the call method has a return value. When creating the callable interface Implement the class!

2: Use the FutureTask class to wrap the Callable object. The FutureTask wraps the return value of the call method of the Callable class.

3: Use the FutureTask object as the target of Thread to create and start a new thread!

4: Use the get method of FutureTask to obtain the return value after execution

What are the characteristics and usage of threads in Java

What are the characteristics and usage of threads in Java

Conclusion: Use Runnable and Callable The advantage is that - the thread class only implements the Runnable or Callable interface, and can also inherit other classes; in this method, multiple threads can share a target object, so it is very suitable for multiple identical threads to process the same resource. situation, thus separating the CPU, code and data, and the model with clear formal parameters embodies the object-oriented programming idea. The disadvantage is that the programming complexity is slightly higher.

The above is the detailed content of What are the characteristics and usage of threads in Java. For more information, please follow other related articles on the PHP Chinese website!

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