aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Threading Model 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 "Threading Model" Watch "Threading Model" New topic
Author

Threading Model

Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Hello everybody:
I am having problems on the threading model. I guess how i could discuss the problem best is, how do u implement the threading model on all the methods in the data class. I know I am not being very specific here.
I am thinking to go for a-thread-a-client rather than a pool of threads and I am also aware of how to do it. I am thinking of having the data class implement runnable. I am wondering about how to implement threading model on the find, add, modify methods. Like what could/should the run() method do? I have not written many threading models before and I would appreciate any help.

Thanks
Prakahs
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

thinking of having the data class implement runnable

Nah, you don't want to do that.
If you are using RMI you don't have to even think about threads, they are all handled for you.
If you use Objects Serialized over Sockets, then the only threading you need is the listener thread to hear requests, then your listener engine then fires off a thread for the client, so that further requests go through that new thread.
I'd suggest doing a bit of reading through this forum.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Originally posted by Mark Spritzler:

Nah, you don't want to do that.
If you are using RMI you don't have to even think about threads, they are all handled for you.
If you use Objects Serialized over Sockets, then the only threading you need is the listener thread to hear requests, then your listener engine then fires off a thread for the client, so that further requests go through that new thread.
I'd suggest doing a bit of reading through this forum.
Mark

Mark: Thanks for your reply
"If you are using RMI you don't have to even think about threads, they are all handled for you."
SO then what goes in the run() method. Once I have my class...say ThreadClass thats implements runnable , I can then make it spawn a thread. But how do I associate the methods with the threads. Say for eg. I have 10 threads spawned up. for say 10 clients. How do I associate each of the 10 threads with each of the clients' request.
Any ideas???
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

There is no run method, you are not implementing Runnable or extending Thread.
I think you should read the RMI tutorial at Javasoft.
RMI Tutorial.
Mark
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
I did read the tutorial(both the sun and the Jguru) but did not see any reference saying that the threading is taken care by itself.
Maybe I missed something!
Thanks. I shall read it again
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Mark:
I thought about what you said. I am still confused. You said that if I am using RMI, threading is taken care by itself. I am talking about threading at the database level. The Db access methods are not thread safe. If there are 10 clients trying to access the dbase how can RMI take care of the threading like locking the records..etc?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Maybe I can explain better.
You were talking about implementing Runnable or extending Thread. That is what I am talking about being unnecessary.
As for as the data class being Thread safe that is what the Synchronized keyword is used for.
When I said RMI handles threads for you I meant this.

Client A does a lookup into the RMI Registry, and it returns the Object, you now have remote access to that class, RMI gives you the thread to get that access.
Now client B does a lookup and gets the same object. Now RMI gives that client a seperate thread connection to the class.
Now in the class if you have the Synchronized keyword in the method declaration and client A calls it, client B cannot call it because it is locked by client A.
Does that help?
Thanks
Mark
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Great!! Thanks Mark.
So my application layer(which interacts with the database).. all it does is spawns up new threads when a client opens up a browser...probably I plan to use a serialised object to convey to the Server about a client requesting something..and then spawn up a thread for the user....

something like this
class Databasethread implements runnable
{
start threads here....one for each client
}
what say?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

"browser..."
What browser, there is no browser for this assignment.

class Databasethread implements runnable
{
start threads here....one for each client
}
what say?

NO THERE ARE NOT ANY THREADS TO CREATE!!!.
You are not understanding the assignemtn, the requirements and how to complete them.
Obviously you didn't understand any of my explainations. I apologize for that. Maybe someone else will be able to help you.
Good Luck
Mark Spritzler
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Hey Mark!
I did understand what you said but wasn't sure if I had asked the right question. And I am sorry I did not mean "browser' per se, I meant the client, which in this case is the GUI.
When I downloaded the assignment, it said that we have to take care of the Threading, networking, GUI and a few enhancements to the Database (locks, unlocks..etc).
The impression that I got from your previous replies were that, there is no need to spawn any threads (since RMI does one for each cleint)and I see that the DATA class already has some methods synchronised. Then I was asking myself, "Do I even need to do any thread handling?"
The book on Java developer certification which I happened to skim(Phillip Heller), talks a little bit about threads, it talks about thread pooling Vs generating threads for each client request.
So then maybe it is for programmers using socket programming! I dont know.
So then there is not much to do for the thread handling part..I guess! Am I right?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes. Thank you, I feel so much better. I thought I wasn't doing a good job explaining it.
Yes, the only place that there is a synchronization issue that you need to handle is in your lock and unlock method. Basically you will call lock, and it will synchronize on your HashSet, HashMap, or LockManager, whoever holds all the locks. but that is thread safety issue rather than making threads.
Mark
Prakash Krishnamurthy
Ranch Hand

Joined: Oct 08, 2002
Posts: 154
Thank you, actually!
I was looking at some good 2 weeks for coding threads...but feel much better that I asked about it in this forum before I could start coding ..otherwise I would have wasted so much time..
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading Model