Re: Strange Socket problem
On 03/02/2012 12:18 PM, x wrote:
Knute Johnson pisze:
I'm having a problem in some production code that I can't figure out.
Can you please describe the usage of your class ?
I'm rather puzzled about having a Thread object inside a Runnable. Your class
The issue would be what's used inside the 'run()' method, not just what fields
the class has.
I agree that the 'thread' variable's scope is wrong - it should be local to
'start()', not an instance member. But at the moment that's not causing any
outright harm.
makes it is extremely easy to have TWO threads accessing private fields, this
is rarely a good idea. (yes, I have recreated it :-)
His class spawns one thread per instance. The main thread doesn't use the
mutable fields. The only common access of note is the 'runFlag' mechanism,
which is bog-standard in his code. The 'thread' member is not referenced
within 'run()'. I'm not sure why all his other instance variables are
'volatile', but at first blush I don't see a problem with his synchronization
safety.
You need to be specific about the trouble you claim you found. Show us where,
because I don't see danger coming from the areas you cited.
What exactly is the point of the public start() method? I must say it looks
suspicious to me.
It's also bog standard, and bog simple. Its purpose is to start the socket thread.
public class Client
{
public static void main(String[] args)
{
new SportsWinClient().start();
}
}
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg