A brief introduction to java multithreading and code examples

Java provides powerful multi-thread support, and multi-thread programming can be realized in a variety of ways. The following are some common Java multithreaded programming methods and tools:

  1. Thread class: Java's basic multi-threaded programming is implemented through the Thread class. Create a subclass of Thread, override the run method, and define the logic of the thread in it. Then start the thread by creating an instance of Thread and calling the start method.
class MyThread extends Thread {
    
    
   public void run() {
    
    
       // 线程的逻辑
   }
}

public class Main {
    
    
   public static void main(String[] args) {
    
    
       MyThread thread = new MyThread();
       thread.start();
   }
}

Runnable interface : In addition to inheriting the Thread class, you can also implement multithreading by implementing the Runnable interface. Create a class that implements the Runnable interface, implement its run method, and define the logic of the thread in it. Then pass the object that implements the Runnable interface to the constructor of the Thread class, and call the start method to start the thread.

class MyRunnable implements Runnable {
    
    
    public void run() {
    
    
        // 线程的逻辑
    }
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        MyRunnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

Callable and Future: In addition to using Runnable, you can also use the Callable interface to define the logic of the thread, and obtain the return result of the thread through the Future interface. Callable can return a result, while Runnable does not return a value.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class MyCallable implements Callable<Integer> {
    
    
    public Integer call() {
    
    
        // 线程的逻辑,并返回一个结果
        return 42;
    }
}

public class Main {
    
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
    
        MyCallable callable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();

        // 获取线程的返回结果
        int result = futureTask.get();
        System.out.println("线程的返回结果:" + result);
    }
}

Executor framework : Java provides the Executor framework to manage and schedule thread execution. It abstracts the thread creation and scheduling process, and provides a more advanced interface for task submission and execution.

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 提交任务给线程池
        executorService.submit(() -> {
    
    
            // 线程的逻辑
        });

        // 关闭线程池
        executorService.shutdown();
    }
}

Java also provides more advanced multithreading tools and technologies such as thread synchronization, locks, condition variables, and concurrent containers, such as the synchronized keyword, Lock interface, CountDownLatch, CyclicBarrier, Semaphore, etc. You can search for it yourself

Guess you like

Origin blog.csdn.net/a203206868/article/details/131518944