Table of Contents
Preface
1. Four ways to implement multi-threading
1. Inherit the Thread class to create threads
2. Implement the Runnable interface to create a thread
3. Implement the Callable interface
4. Implement a thread that returns a result
2. Multi-threading related knowledge
1. The difference between Runnable and Callable
2. How to start a new thread and the difference between calling the start and run methods
3. Basic methods related to threads
4. The difference between wait() and sleep()
5 .Multi-threading principle
Home Java javaTutorial What are the ways to implement multithreading in Java?

What are the ways to implement multithreading in Java?

May 18, 2023 pm 12:55 PM
java

Preface

There are four main ways to implement Java multi-threading:

① Inherit the Thread class and implement the Runnable interface

② Implement the Callable interface and create it through the FutureTask wrapper Thread thread

③ Use ExecutorService and Callable

④ Future to implement multi-threading with return results

The first two methods have no return value after the thread is executed, and the latter two methods The kind has a return value.

1. Four ways to implement multi-threading

1. Inherit the Thread class to create threads

The Thread class is essentially an instance that implements the Runnable interface, representing a thread. Example. Using the start() instance method of the Thread class is the only way to start a thread. A new thread executing the run() method is started by calling the start() method, which is a native method. It is very simple to implement multi-threading in this way. By directly extending Thread through your own class and overriding the run() method, you can start a new thread and execute your own defined run() method. For example:

public class MyThread extends Thread {  
  public void run() {  
   System.out.println("MyThread.run()");  
  }  
}  
MyThread myThread1 = new MyThread();  
MyThread myThread2 = new MyThread();  
myThread1.start();  
myThread2.start();
Copy after login

2. Implement the Runnable interface to create a thread

If your class has extended another class, you cannot directly extend Thread. At this time, you can implement a Runnable interface, as follows:

public class MyThread extends OtherClass implements Runnable {  
  public void run() {  
   System.out.println("MyThread.run()");  
  }  
}
Copy after login

In order to start MyThread, you need to instantiate a Thread first and pass in your own MyThread instance:

MyThread myThread = new MyThread();  
Thread thread = new Thread(myThread);  
thread.start();
Copy after login

In fact, when a Runnable target parameter is passed to Thread, Thread's run( ) method will call target.run(), refer to the JDK source code:

public void run() {  
  if (target != null) {  
   target.run();  
  }  
}
Copy after login

3. Implement the Callable interface

Create a Thread thread through the FutureTask wrapper

Callable interface (There is only one method) defined as follows:

public interface Callable<V>   { 
  V call() throws Exception;   } 
public class SomeCallable<V> extends OtherClass implements Callable<V> {
    @Override
    public V call() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
}
Copy after login
Callable<V> oneCallable = new SomeCallable<V>();   
//由Callable<Integer>创建一个FutureTask<Integer>对象:   
FutureTask<V> oneTask = new FutureTask<V>(oneCallable);   
//注释:FutureTask<Integer>是一个包装器,它通过接受Callable<Integer>来创建,它同时实现了Future和Runnable接口。 
  //由FutureTask<Integer>创建一个Thread对象:   
Thread oneThread = new Thread(oneTask);   
oneThread.start();   //至此,一个线程就创建完成了。
Copy after login

4. Implement a thread that returns a result

Use ExecutorService, Callable, and Future to implement a thread that returns a result

ExecutorService, The three interfaces Callable and Future actually belong to the Executor framework. In JDK1.5, threads that return results are introduced as a new feature, so you no longer need to go through trouble to obtain the return value. And even if you implement it yourself, it may be full of loopholes.

Tasks that can return values ​​must implement the Callable interface. Similarly, tasks that do not return a value must implement the Runnable interface.

After executing the Callable task, you can obtain a Future object. By calling get on the object, you can obtain the Object returned by the Callable task.

Note: The get method is blocking, that is: the thread returns no result, and the get method will wait forever.

Combined with the thread pool interface ExecutorService, the legendary multi-threading with returned results can be realized.

It has been verified under JDK1.5 and there is no problem. You can directly use the multi-threaded test example with returned results provided below. The code is as follows:

