• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to figure out how a server class with threads works.

 
E Kirskon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. I am relatively new to java and programming in general. It's my first post in the forum and I hope my question is not too big.

I have the code for a server/client app which is supposed to work with two users. I am aware of the basic concepts for threads, constuctors, etc, but I can seem to put it all together and understand how it works exactly.

First, in the constructor here two threads are created and started. I am not sure what exactly should the "this" should represent in this case :
thrOne = new Thread(this);

Is it the construcotr passing itself to the newly created thread? If, yes how does it work? Is there another way?
Second thing is the case with the two threads used in this Server class. I guess, that threads are two since the users are supposed to be two as well. But is it a good idea to be done like that? And how does the server assign the different threads to different users. I am lost.

Sorry again if my question is too big.
Best regards.


 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You pass a Runnable to a Thread constructor and then call the Thread's start() method. What you are doing is passing the instance of the class you happen to be in to that Thread constructor. That is what the keyword this (all on its own, without dots or (brackets) means.
Then you are checking for input from the Socket, and you have some non‑object‑oriented features, like checking which thread is obtaining a particular input. I am sure there are ways of passing a different buffered reader to each thread.

This question is actually too hard for this forum, so I shall move it elsewhere.

By the way: where did that code come from?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!