10. Threads Flashcards
How do operations execute in a single-thread environment?
The operations execute one after another (another operation happen only when the previous one is finished)
What is a thread in java?
- An instance of class java.lang.Thread An object that lives en dies on the heap and has method and variables
- An thread of execution
An individual proces that has its own call stack. In java there is 1 thread per call stack
in how many threads does the main method run?
The main method runs in one thread, called the main thread.
What is the most important thing to understand about threads?
When it comes to thread, very little is guaranteed
How does a thread begin?
A thread in java begins as an instance of java.lang.Thread
Which methods of thread do you need to know?
- start()
- yield()
- sleep()
- run()
how does the thread of executing begin?
The thread of execution always begin by invoking the run() method.
in which 2 ways you can define and instantiate a thread?
- Extend the java.lang.Thread class
- Implement the Runnable interface
Can you overload the run() method?
Keep in mind that you are free to overload the run() method in your Thread subclass. But know this: the overloaded methods will be ignored by the Thread class unless you call it yourself. The thread class expects a run() method with no arguments, and it will execute this method for you in a separate call stack after the thread has been started.
Is the Runnable Interface a functional interface?
Yes, That means we can use a lambda expression
What are the overloaded constructors in Thread?
- Thread()
- Thread(Runnable target)
- Thread(Runnable target, String name)
- Thread(String name)
When is a thread considered alive?
To get an actual thread – a new call stack – we still have to start the thread. We do that with the start(), once the start() method is called, the thread is considered alive.
When is a thread considered dead?
A thread is considered dead
after the run() method completes.
What is the best way to determine if a thread has been started but has not yet completed its run() method?
The isAlive() method
What happens after you call start()?
- A new thread of execution starts (with a new call stack)
- The thread moves from the new state to the runnable state
- When the thread gets a chance to execute its target run() method will run.
Can a Thread be started twice?
Once a thread has been started it can never be started again. If you do, you will get a IllegalThreadException.
What does the getId() method returns?
The getId() method returns a positive unique long number, and that number will be that thread’s only ID number for the thread’s entire life.
What is the Thread Scheduler?
The thread scheduler is part of the JVM that decides which thread should run at any given moment and also takes threads out of the run state. Assuming a single processor machine, only one thread can actually run at a time.
What is the Runnable pool?
when a thread has finished with its turn it moves to the end of the line of the
runnable pool and waits until it eventually gets to the front of the line, where it can be chosen again.
Can we control the Thread Schedular?
Although we don’t control the thread scheduler, we can sometime influence it. The following
methods give us some tools for influencing the scheduler.
- public static void sleep (long millis) throws InterruptedException
- public static void yield()
- public final void join() throws InterruptedException
- public final void setPriority(int newPriority)
Which three thread related related methods inherits every class?
- public final void wait() throws InterruptedException
- public final void notify()
- public final void notifyAll()
What states can a thread be in?
A thread can be only in one of five states:
- New
- Runnable
- Running
- Waiting/Blocked/Sleeping
- Dead
What does the NEW state mean?
this is the state the thread is in after the Thread instance has been created but the
start() method has not been invoked on the thread. It is a live Thread but not a thread of
execution. At this point thread is not alive.
What does the Runnable state mean?
This is the state a thread is in when it is eligible to run but the scheduler has not selected it to be running the thread. A thread first enters the runnable state when the start() method is invoked, but can also return to runnable state after returning from a blocked/waiting/sleeping state. When the thread is in Runnable state it is considered alive.