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...?
- Are these applications Swing based? What kind of communication is between them?akarnokd– akarnokd2009-06-23 09:36:24 +00:00Commented Jun 23, 2009 at 9:36
- 1Where exactly does it require multithreading? - In your client, because your client is not only sending these images but also has some other functionality - In your server, because you think multithreading could improve the performance of receiving images - In your server, because again, your server provides some other functioniality Do you have a GUI and userinteraction? Swing/SWT?sebastiangeiger– sebastiangeiger2009-06-23 09:45:22 +00:00Commented Jun 23, 2009 at 9:45
- 3I don't want to sound rude, but if you're asking a question like this you really aren't the person to be implementing a server-side threading model. The topic and use of threads is far from simple, fraught with pitfalls and never works out quite like you expect. Do yourself a favor and download an app-server where this issue and the others you'll face have already been solved.Nick Holt– Nick Holt2009-06-23 10:18:14 +00:00Commented Jun 23, 2009 at 10:18
- hi... i have done the screen capturing and its controlling on a single client and server....now i want to make it multi client application...in which there is a server can capture the screen of multiple clients and it is controlled from server....so can u tell me ...how is it possible....can i use multi threading ...? how?gajera sanjay– gajera sanjay2009-06-23 15:15:30 +00:00Commented Jun 23, 2009 at 15:15
- can u tell me how to use multithreaded multiclient-server application....?gajera sanjay– gajera sanjay2009-06-23 15:16:38 +00:00Commented Jun 23, 2009 at 15:16
4 Answers
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.
You might want to checkout thread pools for handling incomming connections:
http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html
Comments
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!
Comments
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.