import java.util.concurrent.*;  
import java.util.Date;  
import java.util.List;  
import java.util.ArrayList;  
/** 
* 有返回值的线程 
*/  
@SuppressWarnings("unchecked")  
public class Test {  
public static void main(String[] args) throws ExecutionException,  
    InterruptedException {  
   System.out.println("----程序开始运行----");  
   Date date1 = new Date();  
   int taskSize = 5;  
   // 创建一个线程池  
   ExecutorService pool = Executors.newFixedThreadPool(taskSize);  
   // 创建多个有返回值的任务  
   List<Future> list = new ArrayList<Future>();  
   for (int i = 0; i < taskSize; i++) {  
    Callable c = new MyCallable(i + " ");  
    // 执行任务并获取Future对象  
    Future f = pool.submit(c);  
    // System.out.println(">>>" + f.get().toString());  
    list.add(f);  
   }  
   // 关闭线程池  
   pool.shutdown();  
   // 获取所有并发任务的运行结果  
   for (Future f : list) {  
    // 从Future对象上获取任务的返回值,并输出到控制台  
    System.out.println(">>>" + f.get().toString());  
   }  
   Date date2 = new Date();  
   System.out.println("----程序结束运行----,程序运行时间【"  
     + (date2.getTime() - date1.getTime()) + "毫秒】");  
}  
}  
class MyCallable implements Callable<Object> {  
private String taskNum;  
MyCallable(String taskNum) {  
   this.taskNum = taskNum;  
}  
public Object call() throws Exception {  
   System.out.println(">>>" + taskNum + "任务启动");  
   Date dateTmp1 = new Date();  
   Thread.sleep(1000);  
   Date dateTmp2 = new Date();  
   long time = dateTmp2.getTime() - dateTmp1.getTime();  
   System.out.println(">>>" + taskNum + "任务终止");  
   return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";  
}  
}
Copy after login

1. The difference between Runnable and Callable

The main difference is that the run method of the Runnable interface has no return value;

The call method of the Callable interface has a return value and supports the generic Runnable interface. The run method can only throw runtime exceptions and cannot be captured and processed;

The call method of the Callable interface allows exceptions to be thrown and exception information can be obtained

2. How to start a new thread and the difference between calling the start and run methods

The thread object calls the run method without starting the thread. Only object calls methods.

The thread object calls start to open the thread, and lets the jvm call the run method to execute in the opened thread. Calling the start method can start the thread and make the thread enter the ready state, and the run method is just a normal method of thread, or Executed in the main thread.

The basic methods related to threads include wait, notify, notifyAll, sleep, join, yield, etc.

Thread wait (wait) calls this The thread of the method enters the waiting state and will only return if it waits for notification from another thread or is interrupted. It should be noted that after calling the wait() method, the object's lock will be released. Therefore, the wait method is generally used in synchronized methods or synchronized code blocks.

Thread sleep (sleep) sleep causes the current thread to sleep. Unlike the wait method, sleep will not release the currently occupied lock. sleep(long) will cause the thread to enter the TIMED-WATING state, and the wait() method Will cause the current thread to enter the WATING state.

Thread yield (yield) yield will cause the current thread to yield the CPU execution time slice and re-compete with other threads for the CPU time slice. Generally speaking, threads with high priority have a greater chance of successfully competing for CPU time slices, but this is not absolute. Some operating systems are not sensitive to thread priority.

Thread interrupt (interrupt) Interrupting a thread is intended to give the thread a notification signal, which will affect an interrupt flag inside the thread. This thread itself will not change the state (such as blocking, termination, etc.) because of this

Join waits for other threads to terminate the join() method, waits for other threads to terminate, and calls the join() method of a thread in the current thread. , then the current thread changes to the blocking state and returns to another thread to end. The current thread changes from the blocking state to the ready state again, waiting for the favor of the CPU.

Thread wake-up (notify) The notify() method in the Object class wakes up a single thread waiting on this object monitor. If all threads are waiting on this object, one of the threads will be chosen to wake up. The choice is arbitrary. and occurs when an implementation decision is made, the thread waits on the object's monitor by calling one of the wait() methods until the current thread relinquishes the lock on this object before it can continue executing the awakened thread. The awakened thread will compete in the normal manner with all other threads actively synchronizing on the object. Another similar method is notifyAll(), which wakes up all threads waiting on the same monitor.

4. The difference between wait() and sleep()

① From different classes wait(): from Object class; sleep(): from Thread class;

② Regarding the release of locks: wait(): the lock will be released during the waiting process; sleep(): the lock will not be released during the waiting process

③ Scope of use: wait(): must be synchronized Used in code blocks; sleep(): can be used anywhere;

④ Whether to catch exceptions wait(): No need to catch exceptions; sleep(): Need to catch exceptions;

5 .Multi-threading principle

Multi-threading principle: Multi-threading is performed in a concurrent manner. For a CPU, it can only execute one program at a certain point in time, that is, it can only run one process at the same time. The CPU will continuously switch between these processes, and each thread will execute for one time. Because the execution speed of the CPU is too fast relative to our perception, although the CPU rotates execution between multiple processes, we feel as if multiple processes are executing at the same time.

The CPU will switch between multiple processes. If we open too many programs, the time it takes for the CPU to switch to each process will also become longer, and we will also feel that the machine is running slower. Although reasonable use of multi-threading can improve efficiency, excessive use does not bring about efficiency improvements.

Multi-threading technology mainly solves the problem of multiple thread execution in the processor unit. It can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit.

What are the ways to implement multithreading in Java?

The above is the detailed content of What are the ways to implement multithreading in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles