Threads

G

Ghost

I am trying to determine the best way to manage my Threads. Here is my
situation:

- I have a website that requires a user to login.
- When the user logs in, this creates an instance of my WebUser class.
- WebUser creates a new thread for each new user.
- The WebUser class is used to access information from my server. If
the user requests information from the server (by hitting a web page),
WebUser calls something like: getUserInfo().

Currently, WebUser implements Runnable. When WebUser creates a new
thread, that thread basically performs the following run function:
public void run() {
sleep(1000)
}

WebUser does not need to do anything until the user requests
information. Right now it just sleeps all the time.

Is there a better way to handle the WebUser threads? Should WebUser
subclass Thread?

Thanks for any advice.
 
A

Anton Spaans

Ghost said:
I am trying to determine the best way to manage my Threads. Here is my
situation:

- I have a website that requires a user to login.
- When the user logs in, this creates an instance of my WebUser class.
- WebUser creates a new thread for each new user.
- The WebUser class is used to access information from my server. If
the user requests information from the server (by hitting a web page),
WebUser calls something like: getUserInfo().

Currently, WebUser implements Runnable. When WebUser creates a new
thread, that thread basically performs the following run function:
public void run() {
sleep(1000)
}

WebUser does not need to do anything until the user requests
information. Right now it just sleeps all the time.

Is there a better way to handle the WebUser threads? Should WebUser
subclass Thread?

Thanks for any advice.

Why do you want to create a thread? Threads (created or re-used for each
http-request) are managed and handled by the web-server/container. Don't
create and try to manage your own threads, especially if there is not an
absolute requirement for it:

- User logs in.
- The http-request from this log-in goes arrives at your web-server.
- Create a new WebUser instance, cache it, and Call the 'getUserInfo()'
(directly, not via a thread).
- Do some other stuff.
- Write the response (http-response) back to the browser.
- Browser displays the response.

- User goes to next page.
- The http-request goes to your web-server.
- Based on the user, get the correct WebUser from the cache.
- Using the WebUser (config info?), handle the rest of the request.
- etc..

BTW: How do you handle the deletion of WebUser instances (when users go
idle)? You should use the http-session instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top