File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Networking and Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Networking and Multithreading" Watch "Networking and Multithreading" New topic
Author

Networking and Multithreading

Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
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 ]

SCJP 1.4<br />SCJD 1.4
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
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

Joined: Mar 28, 2003
Posts: 11404
    
  81

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

Joined: Sep 02, 2004
Posts: 181
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

Joined: Jun 08, 2004
Posts: 58
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.


Best Regards <br /> <br />Omar F. Kalaldah<br />---------------------<br />SCJP, SCJD, SCBCD
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
That makes sense! Thanks Omar!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Networking and Multithreading
 
Similar Threads
Database Selection Question
Someone please help me with the traceroute using jpcap
networking and threading!
WA #1.....word association
How much the book cover for the new networking features of J2SE 5.0 Tiger?