- Multiple tasks are performed at a time is called as multitasking.
- We can perform a multiple tasks with the help of thread or a process.
- It is a type of multitasking in which multiple tasks are performed with the help of thread.
- Multiprocessing is a type of multitasking in which multiple tasks are performed with the help of multiple processes.
- If the multiple tasks are performed on a multiple processor then it is called as multiple programming.
- If the multiple tasks are performed with the help of different parts of same processor (core) then it is called as multiple Programming.
- If multiuser shares a copy of an operating system then it is called as a multiuser operating system.
public class demo
private int balance;
public synchronized debit(int amt)
bal = amt;
public synchronized credit(int amt)
bal = bal-amt;
Thread class constructors:-
- Thread(String tname);
- Thread(Runnable object);
- Thread(Runnable object, String s);
- Thread(ThreadGroup t, String s);
- Thread(ThreadGroup t, Runnable object);
- Thread(ThreadGroup t, Runnable object, String s);
Inter thread communication:-
class demo extends Thread
public demo(String s)
str = s;
public void run()
public ststic void main(String args)
Thread t1 = new Thraed(first);
Thread t2 = new Thread(second);
class hello implements Runnable
public hello(String s)
str = s;
public void run()
- Thread means single block or a function which run concurrently.
- If there are multiple functions or a block in our application then that application is called as multithreaded application.
Important points about a Thread:-
- Thread has its own local variable.
- Threads share all the open files which are opened by its parent process.
- Thread has its own stack.
- Thread has its own CPU quantum.
- Any thread can kill any other thread.
- An operating system can kill any thread at any time without intimation but in case of java application role of an operating system is performed by JVM.
- Thread can communicate with another thread by using communication methods
- Each thread has its own priority.
- When our thread gets scheduled, it is depend on the operating system strategies.
Different states of a Thread:-
- Sin this state, thread is only created but its execution is not started.
- In this state, thread is created as well as it is ready to run but there is no CPU quantum is allocated for that state.
- Thread is created and for that thread actual CPU quantum is allocated and that thread starts its life cycle for this point.
- At this state, thread executes its own function.
- This dead state is achieved successfully or not successfully.
- After the dead state, our thread cannot run again but in case of ready and running state thread can scheduled again and again.
public static void main(String args)
Runnable t1 = new hello(“first”);
Runnable t2 = new hello(“second”);
Methods of Thread class:-
- When we click on any device or when we interact with any device first signal is generated and that signal is accepted by the device driver of that device.
- After accepting this signal device driver forward this signal to the operating system.
- An operating system accepts that signal or an event and converts that event into a message.
- Message is descriptive format of that event.
- After creating the message an operating system forward that message to the currently running application.
- To handle that event application has to provide a method which is used for handling that event.
- Sending a message to our application means calling a method indirectly.
- The method is used for accepting an event is called as event or an action listener method.
- After invoking an actionListener method that method provides a specific action for that event.
- The above model is called as delegation in java and in windows it is called as an event driven architecture.
- When we write our own actionListener that actionListener must be registered towards an operating system or JVM.
- This step of registering the actionListener is important because this actinListener method is called by the operating system directly.
- To register the actionListener we can use addActionListener method.
- After registering this method our operating system or JVM call this method when event is occurred.
- This method is called as callback method in terms of operating system.
- Above model is called as events and delegates in .NET.
Hardware/Device (signal to) ===>Device Driver (event) ===>Operating system (message) ===>JVM (event) ==>Application===>actionListener===>Approprite action is taken