0

I'm building an android monitoring application, In my application I want a dedicated thread for all socket tasks. Meaning that in this thread I want to use Async socket that will send and recieve all the data in the server.

I tried searching for a good example for async sockets in Android but with no luck.

Thanks, Daniel

Daniel L.
  • 5,060
  • 10
  • 36
  • 59

1 Answers1

1

AFAIK Android doesn't support Java 7's NIO2 and I suspect it wouldn't be very useful if it did. If you want to use one core thread, I suggest you use a framework which supports NIO to do this.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I want to create something like 20 sockets which will update every 5 seconds. Do you think one thread is the way to go? (rather using AsyncTask/TimerTask) – Daniel L. Oct 30 '12 at 11:59
  • I would use a thread per socket which read events from those socket and combine them into an ExecutorService if you really need to have one processing thread. Or you could have each socket have its own thread and do its own work. – Peter Lawrey Oct 30 '12 at 12:04
  • Thanks for the quick response! Just last thing - is it a good practice to open 20 threads just for my app connectivity? Basically, i'm looking for the Android/Java version in which send and receive will be async in such a way that on completion it will call to a callback function (like in other languages). Thanks again – Daniel L. Oct 31 '12 at 09:11
  • On a server, blocking IO is fine up to 1000 thread/connections. On a mobile device I suspect the tipping point is much lower such as 100 or less. You may be better off with NIO for 20 connections but the increased complexity might not be worth it. – Peter Lawrey Oct 31 '12 at 09:13