English 中文(简体)
在 Java执行这种守则模式的最佳和最快的方法是什么?
原标题:What is the best and fastest way to execute this pattern of code in Java?

我不得不将以下法典模式移至 Java。 我很想知道,在 Java与任何图书馆或没有:

async def call_external_service(user):  
    async with session.get(url, headers=headers) as response:  
    response = await response.json()  
    # extract some field from the response  
    country = response.json()[ country ]  
    print(country)

async def get_countries(users):  
    tasks = [asyncio.ensure_future(call_external_service(user)) for user in users]  
    await asyncio.gather(*tasks, return_exceptions=True)

users = list()  
with open("users.txt", "r") as f:  
    for line in f.readlines():  
    users.append(line.strip( 
 ))

asyncio.run(get_countries(users))

增 编

最佳回答

在 Java21年及以后,virtual threads,几乎不需要复杂的合成/反应式方案规划。 你们可以有数百万条虚拟的线索,可以直截了当。

Java有HTTP客户类别

Java有方便的有资源 自动关闭资源,如<代码> Executorservice。

Collection < User > users = … ; // Fetch. 
try
(
    ExecutorService es = Executors.newVirtualThreadPerTaskExecutor() ;
)
{
    for ( User : user ) 
    { 
        es.submit( … some task object — `Runnable` or `Callable` … ) ;
    }
}
// Flow-of-control stops here (blocks) until all submitted tasks complete/fail. 

如果你愿意,上述所有法典都可以放在<条码>可兰经<>条码>内,作为在另一条执行器服务中另一条镜子的任务。

更多学习。 这些专题已在Stack Overflow上广泛论述。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签