Home > Java > Java Tutorial > body text

Java multi-threading implementation principles and related knowledge points

WBOY
Release: 2023-05-06 22:01:06
forward
945 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:yisu.com
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!