We often use word, ‘multi-tasking’ while working on computer. Anyone using a computer would expect it to do multiple tasks at a time. For example, you may be editing a file while listening to your favorite music. You may also move / copy a file from one location to another. Performing this different activities is termed as multi-tasking. The multi-tasking activities are handled by the operating system of the computer.
The next term is ‘threading’. It means in terms of computer is breaking down a single process into multiple tasks. When you are editing your word document, the software automatically performs a spell check, it will also allow to save the file while editing. All these activities are carried out by the help of threads. Here:
The Word software is treated as a process. The different activities it carries out is treated as threads.
- What is the difference between processes and threads in Java?
Process can be referred to as as a program in execution. Thread can be referred to as a single smallest unit of code execution.
Processes are heavy-weight in nature. Threads are light-weight in nature.
A process typically has its own memory space and it also eats up run-time resources. Threads are a part of the process. Any process has at least one thread.
In Java, multi-tasking ability is achieved through effective use of threads.
You can write two types of application in Java: Single threaded application and Multi threaded application
- Describe single threaded application and multi-threaded application in Java.
Single Threaded Application
In a single threaded environment, the application will just have to wait till the network operation is finished. All I/O operations are blocking operations and hence the CPU will sit idle till the network file is not read completely. Hence, the other activity of updating the database cannot be performed.
In this case, the whole application can be broken down into different threads, wherein one thread will perform the network operation while the other will perform database operations.
At the same time, you can also display an animated progress bar to the user about the operation being carried out. All this is possible with the use of threads since all threads can work concurrently and hence, the CPU time is not wasted.
- Explain thread model in Java.
Let’s look at the threading model that is used by Java. Till now, we have only seen single threaded applications.
Whenever you run an application, a default main application thread is created and it executes the main() method. This thread ceases to exist once the main() method returns.
Similarly, there is one more thread that is spawned by the JVM (Java Virtual Machine), the Garbage Collection thread that cleans the out of scope objects. After discussing the details about threads, let’s see how threads are actually created in Java.
In Java, there are two ways to create a thread:
By extending the Thread class
By implementing the Runnable interface
- How do you create a thread in Java by implementing Runnable interface?
The simplest method for creating the thread is to implement the java.lang.Runnable interface. Any object that implements Runnable can be used to spawn a new Thread. The only method that you need to implement is the run() method whose method signature is as follows:
public void run()
The run() method creates another entry point into the application. In other words, it provides the entry point for another thread and allows concurrent execution.
You can call other methods, create instances of classes, declare variables, etc inside this method just like other Java methods. Once the thread is started, the code in this method is executed simultaneously.