Real-life Example of Java Multithreading

Suppose you are using two tasks at a time on the computer, be it using Microsoft Word and listening to music. These two tasks are called processes. So you start typing in Word and at the same time start music app, this is called multitasking. Now you committed a mistake in a Word and spell check shows exception, this means Word is a process that is broken down into sub-processes. Now if a machine is dual-core then one process or task is been handled by one core and music is been handled by another core. 

In the above example, we come across both multiprocessing and multithreading. These are somehow indirectly used to achieve multitasking. In this way the mechanism of dividing the tasks is called multithreading in which every process or task is called by a thread where a thread is responsible for when to execute, when to stop and how long to be in a waiting state. Hence, a thread is the smallest unit of processing whereas multitasking is a process of executing multiple tasks at a time.

Multitasking is being achieved in two ways:

  1. Multiprocessing: Process-based multitasking is a heavyweight process and occupies different address spaces in memory. Hence, while switching from one process to another, it will require some time be it very small, causing a lag because of switching. This happens as registers will be loaded in memory maps and the list will be updated.
  2. Multithreading: Thread-based multitasking is a lightweight process and occupies the same address space. Hence, while switching cost of communication will be very less.

Below is the Lifecycle of a Thread been illustrated

  1. New: When a thread is just created.
  2. Runnable: When a start() method is called over thread processed by the thread scheduler.
    • Case A: Can be a running thread
    • Case B: Can not be a running thread
  3. Running: When it hits case 1 means the scheduler has selected it to be run the thread from runnable state to run state.
  4. Blocked: When it hits case 2 meaning the scheduler has selected not to allow a thread to change state from runnable to run.
  5. Terminated: When the run() method exists or stop() method is called over a thread.

If we do incorporate threads in operating systems one can perceive that the process scheduling algorithms in operating systems are strongly deep-down working on the same concept incorporating thread in Gantt charts. A few of the most popular are listed below which wraps up all of them and are used practically in software development.

  • First In First Out
  • Last In First Out
  • Round Robin Scheduling

Now Imagine the concept of Deadlock in operating systems with threads – how the switching is getting computed over internally if one only has an overview of them. 

So far we have understood multithreading and thread conceptually, so we can conclude advantages of multithreading before moving to any other concept or getting to programs in multithreading.

  • The user is not blocked as threads are independent even if there is an issue with one thread then only the corresponding process will be stopped rest all the operations will be computed successfully.
  • Saves time as too many operations are carried over at the same time causing work to get finished as if threads are not used the only one process will be handled by the processor.
  • Threads are independent though sharing the same address space.

So we have touched all main concepts of multithreading but the question striving in the head is left. why do we need it, where to use it and how? Now, we will discuss all three scenarios why multithreading is needed and where it is implemented via the help of programs in which we will be further learning more about threads and their methods. We need multithreading in four scenarios as listed.

  • Thread Class
  • Mobile applications
    • Asynchronous thread
  • Web applications
  • Game Development

Note: By default we only have one main thread which is responsible for main thread exception as you have encountered even without having any prior knowledge of multithreading

Java Multithreading Tutorial

Threads are the backbone of multithreading. We are living in a real world which in itself is caught on the web surrounded by lots of applications. With the advancement in technologies, we cannot achieve the speed required to run them simultaneously unless we introduce the concept of multi-tasking efficiently. It is achieved by the concept of thread. 

Similar Reads

Real-life Example of Java Multithreading

Suppose you are using two tasks at a time on the computer, be it using Microsoft Word and listening to music. These two tasks are called processes. So you start typing in Word and at the same time start music app, this is called multitasking. Now you committed a mistake in a Word and spell check shows exception, this means Word is a process that is broken down into sub-processes. Now if a machine is dual-core then one process or task is been handled by one core and music is been handled by another core....

Two Ways to Implement Multithreading

Using Thread ClassUsing Runnable Interface...

Method 1: Using Thread Class

Java provides Thread class to achieve programming invoking threads thereby some major methods of thread class are shown below in the tabular format with which we deal frequently along the action performed by them....

Synchronization

It is the mechanism that bounds the access of multiple threads to share a common resource hence is suggested to be useful where only one thread at a time is granted the access to run over....

Why Synchronization is Required?

Data inconsistency issues are the primary issue where multiple threads are accessing the common memory which sometimes results in faults in order to avoid that a thread is overlooked by another thread if it fails out.Data integrityTo work with a common shared resource which is very essential in the real world such as in banking systems....

Real-life Scenario

Suppose a person is withdrawing some amount of money from the bank and at the same time the ATM card registered with the same account number is carrying on withdrawal operation by some other user. Now suppose if withdrawing some amount of money from net banking makes funds in account lesser than the amount which needed to be withdrawal or the other way. This makes the bank unsafe as more funds are debited from the account than was actually present in the account making the bank very unsafe and is not seen in daily life. So what banks do is that they only let one transaction at a time. Once it is over then another is permitted....

Conclusion

Understanding Java Multithreading is important for creating fast and efficient applications. By using multithreading, you can run multiple tasks at the same time, making your programs more responsive and powerful. Key ideas like thread synchronization, concurrent collections, and the executor framework help manage multiple threads safely. In the above article we have described all details about Java multithreading....

Java Multithreading Tutorial – FAQs

What is multithreading in Java?...

Contact Us