Threads Flashcards
Thread definition
Smallest unit of execution scheduled by Operating systems
Process definition
A group of associated threads that execute in the same shared environment
Multi-threaded process?
The process that has one or more threads
Task?
Single unit of work performed by the thread
What is the System Thread?
A system thread is created by JVM and runs in the background of application. Eg.. Garbage Collector
Give some examples of problems that occur in System Thread? Whatexceptions do they throw?
Memory space Error.
They do not throw Exception, they throw Error
What is User-defined threads?
A thread created by application developer to accomplish certain tasks.
What are daemon threads?
Threads that do not prevent JVM from exciting when the application finishes
Eg.., Garbage Collector. If GC is the only thread that is running, then JVM shuts down
Concurrency?
The process of executing multiple threads at the same time is called as Concurrency
What does the operating systems uses to find which thread to execute?
Thread scheduler
What is context Switching?
It is the way of storing the current thread state and later reusing the state of the thread to continue execution
When is context switching be used?
When a thread doesnt complete its task in allotted time, context switch occurs
what are the three constant thread Static constant variable that determines the Thread priority and their values?
- Thread.MIN_PRIORITY (1)
- Thread.NORM_PRIORITY (5)
- Thread.MAX_PRIORITY (10)
What happens when two or more threads have same priority?
The thread scheduler decides arbitrarily
What is the Interface to implement to create a thread?
Runnable Interface. It is a functional Interface
What is special about Runnbale Interfaces?
It doesnt take any arguments and doesnt return anything
What is the simplest way of creating a Thread?
Extending java.lang.Thread class
What does Java does not guarantee?
It does not guarantee the order in which the thread will be processed once the thread starts
What should we keep in mind on creating a thread using Runnable interface
We should pass the Runnable object to the Thread constructor
How should we start a thread?
We should call the ‘start()’ method that starts a new task for the Thread in operating systems
Will the below compile? What is the actual implications?
new PrintData().run(); (new Thread(new PrintData())).run(); (new ReadInventoryThread()).run();
Yes, the below compiles. But the thread created each calls the run method and thus no seperate task on the OS is executed, causing each line to wait until the previous completes
Polling?
It is the process of intermittently checking the data at some fixed interval
What method do we use generally for Polling?
Thread.sleep(long);
What exception does Thread.sleep method throws?
InterruptedException
What is ExecutorService?
In Concurrency APi, java introduces ExecutorService which creates and manages threads
How ExecutorService works?
We get an instance of the ExecutorService interface and then send the service tasks to be processed
Executor Service is an interface. How do we get the ExecutorService instance?
Java concurrency API includes a Executors Factory class that can be used to get the instance
What is factory Pattern?
It is the creational pattern where the underlying implementation details of object creation is hidden from us.
What is the package of ExecutorService?
Java.util.concurrent
Method to use to get the simple ExecutorService?
newSingleThreadExecutor() method in Executors class
What should we keep in mind for Single-thread executor regarding order of execution?
Results are guaranteed to be executed in the order in which they are added. But we should not rely on this
What is the importance of calling Executors shutDown() method?
The thread executor creates a non-deamon thread on first task, so failing to call shutdown results in application never terminating
what are the process involved in shutDown method?
- rejecting any new task submitted to thread executor
2. continuing with the previously submitted task.
What does calling isShutDown and isTerminated method returns while shutDown is called?
isShutDown() returns true
isTerminated() returns false