hi....i m doing a client-server application in which there are multiple clients and they are controlled by a single server.... here i m capturing screen of all clients and i want them to send towords server...so it requires multithreading.... so can anyone tell me how can i use multithreading in my application...?
You might want to checkout thread pools for handling incomming connections:
http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html
Read up on java.util.concurrent
, in particular the Callable
interface which is better than the Runnable
interface in earlier Java versions because it lets you return a value from the call
method (as opposed to the run
method). Thread Pools are also useful - they're created by the Executor
class as ExecutorServices
, and you can limit the number of threads, and hence limit the load on the server side. Certainly the example in the JavaDoc for these is a simple server that accepts connections from clients, so it could be applicable to your situation (although it isn't very clearly described).
Otherwise, threading is a very large subject that really cannot be answered in a post here. You could buy a book on Java Threading, but I don't know which one is the best.
This isn't obviously a multithreading problem, as you've described it;
- if this is a client-driven scenario (i.e. the clients choose when to send their screens), you could have them call a method in the server.
- if it's server-driven, you could use the Observer pattern
Either way, if the problem is as you've described, you can avoid multithreading!
Ideally, you should use a web-server or app server for you server implementation that would already handle this for you.
If this is some sort of custom server, then you should check out thread pools as was already mentioned. This will probably suffice if you don't have any issues with the work being performed by the worker threads not needing to be threadsafe (i.e. Just a simple concurrent dispatching issue for performance).
If you require indepth knowledge of threadsafe programming, then you have a lot more research to do, books to read, and ideally some mentoring to receive.