Flashcards in Threads Deck (85)
Smallest unit of execution scheduled by Operating systems
A group of associated threads that execute in the same shared environment
The process that has one or more threads
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
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?
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?
1. Thread.MIN_PRIORITY (1)
2. Thread.NORM_PRIORITY (5)
3. 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 Thread(new PrintData())).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
It is the process of intermittently checking the data at some fixed interval
What method do we use generally for Polling?
What exception does Thread.sleep method throws?
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?