private Logger logger = LoggerFactory.getLogger(testThread.class);
@Test
public void testrun(){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
logger.debug(“666666*”);
Thread.sleep(101000);
logger.debug("88888888");
}catch (Exception E){
E.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
logger.debug(“999999999*”);
}catch (Exception E){
E.printStackTrace();
}
}
Thread.currentThread().join();//多线程需要加入这句话,加入另一个线程
杀死线程
用thread.interrupt()
或ThreadPoolExecutor.shutdown()
//前端传数据,后端接受需要把数据转换为list集合复制为字符串类型
import com.alibaba.fastjson.JSONObject;jar包
import lombok.Data;安装lombokzu组件
idea(file–setting–plugins–选择marketplace搜索lombok)
@Data//这个注解自动提供get/set方法,无需提供
public List getTableList() {
return tableList;
}
public void setTableList(List tableList) {
this.tableList = tableList;
}
public void setTableList(String tableList) {
List list= JSONObject.parseArray(tableList,SynchronizationTableNoJobID.class);
this.tableList = list;
}
//排序
private static void ListSort(List list) {
Collections.sort(list, new Comparator() {
@Override
public int compare(OutpatientExpenditureV o1, OutpatientExpenditureV o2) {
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
try {
if (o1.getBalanceTime()==null||o1.getBalanceTime()==null){
return -1;
}
Date dt1 = format.parse(o1.getBalanceTime());
Date dt2 = format.parse(o2.getBalanceTime());
if (dt1.getTime() > dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
public static T matchObject(List list, T value) {
if(list == null || list.size() == 0 || value == null){
return null;
}
for(T t : list) {
if(value.equals(t)){
return t;
}
}
return null;
}
public static <T> T matchObject(List<T> list, Long id) {
if(list == null || list.size() == 0 || id == null){
return null;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
return t;
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
public static <T,K> T matchObject(List<T> list, String fieldName, K id) {
if(list == null || list.size() == 0 || id == null){
return null;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("get" + StringUtil.upperFirstWord(fieldName));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
return t;
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
public static <T,K> List<T> matchBatchObject(List<T> list, String fieldName, List<K> ids) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("get" + StringUtil.upperFirstWord(fieldName));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(K id :ids){
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
break;
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return result;
}
public static <T> List<T> matchBatchObject(List<T> list, List<Long> ids) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(Long id :ids){
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
break;
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return result;
}
public static <T> List<T> matchList(List<T> list, Long id) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || id == null){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
public static <T> List<T> matchList(List<T> list, List<Long> ids) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(Long id : ids){
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return result;
}
public static <T,K> List<T> matchList(List<T> list, String fieldName, K id) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || id == null){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("get" + StringUtil.upperFirstWord(fieldName));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
public static <T,K> List<T> matchList(List<T> list, String fieldName, List<K> ids) {
List<T> result = new ArrayList();
if(list == null || list.size() == 0 || ids == null || ids.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("get" + StringUtil.upperFirstWord(fieldName));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
for(K id : ids){
for(T t : list) {
try {
if(id.equals(method.invoke(t))){
result.add(t);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
return result;
}
public static <T> List<Long> getKeyList(List<T> list) {
List<Long> result = new ArrayList();
if(list == null || list.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("getId");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Set<Long> set = new HashSet<>();
for(T t : list) {
try {
Long id = (Long)method.invoke(t);
if(id != null){
set.add(id);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
for(Long id : set){
result.add(id);
}
return result;
}
public static <T,K> List<K> getKeyList(List<T> list, String fieldName) {
List<K> result = new ArrayList();
if(list == null || list.size() == 0){
return result;
}
Class clazz = list.get(0).getClass();
Method method = null;
try {
method = clazz.getMethod("get" + StringUtil.upperFirstWord(fieldName));
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Set<K> set = new HashSet<>();
for(T t : list) {
try {
K k = (K)method.invoke(t);
if(k != null){
set.add(k);
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
for(K k : set){
result.add(k);
}
return result;
}
排序
public static <T,K> List<T> tranferBatch(List<K> list, Class<T> clazz){
List<T> tList = new ArrayList<>();
for(K k : list){
T t = new TypeTransferUtil<T>().transfer(k,clazz);
tList.add(t);
}
return tList;
}
合并集合
addall