SCJP – Quick Notes

THREADS

  • On an operating system a running program is known as a process
  • A single process can have seperate runnable tasks called threads
  • A thread is a single sequential flow of control within a process
  • A thread is also referred to as a lightweight process
  • With a single-processor CPU, only one thread is executing at any given time
  • The CPU quickly switches between active threads giving the illusion that they are all executing at the same time (logical concurrency)
  • On multi-processor systems several threads are actually executing at the same time (physical concurrency)
  • Multi-threading occurs when concurrency exists among threads running in a single process (also referred to as multi-tasking)
  • Java provides support for multi-threading as a part of the language
  • Support centers on the:
    • java.lang.Thread class
    • java.lang.Runnable interface
    • java.lang.Object methods wait(), notify(), and notifyAll
    • synchronized keyword
  • Every Java program has at least one thread which is executed when main() is invoked
  • When the last user thread completes any daemon threads are stopped and the application stops
  • A thread’s default daemon status is the same as that of thread creating it
  • You can check the daemon status using isDaemon()
  • You can set the daemon status using setDaemon()
  • main() daemon status is false
  • If you want all your threads to quit when main() completes you can set their status to daemon using setDaemon(true)
  • There are two basic ways to create and run threads
    1. By subclassing the Thread class
    2. By implementing the Runnable interface
  • a well behaved thread will use the yield() method to voluntarily yield to the CPU, giving it a chance to run another thread of the same priority.

Page Visitors: 2745

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *