Mike Simmons wrote:I'm less impressed by the Stack Overflow post, particularly the part that gave us the addProperty() method discussed here. The problem is that whether or not the individual methods are thread-safe (yes for StringBuffer, no for StringBuilder), the overall code gives unpredictable results because you need more than one append() call to add a property, and there is nothing preventing two different threads from interfering with each other. Even if we use a "thread safe" StringBuffer, the code using the StringBuffer is not thread safe. So it's a confusing example.
Mike, regarding your original question, I would say that in general it's often pretty difficult to *demonstrate* a lack of thread safety. Most of the time if you write unsafe code, it will probably work fine, most of the time. But sometimes, a problem may occur. And when it does occur, it tends to be hard to understand the cause, especially since we can't reliably reproduce the problem.
Campbell Ritchie wrote:At this point, you might start to move out of Big O notation; maybe 50000 executions will fill the available heap space and require multiple GC runs, causing space complexity to become its limiting factor. Repeated String concatenation is rather like that, though the Java9+ performance is much faster than in earlier versions.
. . . 3 ms to complete for a data set of N=500, how long would you expect it to take for an N=50,000 . . .
Junilu Lacar wrote:But that kind of begs the question "Why try to come up with all these other approaches for this exercise when the 2nd column already tells you what to do?"
Ana Smith wrote:Thank you, also there is math associated with this so I want to learn how to prove that it is 2x<work<4x
Junilu Lacar wrote:I dont think the point of the exercise is about getting precise measurements. Big O is about worst case. What would O(250), O(125), and O(62) be then? Are we really concerned about what those exact numbers are in this exercise?
Junilu Lacar wrote:
Ryan McGuire wrote:For the O(log N) row... Increased by 1 what? ms? s? minute?
I would assume the student is expected to apply critical thinking here as well. Understanding the shape of the growth curve of O(log N) is important. If n = 500 is completed in 3 ms, is it really reasonable to think that n = 1000 would take 1 whole second longer, much less 1 whole minute longer?
Sam Peterson wrote:Why does the greater than print out? We exit the while loop when i is equal to 10, not greater than 10.
What's wrong with just doing this: <some code>