Page 375 - CITS - Computer Software Application -TT
P. 375

COMPUTER SOFTWARE APPLICATION - CITS




           3  Thread thread = new Thread(runnable);
           4  thread.start();
           The thread will execute the code which is mentioned in the run() method of the Runnable object passed in its
           argument.

           Multithreading in Java
           Multithreading in Java is a process of executing multiple threads simultaneously.
           A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both
           are used to achieve multitasking.
           However, we use multithreading than multiprocessing because threads use a shared memory area. They don’t
           allocate separate memory area so saves memory, and context-switching between the threads takes less time
           than process.
           Java Multithreading is mostly used in games, animation, etc.
           Advantages of Java Multithreading
           1  It doesn’t block the user because threads are independent and you can perform multiple operations at the
              same time.
           2  You can perform many operations together, so it saves time.
           3  Threads are independent, so it doesn’t affect other threads if an exception occurs in a single thread.

           Multitasking
           Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU.
           Multitasking can be achieved in two ways:
           1    Process-based Multitasking (Multiprocessing)

           2    Thread-based Multitasking (Multithreading)
           1  Process-based Multitasking (Multiprocessing)
              -  Each process has an address in memory. In other words, each process allocates a separate memory area.
              -  A process is heavyweight.
              -  Cost of communication between the process is high.

              -  Switching from one process to another requires some time for saving and loading registers, memory maps,
                 updating lists, etc.
           2  Thread-based Multitasking (Multithreading)
              -  Threads share the same address space.

              -  A thread is lightweight.
              -  Cost of communication between the thread is low.
           Synchronization in Java
           Synchronization in Java is the capability to control the access of multiple threads to any shared resource.

           Java Synchronization is better option where we want to allow only one thread to access the shared resource.
           Why use Synchronization?
           The synchronization is mainly used to
           1  To prevent thread interference.
           2  To prevent consistency problem.

           Types of Synchronization
           There are two types of synchronization
           1  Process Synchronization


                                                           362

                             CITS : IT&ITES - Computer Software Application - Lesson 101 - 108
   370   371   372   373   374   375   376   377   378   379   380