Home  >  Article  >  Java  >  Java multi-threading implementation principles and related knowledge points

Java multi-threading implementation principles and related knowledge points

WBOY
WBOYforward
2023-05-06 22:01:061065browse

1. Thread pool principle in multi-threading

(1) Determine whether the core threads in the thread pool are all executing tasks, if not (the core threads are idle, or core thread is useless), create a new worker thread to perform the task. If all core threads are executing tasks, enter the next process.

(2) The thread pool determines whether the work queue is full. If the work queue is not full, newly submitted tasks are stored in this work queue. If the work queue is full, proceed to the next process.

(3) Determine whether the thread in the thread pool is processing the working status. If not, create a new working thread to perform the task. If it is full, let the saturation strategy handle this task.

2. Multi-threaded example

public class Test implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    static ExecutorService service = newFixedThreadPool(3);
 
    public static void main(String[] args) {
        for (int i=0;i<100;i++) {
            service.execute(new Test());
        }
 
        service.shutdown();
    }
}

The above is the detailed content of Java multi-threading implementation principles and related knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete