09

Cards (18)

  • Concurrency
    The property of executing multiple threads and processes simultaneously
  • Thread
    The smallest unit of execution that can be scheduled by the operating system
  • Process
    A group of associated threads executed in the same shared environment
  • A single-threaded process contains exactly one thread, whereas a multithreaded process supports more than one thread
  • In a shared environment, threads in the same process share the same memory space and can communicate directly with one another
  • Thread scheduler
    Determines which threads should be currently executing
  • Context switch
    The process of storing a thread's current state and later restoring the state of the thread to continue execution
  • When a Java application starts, its main() method is executed by the main thread – a special thread that is created by the Java VM to run your application
  • Creating a thread in Java

    1. Extend the Thread class and override the run() method
    2. Implement the Runnable interface
  • Extending the Thread class
    • public class ThreadDemo extends Thread { ... }
  • Implementing the Runnable interface
    • public class ThreadDemo implements Runnable { ... }
  • Thread methods
    • start
    • run
    • setName
    • getName
    • setPriority
    • getPriority
    • isAlive
    • join
    • sleep
  • Thread priority
    A numeric value associated with a thread that is taken into consideration by the thread scheduler when determining which threads should currently be executing
  • The value of the priority level must be within the range MIN_PRIORITY and MAX_PRIORITY. These values are 1 and 10, respectively. To return a thread to default priority, specify NORM_PRIORITY, which is 5
  • Using setName(), getName(), and setPriority()

    • public class ThreadDemo extends Thread { ... }
  • Using sleep(), join(), and isAlive()
    • public class ThreadDemo extends Thread { ... }
  • A thread with higher priority does not necessarily mean that it will run faster or more often than others; it only has greater potential access to the CPU
  • The Thread.currentThread() method returns a reference to the Thread instance executing currentThread()