Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the implementation methods of threads in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the implementation of threads in Java, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Threads & multithreaded threads:

A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a smaller basic unit that can run independently than a process. The thread itself basically does not own system resources, only a few resources that are essential to running (such as program counters, a set of registers and stacks), but it can share all the resources owned by the process with other threads belonging to the same process.

Multithreading:

Multithreading means that multiple different threads can be run simultaneously in a single program to perform different tasks.

The purpose of multithreaded programming is to "maximize the use of cpu resources". When the processing of a thread does not need to occupy cpu but only deals with resources such as io, other threads that need to occupy Cpu have other opportunities to obtain cpu resources. Fundamentally, this is the ultimate goal of multithreaded programming.

Second, the way of thread implementation

Thread

Runnable

Callable

Thread:

Inherits the Thread class and overrides the run method. In fact, Thread implements the Runnable interface to implement threads, class Thread implements Runnable {.

Public class Test {

Public static void main (String [] args) {

Thread01 thread01 = new Thread01 ()

Thread01.start ()

}

}

Class Thread01 extends Thread {

@ Override

Public void run () {

System.out.println ("Thread 01 executed.")

}

} Runnable:

To implement Runnable and the run method, put Runnable into Thread and start.

Public class Test {

Public static void main (String [] args) {

Thread thread = new Thread (new Runnable01 ())

Thread.start ()

}

}

Class Runnable01 implements Runnable {

Public void run () {

System.out.println ("Runnable01 running....")

}

}

The first method implements the thread by overriding the run method in Thread. In the second method, through the source code, you can find that the code of the run method in Thread is:

Public void run () {

If (target! = null) {

Target.run ()

}

}

The Runnable passed in is target, so when we execute Thread.start (), we are actually executing the run method of Runnable.

Callable:

Implementing Callable overrides the call method. Implementing Callable is similar to implementing Runnable, but it is more powerful, as shown in:

You can provide a return value at the end of the task, but not Runnable.

The call method can throw an exception, but not the run method of Runnable

The Fulture object obtained by running Callable can listen to the result of the target thread calling the call method and get the return value, (fulture.get (), which will block until the return value is obtained).

Public class Test {

Public static void main (String [] args) throws Exception {

System.out.println ("Thread of main:" + Thread.currentThread () .getName ())

Callable01 callable01 = new Callable01 ()

FutureTask ft = new FutureTask (callable01)

Thread thread = new Thread (ft)

Thread.start ()

System.out.println (ft.get ()); / / get the result returned by thread execution

}

}

Class Callable01 implements Callable {

Public Integer call () throws Exception {

System.out.println ("Thread of Callable01:" + Thread.currentThread () .getName ())

Return 111

}

}

If you put Fulture into Thread, you can imagine that Fulture should also implement the Runnable interface:

After reading the above, do you have any further understanding of the implementation of threads in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report