I have an Android app that has a Socket open. I would like to write to this socket without any possibility of blocking the thread on the write for a noticeable amount of time. If any IO error occurs, I would like the write to just silently fail. Is there an easy way to do this?
Asked
Active
Viewed 3,469 times
1
-
3Perform the write on a separate thread? – MByD Jul 20 '11 at 21:38
-
@MByD: Ugh. No. Just no. Threads are not a substitute for non-blocking I/O. – C. K. Young Jul 20 '11 at 21:42
-
@Chris Jester-Young Why is that? Could you elaborate some? – Paul Lammertsma Jul 20 '11 at 21:47
-
@Paul: [This article](http://www.ibm.com/developerworks/java/library/j-javaio/) explains it better than I can. :-D – C. K. Young Jul 20 '11 at 22:00
-
Thanks, that is very informative! In short, it's to reduce thread overhead and improve performance and scalability, although it requires more maintenance code. I would suppose that NIO really begins to show its benefits when making a large number of asynchronous connections. – Paul Lammertsma Jul 20 '11 at 22:07
-
1In this scenario I would just spawn a thread for the existing socket code. – Matt Jul 20 '11 at 22:21
-
I agree that threads are not a substitute for non-blocking I/O. But threads can often be an excellent design alternative. Just as NIO can be a poor design alternative. "It depends". Here's a good discussion: http://stackoverflow.com/questions/5437722/java-in-2011-threaded-sockets-vs-nio-what-to-choose-on-64bit-os-and-latest-java – paulsm4 Jul 21 '11 at 01:23
2 Answers
3
Yes, NIO provides a SocketChannel
class (call the getChannel
method on your Socket
), which allows you to call configureBlocking
to use non-blocking mode. You should then do all your I/O through the channel, and not through the Socket
object.

C. K. Young
- 219,335
- 46
- 382
- 435
2
You might want to consider looking at NIO:
http://developer.android.com/reference/java/nio/channels/package-summary.html

paulsm4
- 114,292
- 17
- 138
- 190
-
-1 SCITE. Not because your answer is wrong (it's not), but because it provides less information than my answer and it's posted 3 minutes late. – C. K. Young Jul 20 '11 at 21:47
-
-
Chris Jester-Young - I actually started researching my answer before you. "Speed" is not necessarily a virtue - and "Haste" is most decidedly a vice. – paulsm4 Jul 21 '11 at 00:35
-
1@paulsm4: Now now, I'm not claiming you stole my answer or anything. I don't give a toss about that. I'm just saying that in the old-timers' honour code, if you post a late answer that isn't substantially better than earlier answers, the Right Thing to do is delete your answer. (The principle being that all else being equal, the earlier answers should win out.) Certainly, I've deleted many of my answers for that reason alone, and [I'm not the only one doing it](http://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem/9745#9745). – C. K. Young Jul 21 '11 at 16:05
-
@paulsm4: (As an aside, users with 10k rep can see deleted posts, and once you get there, you'll notice just how many deleted duplicate answers there often are.) I'm mentioning this to you as an insight into how the culture on SO works. – C. K. Young Jul 21 '11 at 16:13
-
-
@RenniePet http://meta.stackexchange.com/questions/18014/what-is-fgitw-and-scite-on-mso – C. K. Young Jul 20 '13 at 14:05