Week1 -Threading Flashcards
Multi threading
two or more part that can run concurrently. They can run at the same time, while making optimal use of available resources, such as the CPU.
Thread Life Cycle
The life cycle of a variety of stages: Born, Started, Run, Die.
Thread States
The thread states are
- New
- Runnable
- Block
- Wait
- Timed_Waiting
- Terminate
New
Is the born thread state. It stays in this state until the program starts the thread.
Runnable
The thread has been started and is executing a task.
Block
when a thread try’s to acquire an object lock, but said lock is being used by another thread. It will remain in this state until the other thread has finished an releases the lock
Wait
a thread goes to wait state once its calls the wait() method on an object. It will need to wait indefinitely for another thread to perform a particular task.
Timed_Waiting
thread is waiting for another thread to perform a particular action for a specific amount of time. The thread transitions back to Runnable after interval time has expired or when the event its waiting for occurs.
Terminate
thread completes execution. terminated.
Thread Priorities
helps the operation system to determine the order in which threads are scheduled. Thread priority does not guarantee the order in which the threads will execute. and are very platform dependent.
Thread Priority range
MIN_PRIORITY - CONSTANT 1
MAX_PRIORITY CONSTANT 10.
By default every thread is given a priority of NORM_PRIORITY CONSTANT 5.
Joining Threads
Allow one thread to wait for another thread to finish its execution. If t is a thread object whose threads are currently executing. Then t.join() will make sure that the t is terminated, before next instructions are executed by the program. If there are multiple threads calling join() method, it mean overloading on the joins allows the programmer to specify a waiting period.
3 overload functions
join()
join(long millis)
join( long millis, int nanos)
join()
waits for the thread to die. It will put the current thread on wait until the thread in which it was called is dead. If interrupted will throw an interrupted exception.
syntax: public final void join(){
do something
}
join(long millis)
waits at the most millis millisecond for the thread to die.
syntax: public final synchronized void join (long millis){
do something
}
join( long millis, int nanos)
waits for the millis millisecond plus nanos nanosecond for the thread to die.
syntax: public final void join(long millis, int nanos){
do something
}
How many thread are creating when you start a java program?
Three thread are creating
- Main
- Garbage Collector
- Thread Scheduler
Garbage Collector
The garbage collector intermittently removed de-referenced objects from memory. We can not force garbage collection but can request for it to run.
System.gc() - gently request garbage to run.
We can however intervene with garbage collection, in order to specify behavior at the time of garbage collection. java.lang.object.finalize().
A form of Deamon Thread - we don’t interact or control these. They are low priority background processes.
final
is a reserved keyword in java. It can’t be used a identifier. It can be used with methods, variables (objects) and classes.
final class- can’t be extend
final method - cant be overriden
final variable - the value of the variable can not be changed once its initialized .
finally
is used within the try/catch block. It guarantees that a section of the code will be executed even if an exception is thrown.
finalized()
is the method of object class. This method id called just before an object is garbage collected. finalized() method overrides the dispose system resources, performs clean up activities and minimize memory leaks.
Thread Scheduler
The Thread scheduler in java is the part of the JVM that decides which thread should run.
a form of Deamon thread- we don’t interact or control these. They are low priority background processes.
How to create a thread.
- extend thread
- implement Runnable Interface
extend thread
within the main args you start by calling the start() method , which is inhearited by the thread. It runs the method. extendThread.start(). A thread is already Runnable.
syntax: public class MyThread extends Thread{ public void run(){ do something } }