package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.Properties;
public class TestThreadPool { private static int produceTaskSleepTime ;
private static int produceTaskMaxNumber ;
public static void main(String[] args) { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy()); for (int i = 1; i <= p.getProperty(produceTaskMaxNumber); i++) {
try {
String task = "task@ " + i;
System.out.println("创建任务并提交到线程池中:" + task);
threadPool.execute(new ThreadPoolTask(task)); Thread.sleep(p.getProperty.(produceTaskSleepTime));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.Properties;
public class TestThreadPool { private static int produceTaskSleepTime ;
private static int produceTaskMaxNumber ;
public static void main(String[] args) { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.DiscardOldestPolicy()); for (int i = 1; i <= p.getProperty(produceTaskMaxNumber); i++) {
try {
String task = "task@ " + i;
System.out.println("创建任务并提交到线程池中:" + task);
threadPool.execute(new ThreadPoolTask(task)); Thread.sleep(p.getProperty.(produceTaskSleepTime));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Cofig.propreties为produceTaskSleepTime = 2;
produceTaskMaxNumber = 10;
i <= p.getProperty(produceTaskMaxNumber) ->
i <= Integer.valueOf(p.getProperty(produceTaskMaxNumber))Thread.sleep(p.getProperty.(produceTaskSleepTime)); ->
Thread.sleep(Integer.valueOf(p.getProperty.(produceTaskSleepTime)));
produceTaskMaxNumber = 10;后面的分号去掉