public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(1),new ThreadPoolExecutor.AbortPolicy());
for(int i=0;i<5;i++){
final int j=i;
Thread.sleep(1000);
executor.execute(new Thread(){
@Override
public void run(){
try {
//此处业务逻辑执行代码
System.out.println(j);
Thread.sleep(1000*10);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(1),new ThreadPoolExecutor.CallerRunsPolicy());
for(int i=0;i<5;i++){
final int j=i;
Thread.sleep(1000);
executor.execute(new Thread(){
@Override
public void run(){
try {
//此处业务逻辑执行代码
if(j>=3){
System.out.println("主线程在执行...");
}else{
System.out.println(j);
Thread.sleep(1000*10);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(1),new ThreadPoolExecutor.DiscardPolicy());
for(int i=0;i<5;i++){
final int j=i;
Thread.sleep(1000);
executor.execute(new Thread(){
@Override
public void run(){
try {
//此处业务逻辑执行代码
System.out.println(j);
Thread.sleep(1000*10);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1,2, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(1),new ThreadPoolExecutor.DiscardOldestPolicy());
for(int i=0;i<5;i++){
final int j=i;
Thread.sleep(1000);
executor.execute(new Thread(){
@Override
public void run(){
try {
//此处业务逻辑执行代码
System.out.println(j);
Thread.sleep(1000*10);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
}