java 一个方法里面的 所有方法并行执行 用线程
package com. example. springbootycw. schedule; import java. util. Arrays;
import java. util. HashSet;
import java. util. List;
import java. util. Set;
import java. util. concurrent. ExecutorService;
import java. util. concurrent. Executors; public class ParallelExecutionExample { public static void main ( String[ ] args) { ExecutorService executor = Executors. newFixedThreadPool ( 5 ) ; String content = "" ; List< String> monitorTypeList = Arrays. asList ( ) ; Set< String> missingIds = new HashSet< > ( monitorTypeList) ; for ( String missingId : missingIds) { if ( missingId. equals ( "A" ) ) { executor. submit ( ( ) -> { for ( int i = 0 ; i < 10 ; i++ ) { System. out. println ( "A定时任务: " + i) ; try { Thread. sleep ( 1000 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; } } } ) ; } if ( missingId. equals ( "B" ) ) { executor. submit ( ( ) -> { for ( int i = 0 ; i < 10 ; i++ ) { System. out. println ( "B定时任务: " + i) ; try { Thread. sleep ( 1000 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; } } } ) ; } if ( missingId. equals ( "C" ) ) { executor. submit ( ( ) -> { for ( int i = 0 ; i < 10 ; i++ ) { System. out. println ( "C定时任务: " + i) ; try { Thread. sleep ( 1000 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; } } } ) ; } if ( missingId. equals ( "D" ) ) { executor. submit ( ( ) -> { for ( int i = 0 ; i < 10 ; i++ ) { System. out. println ( "D定时任务: " + i) ; try { Thread. sleep ( 1000 ) ; } catch ( InterruptedException e) { e. printStackTrace ( ) ; } } } ) ; } } executor. shutdown ( ) ; }
}