output ought to be a random mix from the threads (i.e. anything from one after another to total mish-mash)
I would expect the threads to execute in unpredictable order, so the As i is a local variable, I would expect to see it increase by 1 each time a line was output from a thread. So all thread 1 output would have i incrementing in sequence, all thread 2 output would have it increments in sequence, etc.
As p is a class variable, and therefore shared between the threads, and incremented by all of them, I would expect to see p increase from 1 on the first line of output, to 2 on the second, 3 on the third, etc, regardless of the order in which the threads are executed.
Originally posted by CL Gilbert:
The point is each instance of the class gets its own copy of member variables. If you wanted p shared between instances you should have made it static.
Ahh Henry, for shame
You said p was not a class variable, then you went on to call the program unsafe due to p.
Originally posted by Simon Birch:
I was making an unfounded assumption though - I assumed that a thread could only be interrupted when a line of code had finished executing.
Originally posted by scott p laplante:
as for te claims that this is not thread safe, I have a question...
are both ++p and p++ atomic operations? if so, i don't see how this is not thread safe.
What does a metric clock look like? I bet it is nothing like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton