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.