Wednesday, 3 October 2018

Java Multithreading Overview


  • A Thread is a single sequential flow control within a program.
  • Multi Threading  means that you could have multiple threads of execution inside the same program.
  • Creating and Starting Java Threads
    • By implementing Runnable Interface
    • By implementing Callable Interface
    • By Extending Thread Class
  • Race Conditions and Critical Sections
    • The problems arise when multiple threads access the same resources.
    • In fact, problems only arise if one or more of the threads write to same resource.
  • Thread Safety and Shared Resources
    • Code that is safe to call by multiple threads simultaneously is called thread safe.
    • Race condition only occur when multiple threads update shared resources.Therefore it is important to know what resources Java threads share when executing.
    • Local Variables
      • Local variables are stored in each thread's own stack. That means that local variables are never shared between threads and it is thread safe.
      • That also means that all local primitive variables are thread safe.
    • Local Object References
      • All objects are stored in the shared heap.
      • If an object created locally never escapes the method it was created in, it is thread safe.
    • Object Members
      • Object members are stored on the heap along with the object.
      • if two threads call a method on the same object instance and this method updates object members, the method is not thread safe.




  • No comments:

    Post a Comment

    Search This Blog

    Contact us

    Name

    Email *

    Message *