Java - Threads

The flashcards below were created by user javatomas on FreezingBlue Flashcards.

  1. Multithreading
    multiple lines of a single program can be executed at the same time
  2. Under Unix, FORKING means?
    forking a process creates a child process with a different address space for both code and data
  3. What are the 2 ways of creating a Thread?
    Implementing and Runnable interface and extending the Thread class
  4. Once a Thread is stopped, can it be restarted with start()?
    No, stop() will terminate the execution of a thread.
  5. What will happen to the Thread you can pause it?
    The thread will sleep for a certain period of time and then begin executing when the time limit is reached
  6. What does the Thread Scheduler monitor do?
    monitors all running threads in all programs and decides which threads should be running and which are in line to be executed
  7. What are the 2 characteristics of a thread that the scheduler identifies in its decision process?
    • The most important:  The priority of the thread
    • The daemon flag.
  8. What are daemon threads?
    Daemon threads are non-user threads. They are typically used to carry out low-priority tasks that should not take priority over the main task of the program
  9. What happens to the JVM when there are only daemon threads running?
    JVM will exit!
  10. The scheduler comes in what 2 flavors?
    • preemptive
    • non-preemptive
  11. What are Preemptive schedulers?
    They give a certain time-slice to all threads running on the system.
  12. What do non-preemptive schedulers accomplish?
    decide which thread should run and run it until the thread is complete
  13. yield()
    method is a way for a thread to force the scheduler to start executing another waiting thread.
  14. What is Synchronization?
    Synchronization is the act of serializing (or ordering one at a time) thread access to those code sequences that let multiple threads manipulate class and instance field variables, and other shared resources
  15. Can Threads share local variables and parameters?
    No, because local variables and parameters allocate on a thread's method-call stack. As a result, each thread receives its own copy of those variables
  16. Why do we need synchronization?
    Think of a withdrawal/deposit of financial transactions? Each thread manipulates a pair of shared variables.
  17. 3 problems with synchronization
    • no synchronization
    • deadlock
    • time cost associated with lock acquisition
  18. No synchronization
    After a thread voluntarily or involuntarily (through an exception) exits a critical code section, it releases a lock so another thread can gain entry. Suppose two threads want to enter the same critical code section. To prevent both threads from entering that critical code section simultaneously, each thread must attempt to acquire the same lock. If each thread attempts to acquire a different lock and succeeds, both threads enter the critical code section; neither thread has to wait for the other thread to release its lock because the other thread acquires a different lock. The end result: no synchronization
  19. How to avoid no-synchronization?
    choose an object common to all relevant threads. That way, those threads compete to acquire the same object's lock, and only one thread at a time can enter the associated critical code section.
  20. Deadlock
    Thread A acquires a lock that thread B needs before thread B can enter B's critical code section. Similarly, thread B acquires a lock that thread A needs before thread A can enter A's critical code section. Because neither thread has the lock it needs, each thread must wait to acquire its lock. Furthermore, because neither thread can proceed, neither thread can release the other thread's lock, and program execution freezes.
  21. How to avoid deadlock?
    carefully analyze your source code for situations where threads might attempt to acquire each others' locks, such as when a synchronized method calls another synchronized method. You must do that because a JVM cannot detect or prevent deadlock
  22. What is thread scheduling?
    Manages the one or more threads to share the processor.
  23. What are the 4 thread states?
    • Initial State
    • Runnable State
    • Blocked State
    • Terminating State
  24. Initial state
    A program has created a thread's thread object, but the thread does not yet exist because the thread object's start() method has not yet been called.
  25. Runnable state
    This is a thread's default state. After the call to start() completes, a thread becomes runnable whether or not that thread is running, that is, using the processor. Although many threads might be runnable, only one currently runs. Thread schedulers determine which runnable thread to assign to the processor.
  26. Blocked state
    When a thread executes the sleep(), wait(), or join() methods, when a thread attempts to read data not yet available from a network, and when a thread waits to acquire a lock, that thread is in the blocked state: it is neither running nor in a position to run. (You can probably think of other times when a thread would wait for something to happen.) When a blocked thread unblocks, that thread moves to the runnable state.
  27. Terminating state
    Once execution leaves a thread's run() method, that thread is in the terminating state. In other words, the thread ceases to exist.
  28. What are green threads?
    JVM threads
  29. What are native threads?
    OS handles thread scheduling
  30. How does native thread schedulers prevent starvation?
    It uses time-slicing to prevent processor starvation of equal-priority threads.

    The idea is to give each equal-priority thread the same amount of time, known as a quantum. A timer tracks each quantum's remaining time and alerts the thread scheduler when the quantum expires. The thread scheduler then schedules another equal-priority thread to run, unless a higher-priority thread unblocks.
  31. How to prevent lower-priority threads from starving?
    some thread schedulers, such as Windows schedulers, give temporary priority boosts to threads that have not run in a long time. When the thread runs, that priority boost decays. Thread schedulers still give higher-priority threads preference over lower-priority threads, but at least all threads receive a chance to run.
  32. What is the significance of setPriority(int priority)?
    proves useful in preventing processor starvation.
Card Set:
Java - Threads
2013-09-18 16:56:09

Java threads terminology
Show Answers: