JAVA

Multithreading in Java

*     Multithreading in java is a process of executing multiple threads simultaneously.

*     Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.But we use multithreading than multiprocessing because threads share a common 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 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 exception occur 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 by two ways:

1) Process-based Multitasking (Multiprocessing)

Each process have its own address in memory i.e. each process allocates separate memory area.Process is heavyweight.Cost of communication between the process is high.Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.

2) Thread-based Multitasking (Multithreading)

*     Threads share the same address space.

*     Thread is lightweight.

*     Cost of communication between the thread is low.

What is Thread in java

A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.

Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.

Life cycle of a Thread (Thread States)

A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.But for better understanding the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

·        New

·        Runnable

·        Running

·        Non-Runnable (Blocked)

·        Terminated

 

  1) New

 The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

 2) Runnable

 The thread is in runnable state after invocation of start() method, but the thread schedulerhas not selected it to be the running thread.

 3) Running

The thread is in running state if the thread scheduler has selected it.

4) Non-Runnable (Blocked)

This is the state when the thread is still alive, but is currently not eligible to run.

5) Terminated

Starting a thread:

start() method of Thread class is used to start a newly created thread. It performs following tasks:

A new thread starts(with new callstack).

The thread moves from New state to the Runnable state.

When the thread gets a chance to execute, its target run() method will run.

How to create thread

There are two ways to create a thread:

class Multi extends Thread{ 

public void run(){ 

System.out.println("thread is running..."); 

} 

public static void main(String args[]){ 

Multi t1=new Multi(); 

t1.start(); 

 } 

} 

 


OUTPUT:

thread is running...


 

By implementing Runnable interface

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.

Runnable interface have only one method named run().

 

public void run(): is used to perform action for a thread.

class Multi3 implements Runnable{ 

public void run(){ 

System.out.println("thread is running..."); 

} 

 

public static void main(String args[]){ 

Multi3 m1=new Multi3(); 

 

Thread t1 =new Thread(m1); 

t1.start(); 

 } 

}

 


OUTPUT:

thread is running 


 

Thread Scheduler in Java

Thread scheduler in java is the part of the JVM that decides which thread should run.

There is no guarantee that which runnable thread will be chosen to run by the thread scheduler.

Only one thread at a time can run in a single process.

The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.

Difference between preemptive scheduling and time slicing

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

Commonly used methods of Thread class:

public void run(): is used to perform action for a thread.

public void start(): starts the execution of the thread.JVM calls the run() method on the thread.

public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

public void join(): waits for a thread to die.

public void join(long miliseconds): waits for a thread to die for the specified miliseconds.

public int getPriority(): returns the priority of the thread.

public int setPriority(int priority): changes the priority of the thread.

public String getName(): returns the name of the thread.

public void setName(String name): changes the name of the thread.

public Thread currentThread(): returns the reference of currently executing thread.