Hi Taras Shemberko,
Welcome to CodeRanch!
Its good that you've used code tags, however, make sure to execute code at local machine before posting it. e.g. the code given by you is having 2 issues:
1) Typo error in InterruptedException ('c' is missing in Exception
)
2) Since try block is empty, there's no code inside it which will throw an InterruptedException. Hence, another compile time error would be - unreachable catch block. A good practice is to catch only those exceptions which can be thrown by try block.
That apart, lets come to the code snippet (assuming that all compile time errors are resolved).
This style of construction is called as anonymous subclass, or inner class (btw, are they same?
). Here, what exactly you are trying to do is - create a new class, which is a sub-class of
Thread. The new class does not have any name. Now, in that new class, you've overridden run method. Thus, the new class will come into picture only as long as timer object is concerned - i.e. when 'timer' is started, the code of run method would be invoked internally. However, the behavior is specific only to that object. All other Threads will not necessarily have this behavior.
This phenomenon is widely used in UI programming, where each element can have different ActionListener, but since those Listeners are going to be used only for those elements, there's no point in writing a new class and creating an object. So, it can be easily done by anonymous classes.
I hope this helps.