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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

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

See all articles