• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic