问题背景:
目前需要解析十多个Excel文件并将数据导入到数据库,文件导出完成之后还有后续步骤需要处理,例如更新批次状态。
如果采用单线程的方式顺序去处理每个文件的话,感觉有点慢,虽说不算是特别慢,但是由于用户需要等待导入结果,
考虑到用于体验问题,因此需要坐下性能优化。既然是性能优化,基本都是考虑到使用多线程并发处理。但是多线线程有个问题,
就是启动多个线程后,如何让当前线程等待这些子线程执行完毕,因为需要根据所以子线程的执行结果来更新批次状态
解决方案1:
基本思路是这样:
每个SubThread子线程类实例有个自己的状态99-初始化 0-执行成功 1-执行失败,当执行完毕之后,将状态修改为0或者1
MainThread主线程类中有个List,用来登记所有子线程。子线程的创建通过主线程来创建,每次创建之后,都会将子线程添加到List中。
所有子线程创建完成之后,通过主线程的start方法启动所有子线程,并通过一个while循环来遍历List中的所有子线程的状态,
判断是否存在状态为99的,如果没有,则便是全部子线程执行完毕
/**
* 子线程类
* @author Administrator
*
*/
public class SubThread implements Runnable{
private int status = 99; //99-初始化 0-执行成功 1-执行失败
public void run() {
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
} |
/**
* 主线程类
* @author Administrator
*
*/
public class MainThread {
private List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @return
*/
public SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(new SubThread());
return subThread;
}
public boolean start(){
for(SubThread subThread : subThreadList){
new Thread(subThread).start();
}
/**
* 监控所有子线程是否执行完毕
*/
boolean continueFlag = true;
while(continueFlag){
for(SubThread subThread : subThreadList){
if(subThread.getStatus()==99){
continueFlag = true;
break;
}
continueFlag = false;
}
}
/**
* 判断子线程的执行结果
*/
boolean result = true;
for(SubThread subThread : subThreadList){
if(subThread.getStatus()!=0){
result = false;
break;
}
}
return result;
}
|
测试代码:
public static void main(String[] args) {
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
boolean result = main.start();
System.out.println(result);
}
|
解决方案2:
通过计数器方式解决。基本思路如下:
计数器类CountLauncher负责记录正在执行的子线程的总数,所有的子线程共享该计数器类对象,当子线程执行完毕之后,调用计数器的counDown()方法进行计数器减1.
主线程通过计数器类来判断是否所有子线程都执行完毕。
/**
* 计数器类
* @author Administrator
*
*/
public class CountLauncher {
private int count;
public CountLauncher(int count){
this.count = count;
}
public synchronized void countDown(){
count --;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
} |
/**
* 子线程类
* @author Administrator
*
*/
public class SubThread implements Runnable{
/**
* 计数器类对象实例
*/
private CountLauncher countLauncher;
private int status = 99; //99-初始化 0-执行成功 1-执行失败
public void run() {
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
countLauncher.countDown();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public CountLauncher getCountLauncher() {
return countLauncher;
}
public void setCountLauncher(CountLauncher countLauncher) {
this.countLauncher = countLauncher;
}
|
/**
* 主线程类
* @author Administrator
*
*/
public class MainThread {
private List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @return
*/
public synchronized SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(new SubThread());
return subThread;
}
public boolean start(){
CountLauncher countLauncher = new CountLauncher(subThreadList.size());
for(SubThread subThread : subThreadList){
subThread.setCountLauncher(countLauncher);
new Thread(subThread).start();
}
while(countLauncher.getCount()>0){
System.out.println(countLauncher.getCount());
}
/**
* 判断子线程的执行结果
*/
boolean result = true;
for(SubThread subThread : subThreadList){
if(subThread.getStatus()!=0){
result = false;
break;
}
}
return result;
}
/**
* 测试实例
*/
public static void main(String[] args) {
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
boolean result = main.start();
System.out.println(result);
}
|
解决方案3(推荐):
该方法的处理方式同解决方案2 ,只不过使用的是Java自带的计数器类java.util.concurrent.CountDownLatch。
还有一点就是不需要在主线程中通过while来监控所有子线程,是否通过调用它的await方法进行等待所有子线程的执行完毕。
使用计数器时,需要注意的一点是:子线程中调用countDown()方法时一定要放在最后来执行,否则会出现子线程未执行完毕,主线程就开始往下执行了。
因为一定计数器为0,就会自动唤醒主线程的。
/**
* 子线程
* @author Administrator
*
*/
public class SubThread implements Runnable{
private int status =99;
private CountDownLatch threadsSignal;
@Override
public void run(){
System.out.println("开始执行...");
try{
Thread.sleep(2000);
}catch(Exception e){
e.printStackTrace();
}
status=0;
System.out.println("执行完毕...");
threadsSignal.countDown();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public CountDownLatch getThreadsSignal() {
return threadsSignal;
}
public void setThreadsSignal(CountDownLatch threadsSignal) {
this.threadsSignal = threadsSignal;
}
} |
public class MainThread {
public List<SubThread> subThreadList = new ArrayList<SubThread>();
/**
* 创建子线程
* @param clazzPath
* @param methodName
* @return
*/
public SubThread createSubThread(){
SubThread subThread = new SubThread();
subThreadList.add(subThread);
return subThread;
}
/**
* 启动线程
*/
public int start() throws Exception{
CountDownLatch threadSignal = new CountDownLatch(subThreadList.size());
for(SubThread subThread :subThreadList){
subThread.setThreadsSignal(threadSignal);
Thread thread = new Thread(subThread);
thread.start();
}
threadSignal.await();
System.out.println("所有子线程执行完毕...");
for(SubThread subThread :subThreadList){
if(subThread.getStatus()!=0){
return 1;
}
}
return 0;
}
public static void main(String[] args) throws Exception{
MainThread main = new MainThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
main.createSubThread();
int result = main.start();
System.out.println("执行结果:" + result);
}
|