睡眠1s
public static void main ( String[ ] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor ( 1 ,
1 , 10 ,
TimeUnit. SECONDS,
new ArrayBlockingQueue < > ( 1 ) ,
new ThreadPoolExecutor. DiscardOldestPolicy ( ) ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
int finalI = i;
executor. execute ( ( ) - > {
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( finalI) ;
} ) ;
}
executor. shutdown ( ) ;
}
修改时间
public static void main ( String[ ] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor ( 1 ,
1 , 10 ,
TimeUnit. SECONDS,
new ArrayBlockingQueue < > ( 1 ) ,
new ThreadPoolExecutor. DiscardOldestPolicy ( ) ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
int finalI = i;
executor. execute ( ( ) - > {
System. out. println ( finalI) ;
} ) ;
}
executor. shutdown ( ) ;
}
}
修改任务数量
public static void main ( String[ ] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor ( 1 ,
1 , 10 ,
TimeUnit. SECONDS,
new ArrayBlockingQueue < > ( 1 ) ,
new ThreadPoolExecutor. DiscardOldestPolicy ( ) ) ;
for ( int i = 0 ; i < 100 ; i++ ) {
int finalI = i;
executor. execute ( ( ) - > {
System. out. println ( finalI) ;
} ) ;
}
executor. shutdown ( ) ;
}