Campbell Ritchie wrote:That looks like letting a this reference escape the constructor; as Paul C says, it is something you should never do.
Paul Clapham wrote:Nope... consider this situation:
As you can see, the thread is started before the SpecializedThing's initialization is complete. As you say, that could be a disaster.
Jesper de Jong wrote:Possible solution: Make the constructor private and provide a static factory method:
Thomas Gard wrote:To have the least obtrusive API, there are times when I want the constructor of the object to launch a thread that is needed by the object.
Campbell Ritchie wrote:That looks like letting a this reference escape the constructor; as Paul C says, it is something you should never do.
Campbell Ritchie wrote:You cannot even guarantee that your second code snippet will complete instantiation before the print statement runs because they have disjoint frames
Campbell Ritchie wrote:Stephan is right about method names; the name should make it clear to all reading it what it does: here are three examples of methods whose names don't tell users what they do: 1 2 3. At least I think the names don't make it obvious what those methods do. Stephan is right: that sort of side‑effect can be dangerous.
Not unless you can find somewhere in the JLS saying line 1 happens before line 2.Thomas Gard wrote:. . . The JLS doesn't require that the line above the println() happen (and complete) first?
Does that make any difference?. . . it's not a method name with side effects, it's an constructor with side effects.
So you are accessing Swing Components via two threads? Dangerous: most Swing classes are non‑thread‑safe. If you want something to be done repeatedly, you are probably better off with a Swing Timer.. . . spawning a thread for the button that waits(). . . .
Campbell Ritchie wrote:
Not unless you can find somewhere in the JLS saying line 1 happens before line 2.Thomas Gard wrote:. . . The JLS doesn't require that the line above the println() happen (and complete) first?
Does that make any difference?. . . it's not a method name with side effects, it's an constructor with side effects.
So you are accessing Swing Components via two threads? Dangerous: most Swing classes are non‑thread‑safe. If you want something to be done repeatedly, you are probably better off with a Swing Timer.Campbell Ritchie wrote:. . . spawning a thread for the button that waits(). . . .
No. We have had trouble with people “correcting” old posts so replies look nonsense. At the moment editing is tightly restricted.Thomas Gard wrote:Is there any way to edit a post . . . .
Thomas Gard wrote:Well it's not a method name with side effects, it's an constructor with side effects.
The Repeat button functions by spawning a thread for the button that waits().
Basically, invoking the constructor:should be idiomatically identical to any other button:or(etc.), or I will most certainly violate the law of least surprise.
What's unacceptable in this case is to have the threading implementation of the repeating strobe be made external in any way because it's not something that I want to have only show up occasionally.
Consider this mistake: Here I have a button suddenly break the already known idiom. The user certainly is expecting to create a repeating button that works when you create it.
In other words, the meaning of the constructor is 100% clear: A repeating button. It's not "a repeating button with a thread you have to start externally".
Thomas Gard wrote:But what I didn't know was there WAS such a thing as javax.util.SwingTimer class. THANKS! I'll look into it. What JDK revision did that show up in?
Stephan van Hulst wrote:
Thomas Gard wrote:The Repeat button functions by spawning a thread for the button that waits().
So the thread has to wait anyway until the button is held? Then why not just start it in the mouse down event?
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|