Concurrency Flashcards
what is a thread?
smallest unit of execution that can be scheduled by the OS
what is a process?
group of associated threads that execute in the same shared memorythreads can communicate with each other
what is a task?
single unit of work performed by a thread
can a thread perform multiple tasks?
yes, but only one task at a time
what is a system thread?
a thread that is created by the JVM
what is a user-defined thread?
a thread created by a developer
are all java programs multi-threaded?
yes, technically always system threads running in the background even if application only requires a single thread
what is a single-threaded application referring to?
a single user-defined thread(any number of system threads)
what is a daemon thread?
a thread that will not prevent the JVM from exiting once the program finishesex: system thread for garbage collection. when program finishes, if gb coll thread is the only one running, JVM can shut down.
what types of threads can be marked as daemon threads?
system threads user-defined threads
what is concurrency?
the property of executing multiple threads and processes at the same time?
how does the JVM determine which threads will execute given the CPUs?
often, more threads than available CPUs, so threads must be scheduled.
what is a context switch?
when threads are being executed, they may only be allotted a limited number of CPU cycles. if the thread doesn’t finish executing, the thread’s state must be stored and later restored to continue processing. performance cost with storing/restoring
what is thread priority?
a thread can interrupt or supercede another thread if it has a higher thread priority
what are the defaults = to? Thread.MIN_PRIORITY Thread.NORM_PRIORITY Thread.MAX_PRIORITY
1510
how is it determined which thread runs first if both have same priority?
thread scheduler will arbitrarily decide
what is Runnable?
a functional interface that takes no arguments and returns no data@FunctionalInterfacepublic interface Runnable() { void run();}
what is Runnable commonly used for?
define work a thread will do separate from the main application thread
What is the Thread class commonly used for?
executing a thread similar to Runnable
What is the 2-step process from executing a thread?
define the task in run();start the thread - Thread.start();
what are two ways to define the task a thread will do?
1) pass a Runnable object or lambda to Thread constructor2) extend Thread class and override run method
when Thread.start() is not used, what order are threads run in?
run tasks are completed on the main app thread in the order of code by line
when Thread.start() is used, what order are threads run in?
indeterminate - threads are run in parallel to the main app thread
when is extending Thread preferable to implementing Runnable?
you need thread priorities set