一个 Java 小程序中的两个线程最佳实践

发布于 2024-12-26 07:28:48 字数 283 浏览 2 评论 0原文

我见过很多这样的例子:

public class AppletName extends Applet implements Runnable {
    // member variables
    // run method and so on...
}

我无法弄清楚的问题是在一个小程序中使用两个线程的最佳方法是什么。我试图在 Google 上找到这个,我找到了 500 页的书籍:(,我需要一些快速的东西。

有人可以给我一些简短的例子或告诉我什么是最佳实践吗?

I have seen many examples like this:

public class AppletName extends Applet implements Runnable {
    // member variables
    // run method and so on...
}

The problem that I cannot figured out is what is the best way to use two threads in one applet. I tried to find this on Google I have found books with 500 pages :(, I need something quick.

Can someone give me some short example or tell me what is the best practice?

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

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

发布评论

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

评论(4

━╋う一瞬間旳綻放 2025-01-02 07:28:48

我见过很多这样的例子

如果它们是基于Applet的,那么它们要么很老,要么是由相对不称职的作者编写的。把它们放回你发现它们的地方。

  1. 使用 Swing 作为组件(例如扩展 JApplet)。
  2. 有关如何使用多线程的更多详细信息,请阅读 Swing 中的并发

I have seen many examples like this

If they are based on Applet, they are either very old or written by relatively incompetent authors. Put them back where you found them.

  1. Use Swing for the components (e.g. extend JApplet).
  2. Read Concurrency in Swing for more details of how to use multiple threads.
哥,最终变帅啦 2025-01-02 07:28:48

我将使用 ExecutorService 或 ScheduledExecutorService 并将任务(例如匿名 Runnable 类)添加到线程池中。您可能会发现即使您有两个或大多数任务,您也只需要一个线程(它只是线程池的配置,您可以稍后更改)

I would use an ExecutorService or a ScheduledExecutorService and add tasks e.g. anonymous Runnable classes to the thread pool. You may find you only need one thread even though you have two or most tasks (its just a configuration of the thread pool which you can change later)

眼泪也成诗 2025-01-02 07:28:48

Java Applet 和线程 的示例。简短而简单。带有示例代码。

如果您有一个用于并发处理的任务池,请考虑 ExecutorService 类。

对于最佳实践,有更短的(<500 页:) 和最权威的指南 在这里。第 07 - 11 章与多线程代码相关。 (只是最佳实践和典型错误的列表)

An example of Java Applets and Threads. Short and simple. With example code.

If you have a pool of tasks for concurrent processing consider ExecutorService class.

For best practices there is a more short (<500 pages :) and the most definitive guides is here. Chapters 07 - 11 related to multithreaded code. (just a list of best practices and typical mistakes)

错爱 2025-01-02 07:28:48

完善响应迅速的 Applet 和 Java GUI 应用程序需要一段时间。

你必须明白,扩展一个线程是可以的,但你用它做什么很重要。

有很多框架可供使用,如果您需要快速的东西,那么扩展线程就可以了。

您可能需要从设计角度问自己:

  1. 您的应用程序是否有多个页面/框架?

  2. 经常更新吗? (画太多,或者只是进度条多)

  3. 是否经常需要用户输入(例如游戏控件)

如果您全部回答“是”,则考虑换行为一个线程,最好在显示线程之前启动主进程(后台)线程。

再说一次,我不知道你想实现什么目标,也许可以启发我们。

It takes a while to perfect Applet and Java GUI apps that are very responsive.

You must understand that its ok to extend a thread, but what you do with it is important.

There are many frameworks to utilize, if you need something fast, then extending a thread is fine.

You may need to ask yourself from a design perspective:

  1. Does you app have a multiple pages / frames?

  2. Does it updates often? (excessive paintings, or just many progress bars)

  3. Does it require user input often (eg game controls)

If you answer Yes to all, then consider wrapping to a thread, its a good practice to start the main process (background) thread before the display thread.

again, I have no idea what you would like to achieve, perhaps enlightening us.

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