使用 Java 进行异步编程

发布于 2024-08-22 05:44:07 字数 281 浏览 6 评论 0原文

在哪里可以找到使用 Java 的异步编程示例?我感兴趣的是在异步编程中寻找模式来构建具有响应能力的应用程序(防止应用程序定期挂起并停止响应用户输入以及服务器应用程序不及时响应客户端请求)和可扩展性。

尤其是,查看执行 I/O 操作(例如文件读/写、Web 请求和数据库查询)并且还涉及大量 CPU 处理(如网页中的购物建议)的示例将会很有帮助。

哪些 Java 库可以帮助确定应用程序的响应能力何时不可预测 - 因为应用程序的线程执行 I/O 请求,所以应用程序基本上放弃了对 I/O 设备(硬盘驱动器、网络或其他)

Where can I find asynchronous programming example using Java? I'm interested in finding patterns in asynchronous programming for building applications that present responsiveness (preventing applications that periodically hang and stop responding to user input and server applications that do not respond to client requests in a timely fashion) and scalability.

In particularly it will be helpful to see a sample which performs I/O operations (such as file reads/writes, Web requests, and database queries) and also has a lot of CPU processing involved like a shopping suggester in a webpage.

Which are the Java libraries which can help in determining when an application's responsiveness is unpredictable - because the application's thread performs I/O requests, the application is basically giving up control of the thread's processing to the I/O device (a hard drive, a network, or whatever)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏爱你一生 2024-08-29 05:44:07

在 GUI 中,您可以使用线程来执行后台任务。

Java 在新的 I/O API (NIO) 中支持非阻塞 I/O。

如果您的问题更面向架构,这本书提供了对异步模式的深入讨论:企业应用程序架构的模式,作者:Martin Fowler。

In a GUI, you could use threads to perform background tasks.

Java supports non blocking I/O in the new I/O API (NIO).

If your question is more architecturally oriented, this book offers an in-depth discussion of asynchronous patterns: Patterns of Enterprise Application Architecture, by Martin Fowler.

香橙ぽ 2024-08-29 05:44:07

有关执行异步操作(重点是对文件进行非阻塞 IO)的示例,您可以在此处查看一些示例:https:// github.com/javasync/idioms(免责声明我是作者)。

我在介绍 Java 异步编程时使用了这些示例,并探索了基于回调的 CompletableFuture 以及最终的反应流。

For examples performing asynchronous operations with emphasis on non-blocking IO on files, you may check some samples here: https://github.com/javasync/idioms (disclaimer I am the author).

I use this samples in introduction to asynchronous programming in Java and we explore callback based, CompletableFuture and finally reactive streams.

爱情眠于流年 2024-08-29 05:44:07

哪些 Java 库可以帮助确定应用程序的响应能力何时不可预测 - 因为应用程序的线程执行 I/O 请求,所以应用程序基本上放弃了对 I/O 设备的线程处理的控制(硬驱动器、网络或其他)

如果我理解正确的话,您需要一些库来检查其他线程以确定它们是否在 I/O 调用中被阻塞。

我认为这在标准 JVM 中是不可能的。此外,我认为这不一定足以保证“响应能力”。

Which are the Java libraries which can help in determining when an application's responsiveness is unpredictable - because the application's thread performs I/O requests, the application is basically giving up control of the thread's processing to the I/O device (a hard drive, a network, or whatever)

If I understand you correctly, you are asking for some library that examines other threads to determine if they are blocked in I/O calls.

I don't think that this is possible in a standard JVM. Furthermore, I don't think that this would necessarily be sufficient to guarantee "responsiveness".

你穿错了嫁妆 2024-08-29 05:44:07

如果您正在使用某种 I/O 操作(例如在 InputStream 上读取,它可能会阻塞),您可以将其放入线程中,最简单的解决方案是在线程上使用给定数量的 join:

MyThread myThread = new MyThread(); 
myThread.start();
myThread.join(10000);

然后此 join 将等待最多10秒。在那之后你就可以忽略该线程,...
您还可以使用装饰器模式。您可以在此处阅读更多内容

If you are using some kind of I/O operation (for example read on InputStream, which can block) you put it into a thread and the simplest solution is to use join on the thread for a given amount:

MyThread myThread = new MyThread(); 
myThread.start();
myThread.join(10000);

This join will then wait for atmost 10 seconds. After that time you can just ignore the thread, ...
You can also use the Decorator pattern. You can read more here.

墨洒年华 2024-08-29 05:44:07

在 Web 环境中,您可以利用新的 j2ee6 异步功能。
看看
http://docs.oracle.com/javaee/6/tutorial/doc /gkkqg.html

in a web environment, you can make use of the new j2ee6 Asynchronous feature.
take a look at
http://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文