We want the blinking to begin when the application starts, so we’ll start the thread in the initialization code in HelloComponent4’s constructor. It takes only two lines:
Thread t = new Thread(this);
t.start();
First, the constructor creates a new instance of Thread, passing it the object that contains
the run() method to the constructor. Since HelloComponent4 itself contains our run()
method, we pass the special variable this to the constructor. this always refers to our
object. After creating the new Thread, we call its start() method to begin execution.
This, in turn, invokes HelloComponent4’s run() method in the new thread.
0 comments:
Post a Comment