Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeNLPUtil增加多种调用方式,比如不抛出异常,限制时间内完成和使用线程池等方式。 #83

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 114 additions & 29 deletions src/main/java/com/xkzhangsan/time/nlp/TimeNLPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.xkzhangsan.time.constants.Constant;
import com.xkzhangsan.time.formatter.DateTimeFormatterUtil;
import com.xkzhangsan.time.utils.CollectionUtil;
import com.xkzhangsan.time.utils.GlobalThreadPool;
import com.xkzhangsan.time.utils.StringUtil;

/**
Expand All @@ -18,7 +21,7 @@
* 包括功能: <br>
*(1)以当前时间为基础分析时间自然语言。 <br>
*(2)以指定时间为基础分析时间自然语言。 <br>
*(3)不抛出异常并且限制时间内完成功能。<br>
*(3)增加多种调用方式,比如不抛出异常,限制时间内完成和使用线程池等方式。<br>
*
* 修改自 https://github.com/shinyke/Time-NLP<br>
* 做了一些修改如下:<br>
Expand Down Expand Up @@ -52,33 +55,71 @@ public static List<TimeNLP> parse(String text){
}

/**
* 时间自然语言分析 不抛出异常,并且限制时间在3s中内完成
* 时间自然语言分析,不抛出异常
* @param text 待分析文本
* @return 结果列表
*/
public static List<TimeNLP> parseSafe(String text){
return parse(text, null, true, 0, false);
}

/**
* 时间自然语言分析,不抛出异常,并且限制时间在指定时间内完成,注意:会创建一个线程异步完成
* @param text 待分析文本
* @param timeout 超时时间 毫秒
* @return 结果列表
*/
public static List<TimeNLP> parseSafe(String text, long timeout){
return parse(text, null, true, timeout, false);
}

/**
* 时间自然语言分析 不抛出异常,并且限制时间在3s中内完成,注意:会创建一个线程异步完成
* @param text 待分析文本
* @return 结果列表
*/
public static List<TimeNLP> parseSafeAndTimeLimit(String text){
return parse(text, null, true, Constant.TIME_NLP_TIMEOUT);
return parse(text, null, true, Constant.TIME_NLP_TIMEOUT, false);
}

/**
* 时间自然语言分析,注意:会创建一个线程异步完成,使用线程池
* @param text 待分析文本
* @return 结果列表
*/
public static List<TimeNLP> parseUseThreadPool(String text){
return parse(text, null, false, 0, true);
}

/**
* 时间自然语言分析,不抛出异常,注意:会创建一个线程异步完成,使用线程池
* @param text 待分析文本
* @return 结果列表
*/
public static List<TimeNLP> parseSafeUseThreadPool(String text){
return parse(text, null, true, 0, true);
}

