Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Networking and Multithreading

 
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 ]
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • 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!
 
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!
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!