I need to write a basic chat program, and I created the GUI using JFrame. Should I be using a Thread to create the connection, or request chats? Is there an example that I can follow. Basically I am having problems requesting chats with other people, and in a previous thread it was mentioned that I should be using threads.
And by threads, I am assuming its a separate class that is called and running as a separate process?
I have one app where chatting is its secondary responsibility so I spawn a thread off that handles all connectivity and communication with the chat server as I don’'t want any failures to interfere with the primary responsibility of the process.
On the other hand I have another app that absolutely depends on having a connection to the chat server so in this app I don’'t spawn a thread and instead wait for connectivity on the main thread.
As far as a Swing app if you don’'t want to block the event dispatcher thread (thereby making your GUI unresponsive) while you establish a connection you may want to spawn off the establishing of connectivity to a thread.
So the answer really depends on your requirements.
Sorry, i’'ve only used threads once and it was a while ago. How do I access the connection variable initialized in the connectio thread in my chat program? So…after I obtain a connection on my thread class, how do I refer to the Connection class variable created, in my chat program (i.e. for creating chats, etc.?)
The usage of threads would imo be entirely dependent on your use case.
If you were envisaging lots of conversations happening at once then you may want to have each conversation running on a separate thread. If you only expect a couple of conversations happening at once then it may not be worth the extra complexity adding thread support to your application.
You mentioned that you were having problems requesting chats with other people, but you didn’t actually say what these problems were. If you could provide a bit more information I’'ll try and give you some pointers
As someone has said before, using chats would depend on your situation. But from my experience (i wrote a jabber client whose main functionality was to chat).
Create a thread when you connect to the jabber server, login to is and retrieve the roster
It maybe good to create a thread when you are retrieving a VCard
You do not need to creat a thread to create a Chat object and send messages through it
Usually, you will prefer to handle the incoming messages in an asynchronous way, by using listeners, so no thread is needed for the incmoming messages.
/html
To create a thread in your case (this is just an example)