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