Advanced Java programming: thread pool and Lambda

Keywords: Lambda Java


Today, I'll show you the use case of multithreading. Hope to help you, please see the following list, thank you!

  1. Use of thread pool

     public static ExecutorService newFixedThreadPool(int nThreads);
    

Get a thread object. The initialization parameter is the required number of threads in the current thread pool

public Future submit(Runnable target);

Get a thread object from the thread pool and execute the given Runnable interface implementation class object as the execution target

Demo code
package com.qfedu.b_executors;

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

class MyThread1 implements Runnable {
	@Override
	public void run() {
		System.out.println("Runnable Interface implementation class, thread object code");
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(Thread.currentThread().getName() + " Have potato and beef brisket tomorrow");
	}
}

public class Demo1 {
	public static void main(String[] args) {
		// 1. Create thread pool object
		ExecutorService service = Executors.newFixedThreadPool(5);
		
		// 2. Create a MyThread1 Runnable interface implementation class object
		MyThread1 target = new MyThread1();
	
	// 3. Use a thread in the thread pool object to specify the target code
	// The number of initialization threads is 5. Here, we use 5 existing threads in the thread pool to execute the code
	service.submit(target);
	service.submit(target);
	service.submit(target);
	service.submit(target);
	service.submit(target);
	
	// Because the original 5 threads are all in use, you need to wait for 5 threads to finish executing and idle threads appear
	// To execute the corresponding target code
	service.submit(target);
	service.submit(target);
	
	// 4. Close thread pool
	// In general, the thread pool does not need to be closed. It will be closed as the program exits
	// service.shutdown();
}

}

  1. Lambda expression
    2.1 Lambda expression ideas

     service.submit(new Runnable() {                                          
     	@Override                                                
     	public void run() {                                      
     		System.out.println(Thread.currentThread().getName());
     	}                                                        
     });                                                          
    

Anonymous inner class method to execute target code as thread pool

1. The parameter required for this method is the implementation class object of the Runnable interface
 2. The goal of runnable interface is to provide a run method. What will be run
 3. What will be run??? where??? run method content

Here, Runnable is required to provide the Run method and the Run method body

"Say the point"
Need Run method body

2.2 Lambda expression format

service.submit(() -> 
System.out.println(Thread.currentThread().getName())); 
( ) -> 
System.out.println(Thread.currentThread().getName())

Lambda expression

() parameter list

->What to do is the corresponding method body
 The code after the arrow is the normal statement

(parameter list) - > {code statement}

2.3 premise of lambda expression
There is only one interface whose default property is public abstract method, such as Comparator interface and Runnable interface

There is a pre and post constraint when using lambda expressions
 Method is of interface type,
Or the local variable uses the calling method, and the parameters of the method are interface types.
It's OK to use lambda

Fun face book

43 original articles published, praised 29, visited 10000+
Private letter follow

Posted by robwilson on Wed, 04 Mar 2020 00:25:58 -0800