/**
* 时间自然语言分析 不抛出异常,并且限制时间在指定时间内完成
* 时间自然语言分析不抛出异常,并且限制时间在指定时间内完成,注意:会创建一个线程异步完成,使用线程池
* @param text 待分析文本
* @param timeout 超时时间 毫秒
* @return 结果列表
*/
public static List<TimeNLP> parseSafe(String text, long timeout){
return parse(text, null, true, timeout);
public static List<TimeNLP> parseSafeUseThreadPool(String text, long timeout){
return parse(text, null, true, timeout, true);
}

/**
* 时间自然语言分析 不抛出异常
* 时间自然语言分析不抛出异常,并且限制时间在3s内完成,注意:会创建一个线程异步完成,使用线程池
* @param text 待分析文本
* @return 结果列表
*/
public static List<TimeNLP> parseSafe(String text){
return parse(text, null, true, 0);
public static List<TimeNLP> parseSafeAndTimeLimitUseThreadPool(String text){
return parse(text, null, true, Constant.TIME_NLP_TIMEOUT, true);
}


/**
* 时间自然语言分析
* @param text 待分析文本
Expand Down Expand Up @@ -131,38 +172,82 @@ public static List<TimeNLP> parse(String text, String timeBase){
* @param timeBase 指定时间
* @param isSafe true 不抛出异常,false 抛出异常
* @param timeout 超时时间
* @param useThreadPool 是否使用线程池
* @return 结果列表
*/
public static List<TimeNLP> parse(String text, String timeBase, boolean isSafe, long timeout){
if(isSafe && timeout > 0){
try{
public static List<TimeNLP> parse(String text, String timeBase, boolean isSafe, long timeout,
boolean useThreadPool) {
List<TimeNLP> result = null;
if (StringUtil.isEmpty(text)) {
return result;
}

if (!isSafe && timeout <= 0 && !useThreadPool) {
result = parse(text, timeBase);
}

if (isSafe && timeout > 0 && useThreadPool) {
try {
result = GlobalThreadPool.getGlobalThreadPool().submit(new TimeNLPCallable(text, null)).get(timeout,
TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
} else if (isSafe && timeout > 0) {
try {
TimeNLPCallable timeNLPCallable = new TimeNLPCallable(text, timeBase);
FutureTask<List<TimeNLP>> futureTask = new FutureTask<>(timeNLPCallable);
new Thread(futureTask).start();
return futureTask.get(timeout, TimeUnit.MILLISECONDS);
}catch(Exception e){
result = futureTask.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}else if(isSafe){
try{
return parse(text, timeBase);
}catch(Exception e){
} else if (isSafe && useThreadPool) {
try {
result = GlobalThreadPool.getGlobalThreadPool().submit(new TimeNLPCallable(text, null)).get();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}else if(timeout > 0){
try{
TimeNLPCallable timeNLPCallable = new TimeNLPCallable(text, timeBase);
FutureTask<List<TimeNLP>> futureTask = new FutureTask<>(timeNLPCallable);
new Thread(futureTask).start();
return futureTask.get(timeout, TimeUnit.MILLISECONDS);
}catch(Exception e){
} else if (timeout > 0 && useThreadPool) {
try {
result = GlobalThreadPool.getGlobalThreadPool().submit(new TimeNLPCallable(text, null)).get(timeout,
TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
} else if (isSafe) {
try {
result = parse(text, timeBase);
} catch (Exception e) {
e.printStackTrace();
}
} else if (timeout > 0) {
TimeNLPCallable timeNLPCallable = new TimeNLPCallable(text, timeBase);
FutureTask<List<TimeNLP>> futureTask = new FutureTask<>(timeNLPCallable);
new Thread(futureTask).start();
try {
result = futureTask.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
} else if (useThreadPool) {
try {
result = GlobalThreadPool.getGlobalThreadPool().submit(new TimeNLPCallable(text, null)).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
return parse(text, timeBase);
return result;
}

}
44 changes: 44 additions & 0 deletions src/main/java/com/xkzhangsan/time/utils/GlobalThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.xkzhangsan.time.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* 公共线程池(单例),核心线程为0,最大线程为cpu+1,存活60s,使用同步队列和使用CallerRunsPolicy拒绝策略的线程池。
*
* @author xkzhangsan
*/
public class GlobalThreadPool {

private static volatile GlobalThreadPool globalThreadPool;

private ExecutorService executorService;

private GlobalThreadPool(){
this.executorService = new ThreadPoolExecutor(0, Runtime.getRuntime().availableProcessors() + 1, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
}

public static GlobalThreadPool getGlobalThreadPool() {
if(globalThreadPool == null){
synchronized(GlobalThreadPool.class){
if(globalThreadPool == null){
globalThreadPool = new GlobalThreadPool();
}
}
}
return globalThreadPool;
}

public <T> Future<T> submit(Callable<T> task){
return executorService.submit(task);
}


}
5 changes: 5 additions & 0 deletions src/test/java/com/xkzhangsan/time/test/TimeNLPUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,10 @@ public void parseSafeAndTimeLimitTest() {
System.out.println("下午3点对应时间");
System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList2.get(0).getTime()) + "-"
+ timeNLPList2.get(0).getIsAllDayTime());

List<TimeNLP> timeNLPList3 = TimeNLPUtil.parseUseThreadPool("下午3点对应时间");
System.out.println("下午3点对应时间");
System.out.println(DateTimeFormatterUtil.formatToDateTimeStr(timeNLPList3.get(0).getTime()) + "-"
+ timeNLPList3.get(0).getIsAllDayTime());
}
}