Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33275
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: multithread client socket |
| Date | 2015-12-02 15:30 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n3nk96$2ea$1@dont-email.me> (permalink) |
| References | (1 earlier) <n3ksvl$683$2@dont-email.me> <4ab5709a-0b5d-4f1b-8b50-139828b5b76e@googlegroups.com> <n3n0s9$8ml$3@dont-email.me> <n3n43i$uto$1@dont-email.me> <a2aef40d-4926-4982-bd07-9d344c07a772@googlegroups.com> |
On 12/2/2015 2:47 PM, Eric Douglas wrote:
> On Wednesday, December 2, 2015 at 10:55:04 AM UTC-5, Eric Sosman wrote:
>> Use an AtomicInteger, not a volatile int. The latter is not
>> thread-safe (unless guarded by other synchronization, or unless it's
>> read by many threads but modified by only one).
>
> but thread-recklessness is such fun, eh? I just wrote a test program today that crashed with a ConcurrentModificationException.
>
> I've actually never used the keyword volatile or an AtomicInteger.
> A standard int seems to work fine to track the number of clients as long as it's a single server object doing all the tracking.
If there's only one thread that looks at or modifies the counter,
a plain int is fine.
If there are multiple threads looking at the counter but only one
modifying it, you need a volatile int. (Modifying the counter inside
the only thread that modifies it is safe, but you need volatile to
ensure that the modifications are "published" to other threads.)
If there are multiple threads modifying the counter, you need some
kind of synchronization for the writes (so they don't interfere with
each other) and for the reads (so the effect of the writes are made
visible to other threads). There are lots of ways to do this, but
perhaps the three simplest are:
- Use a plain int, but do all the reads and writes inside a block
(or entire method) that synchronizes on one object,
- Use a volatile int, synchronizing around all writes and letting
the reads just rely on volatility to make the writes visible, or
- Use an AtomicInteger, whose own methods provide all the necessary
synchronization and publication without further work on your part.
"Seems to work" is just "I haven't noticed the failures yet." If
you want code that's more trustworthy than "Keep your fingers crossed,"
you'll need a bit more care. It's not hard -- and if you're doing
multi-threaded stuff, you Really Really Should exercise care.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
multithread client socket Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-01 09:36 -0800
Re: multithread client socket Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2015-12-01 10:12 -0800
Re: multithread client socket Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-01 19:38 +0000
Re: multithread client socket Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-01 22:56 -0800
Re: multithread client socket Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-02 14:57 +0000
Re: multithread client socket Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-02 10:54 -0500
Re: multithread client socket Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-02 19:18 +0000
Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-02 11:47 -0800
Re: multithread client socket Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-02 15:30 -0500
Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-02 12:57 -0800
csiph-web