Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Networking and Multithreading

 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am beginning to make an outline of my networking approach. I have a question about threads. When a new client in networked mode launches the application and a new thread is spawned, what should be in the run method? Thanks!
[ December 13, 2004: Message edited by: Daniel Simpson ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11944
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

I am not sure that I understand your question, so if I am going off on a tangent, please ignore my ramblings and if you can spot where I have misunderstood you then please correct me.

If you are talking about using the SwingUtilities.invokeLater() to explicitly start a new thread to do the booking, then you probably want your runnable method to contain all the method calls needed to do a booking. You might want some to use some sort of Observable pattern to get information back to the main thread about success / failure of the booking process.

If you are talking about using Sockets to connect to the server, then there are plenty of tutorials showing how to do this. The simplest way to do this is to spawn a new thread as soon as you have a connection, then the runnable method will open your streams and start sending and receiving data on them.

We should not be talking about RMI, since RMI creates it's own threads.

Regards, Andrew
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, Andrew. In my implementation of the book method, it goes, lock-->verify data-->update(book)-->unlock. I guess my question is, if I call the book method from the run method, how do I pass the parameter of the record number into the run method?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11944
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

I still don't know which run method you are talking about, although I am still leaning towards you talking about using the SwingUtilities.runLater() method.

Take a look at the following code:Does this provide you with an answer?

As you can see, I am passing parameters into, and out of, the separate thread that I am creating in order to do the real work.

Regards, Andrew
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new to networking/multithreading so I am sorry for all the confusion. Forgive me if I am wrong, but in the code, wouldn't there need to be a thread creation using Runnable parameter from your inner class and also calling the start() method on that thread?? Also, the run method I was referring to was . Thanks!
 
Omar Kalaldeh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,
From what I have read, I don’t think you are handling client requests within thread pool, if not I think you should consider it, it is not a necessity but handling requests in a thread pool is always better.

Any way, as for how to handle requests at server side, you can set accepted socket client in the constructor of your handling thread (that is if you are not using thread pool).

Your code in very simple way could look like:

Now inside your run method you will read request Object and handle it there.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense! Thanks Omar!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic