166.
a.
b.
c.
d.
e.
f.
g.
167.
a.
b.
}
Answer:
When the above code is executed,
the JVM creates a thread to
run this code. This thread executes the code in the main
method
after which the program terminates.
Explain some of the main methods in the Thread class.
Answer:
start(): Initiates the thread and invoke the run() method
run(): Code that needs to be executed in a separate thread
needs to
be placed in the run method
sleep(): makes the running thread pause for the specified
time
setName(): sets the name of the thread to the value specified
join(): waits for the thread to die for at most the specified
time
isAlive(): Returns a boolean value that indicates if the
current
thread is alive or not
setPriority(): Changes the priority of the thread to the
specified value
Explain thread states.
Answer:
Thread exists in five states. They are as follows:
New: A thread is in new state
when a new Thread is created
but the start method is not invoked.
Runnable: A Thread is in the Runnable state after the start()
method is invoked but the Thread scheduler has not yet
c.
d.
e.
168.
169.
started executing the thread.
Running: When the thread scheduler
starts executing the
thread, it is in the Running state. The code within the body of
the run() method gets executed.
Do'stlaringiz bilan baham: