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

How to use Java multithreading

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use Java multithreading, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Multithreaded programming

What is multithreading, in popular terms, is to let your code do several things at the same time.

And one of our code files or a project is a process, and if we want to improve efficiency, we can open more files for multi-processes, or we can create multiple threads in the process (write a few more methods). But multi-process is more resource-consuming, so it is generally recommended that multi-threading, in the code, let the code do what several files do.

Multithreaded programming can make our code more efficient.

The working process of a thread

To use multithreading, you need to create one or more threads.

Then let the thread call the CPU resource and start running

Then run and wait for all threads to finish.

Then delete the resource and end the thread

Java thread has priority

Priority is that the system determines the scheduling order of threads, but does not execute the order, because the execution of multi-threads is disordered.

The scheduling order is only the order in which the system allocates resources, and cannot be executed without allocating resources.

Create multithreading

How to create multithreading in Java code, Java has built-in libraries for multithreading.

1. Inherit the Thread class and override the run () method

We need to write a class A to inherit the Thread class and override the run () method.

The run () method contains the code that we need to multithread.

Then instantiate the object of class An in the main class and call the strat method.

Public class TheadText {public static void main (String [] args) {/ / instantiate StartThread and call the start method to make the thread move StartThread T1 = new StartThread ("one"); t1.start (); StartThread T2 = new StartThread ("two"); t2.start (); StartThread T3 = new StartThread ("three"); t3.start () StartThread T4 = new StartThread ("four"); t4.start ();}} / * 1. Inherit the Thread class and override the run method * / class StartThread extends Thread {String name; StartThread (String name) {this.name = name;} @ Override public void run () {System.out.println (name+1); System.out.println (name+2); System.out.println (name+3); System.out.println (name+4); System.out.println (name+5);}}

two。 Implement the interface Runnable interface, implement the run method

Using the Runnable interface avoids the limitation of single inheritance. Thread classes only implement the Runnable interface and can inherit other classes.

Public class TheadText {public static void main (String [] args) {StartRunnable startRunnable1 = new StartRunnable ("one"); Thread te1 = new Thread (startRunnable1); te1.start (); StartRunnable startRunnable2 = new StartRunnable ("II"); Thread te2 = new Thread (startRunnable2); te2.start (); StartRunnable startRunnable3 = new StartRunnable ("III"); Thread te3 = new Thread (startRunnable3) Te3.start (); StartRunnable startRunnable4 = new StartRunnable ("four"); Thread te4 = new Thread (startRunnable4); te4.start ();}} / * * 2. Implement Runnable interface, implement run method * / class StartRunnable implements Runnable {String name;// overload method, help to distinguish StartRunnable (String name) {this.name = name;} @ Override public void run () {System.out.println (name+1); System.out.println (name+2); System.out.println (name+3); System.out.println (name+4) System.out.println (name+5);}}

3. Implement Callable interface and call method

I studied this for a long time.

After implementing this interface, you also need to implement the call method

Then encapsulate our instantiated object through the FutureTask class

Finally, we need to instantiate the Thread class for start operation to start our thread.

Code demonstration:

Public class TheadText {public static void main (String [] args) {/ / first instantiate StartCallable a = new StartCallable (); / / then call the FutureTask class FutureTask te1 = new FutureTask (a); / / set our name property a.name = "one"; / / start our thread-new Thread (te1). Start () StartCallable b = new StartCallable (); FutureTask te2 = new FutureTask (b); b.name = "two"; new Thread (te2). Start (); StartCallable c = new StartCallable (); FutureTask te3 = new FutureTask (c); c.name = "three"; new Thread (te3). Start (); StartCallable d = new StartCallable (); FutureTask te4 = new FutureTask (d) D.name = "four"; new Thread (te4). Start ();}} / * * implement Callable interface and call method * / class StartCallable implements Callable {String name; public String call () throws Exception {System.out.println (name+1); System.out.println (name+2); System.out.println (name+3); System.out.println (name+4); return name }}

Life cycle of a thread

The thread waits for CPU's resource allocation after we create it, then starts to work, and disappears after finishing our assigned work.

Thank you for reading this article carefully. I hope the article "how to use Java Multithreading" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report