• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads

 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody:
I am workin on a small chat application . I need the clinet to listen for the output from the server and at the same time send msgs to the server at the same time . I knoe i need to start two threads for that . But can anyone tell me how can i do that
can anyone suggest any code segment .
Thank you
Siva
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
when you create a class that implements Runnable and run a thread, there is infact two thread ruuning-one mian thread and the thread you have created. So
[code]
class chat implements Runnable{
Thread sender;
chat()
{
sender=new Thread(this);
sender.start();
}
public void run()
{
while(true)
{
//listen to the server for messages
//and then display in to the user
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
//on clicking send button
public void actionPerformed(ActionEvent ae)
{
//submit the message typed by the user to the server
}
[code]
so the thread sender will check in a loop for messages from the server. If the user clicks on the 'send button' then call this actionPerformed (by addActionListnener ofcourse) to send the message to the server through the main thread
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!