Two threads and 1 service, or a service per thread?
Well.. what I'm trying to do here is implement something like a peer-to-peer client. Being that, it will start a client thread and a server thread.
I know Services themselves run in the main GUI thread, so I'll have to start a couple of independent threads (or Asynctasks?) for each server and client. The only thing I'm not so sure about is if I'll better have 1 Service starting 2 threads, or maybe 2 services, each one of them starting their own thread.
What do you think? which way is better 开发者_运维问答perfomance-wise? or is it irrelevant?
Thanks Nelson
Performance-wise, it's irrelevant.
So it all depends on the semantics of your two tasks, and how much code you'd share, etc. Maybe having a BilthonService
spanning a ClientThread
and ServerThread
is not too bad, given that the service class itself will barely have any code.
You should read Processes and Threads chapter from Android Fundamentals if you have not yet read.
Each service should span it's own thread. When each new request is recepted span a new thread dedicated for that communication. This way you can handle multiple request in the same time on your server.
精彩评论