gpt4 book ai didi

java - 如何使用 API java.util.concurrent.Future 而不是在 Java 中显式创建线程?

转载 作者:行者123 更新时间:2023-11-30 05:56:50 24 4
gpt4 key购买 nike

我有两个线程在 java 程序中并行运行,如下所示:

// Threading
new Thread(new Runnable() {
@Override
public void run() {
try {
gpTableCount = getGpTableCount();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
try {
hiveTableCount = getHiveTableCount();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

while(!(gpTableCount != null && gpTableCount.size() > 0 && hiveTableCount != null && hiveTableCount.size() > 0)) {
Thread.sleep(5000);
}
// Threading

两者具有相同的功能。以下是 getHiveTableCount() 的代码。另一种方法与下面的方法略有不同(一两行),但功能保持不变。

public Map<String, String> getHiveTableCount() throws IOException, SQLException {
hiveDataMap = new HashMap<String, String>();
hiveTableErrs = new HashMap<String, String>();
Iterator<String> hiveIterator = filteredList.iterator();
Connection hiveConnection = DbManager.getHiveConnection();
PreparedStatement hive_pstmnt = null;
String hiveExcpnMsg;
String ssn;
String hiveMaxUpdTms;
Long hiveCount;
String gpHiveRec;
String[] hiveArray;
String[] hiveDetails;
String hiveQuery;
while(hiveIterator.hasNext()) {
gpHiveRec = hiveIterator.next();
hiveArray = gpHiveRec.split(",");
hiveDetails = hiveArray[1].split("\\.");
hiveQuery = "select '" + hiveDetails[1] + "' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from " + hiveArray[1] + " where source_system_name='" + hiveArray[2] + "' group by source_system_name";
try {
hive_pstmnt = hiveConnection.prepareStatement(hiveQuery);
ResultSet hiveCountRs = hive_pstmnt.executeQuery();
while(hiveCountRs.next()) {
hiveCount = hiveCountRs.getLong(2);
ssn = hiveCountRs.getString(3);
hiveMaxUpdTms = hiveCountRs.getTimestamp(4).toString();
hiveDataMap.put(hiveDetails[1] + "," + ssn, hiveCount + "," + hiveMaxUpdTms);
}
} catch(org.postgresql.util.PSQLException e) {
hiveExcpnMsg = e.getMessage();
hiveTableErrs.put(hiveDetails[1] + ": for the SSN: " + hiveArray[2], hiveExcpnMsg + "\n");
} catch(SQLException e) {
hiveExcpnMsg = e.getMessage();
hiveTableErrs.put(hiveDetails[1] + ": for the SSN: " + hiveArray[2], hiveExcpnMsg + "\n");
} catch(Exception e) {
hiveExcpnMsg = e.getMessage();
hiveTableErrs.put(hiveDetails[1] + ": for the SSN: " + hiveArray[2], hiveExcpnMsg + "\n");
}
}
return hiveDataMap;
}

这两个线程同时运行。我最近在网上读到:

Future class represents a future result of an asynchronous computation – a result that will eventually appear in the Future after the processing is complete.

我从理论上理解了这个概念,但我不知道如何为上述相同的代码应用 java.util.concurrent.Future api,而不是显式创建线程。谁能告诉我如何使用 java.util.concurrent.Future api 在以下方法上实现多线程: getGpTableCount() & getHiveTableCount 使用 java.util.concurrent.Future api 而不是创建线程创建新线程,如 new Thread(new Runnable( )?

最佳答案

您正在使用Runnable提交您的任务接口(interface)不允许您的线程在计算结束时返回值(并导致您使用共享变量 - gpTableCounthiveTableCount )。

Callable接口(interface)是后来添加的,它允许您的任务返回一个值(在您的例子中, Map<String, String> )。

作为直接使用线程的替代方法,并发 API 引入了 ExecutorService作为管理线程池并能够异步执行任务的更高级别对象。

提交 Callable 类型的任务时到ExecutorService您期望任务产生一个值,但由于提交点和计算结束没有耦合,ExecutorService将返回Future它允许您获取该值,并在该值不可用时阻止。因此,Future可用于在不同线程之间进行同步。

作为ExecutorService的替代品您还可以查看FutureTask<V>这是 RunnableFuture<V> 的实现:

This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation

A FutureTask can be used to wrap a Callable or Runnable object.

关于java - 如何使用 API java.util.concurrent.Future 而不是在 Java 中显式创建线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53030413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com