使用Swing的Java聊天应用程序(概念) [英] Java chat application using Swing (Conceptual)

查看:89
本文介绍了使用Swing的Java聊天应用程序(概念)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Swing作为界面用Java编写聊天应用程序。

I want to write a chat application in Java using Swing as an interface.

我想出了一个想法(在MadProgrammer ),但我不确定这是否是最佳方式。

I have come up with an idea (with the help of MadProgrammer), but I am not sure whether it is the best way to go about this.

main 主题中有两个 Blockinqueue 队列,一个用于传入消息,另一个用于传出消息。

There are two Blockinqueue queues in the main Thread, one for incoming messages and one for outgoing messages.

有四个线程,两个用于传出,两个用于传入消息,一个用于处理GUI和套接字。

There are four threads, two for outgoing and two for incoming messages, one each to handle the GUI and the socket.

外发邮件的主题


  1. ActionListener(Swing):当用户点击GUI中的发送时触发。线程将新消息添加到传出队列并触发 notifyAll()

  2. socketOutgoing :可以访问套接字。使用 wait()在传出队列中休眠,直到通知为止。通过套接字在Outgoing Queue中发送新消息,然后再次进入休眠状态。

  1. ActionListener (Swing): Is triggered when user clicks "send" in GUI. The thread adds the new message to the Outgoing Queue and triggers notifyAll() on it.
  2. socketOutgoing: Has access to socket. Sleeps, with wait() on Outgoing Queue, until it is notified. Sends new messages in Outgoing Queue through socket, then goes back to sleep again.

传入消息的线程:


  1. socketIncoming:可以访问套接字。在socket中连续检查新的
    消息(如何?)。当有新消息时,将其添加到
    Outgoing Queue并触发 notifyAll()

  2. Swingworker displayIncoming :在传入队列中与 wait()一起睡觉,直到收到通知。在GUI中显示新消息,然后再次进入休眠状态。

  1. socketIncoming: Has access to socket. Checks continuously for new message in socket (how?). When there is a new message, adds it to the Outgoing Queue and triggers notifyAll() on it.
  2. Swingworker displayIncoming: Sleeps, with wait() on Incoming Queue, until it is notified. Displays new messages in GUI, then goes back to sleep again.

虽然这在理论上可行,但似乎有些混乱(且不可靠) )为此有四个线程。

While this would theoretically work, it seems a bit messy (and unreliable) to have four threads for this.

是否有更实用的解决方案?

Is there a more practical solution?

未来读者注意事项:我的 socketIncoming 的描述被误导:无法在套接字中连续检查新邮件。

Note to future readers: My description of socketIncoming was misguided: It is not possible to "check continuously for new message in socket".

你调用 ObjectInputStream#readObject(),并且没有新消息,它只是等待或阻塞,直到新消息到达。无法事先检查新消息是否已到达。 查看此问题

When you call ObjectInputStream#readObject(), and there is no new message, it simply waits, or "blocks", until a new messages arrives. There is no way to check beforehand whether a new message has arrived. See this question.

推荐答案

如果我正在考虑做一些事情,我可能会设置两个队列,即传出队列和传入队列。这些将用于暂存消息。

If I was thinking about doing something like I would probably set up two queues, an outgoing and incoming queue. These would be used to "stage" messages.

这个想法是将传出消息放入传出队列并且 Thread 能够,它将弹出下一条消息并发送它。当队列为空时,它只是等待直到新消息可用。

The idea being that outgoing messages would be placed into the outgoing queue and when Thread was able to, it would pop off the next message and send it. When the queue was empty, it would simply "wait" until a new message become available.

该概念对于传入队列反向工作。 线程将读取一条消息并将其推送到传入队列。

The concept would work in reverse for the incoming queue. The Thread would read a message and push it onto the incoming queue.

其他一些进程(可能是 SwingWorker )将监视队列并弹出下一条消息并将其与GUI重新同步。

Some other process (possibly a SwingWorker) would be monitoring the queue and pop the next message of it and re-sync it with the GUI.

你可能会在某些用途中找到 Swing中的并发

You might find Concurrency in Swing of some use.

虽然

这篇关于使用Swing的Java聊天应用程序(概念)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