Share this post on:

Threading in Java is used to achieve multi-tasking. Multitasking is a way to do more than one task simultaneously. Multitasking can be achieve by using process or thread. The Process can be further convert into multi processing. Some threading can be converted into multithreading.

Multitasking can be done in two ways:

  • Process Based
  • Thread Based

Difference between Program & Process

Program represents set of Instruction which are stored in a file in order to execute a program various resources such as memory, I/O devices, processor are provided. when resources are provided to the program it become process to provide multiple resources to the multiple program is called as multiprocessing.

Thread is a Independent module of process that ca be concurrently executed with other thread.

Multi-Processing

  • In Multiprocessing each process represents Independent application
  • Multiprocessing adds CPUs to increase computing power.
  • Multiprocessing are executed concurrently.
  • Multiprocessing can be symmetric or asymmetric.
  • Multiprocessing each process has its own Space.

Multi-Threading

  • In multithreading all the thread share as common space.
  • In multithreading represents and Independent module in the application.
  • In Multithreading creates multiple threads of a single process to increase computing process.
  • In Multithreading multiple threads of a single process are executed concurrently.
  • Multithreading are not classified.

In Java provide for the identification of independent module in the application sum microsystem provide one interface name Runnable which run() method which indicate thread.

interface Runnable

{

void run();

}

for handling the runnable Interface that is to manage sun microsystem provide one predefine class Thread.

Following are the Important Method of Thread:

  • getName() : It is used to find out name of current name.
  • setName() : It is used to change the name of current Thread.
  • sleep() : sleep method is used to suspend the current thread for specific time being.
  • start() : It is used to start a thread that is it used to start run method.
  • isAlive() : It is used to check given thread is completed or not.
  • currentThread() : It is used to obtained to reference of current thread.

Lifecycle of Thread that is states of thread

There are 5 states in lifecycle of Thread.

  1. New
  2. Runnable
  3. Running
  4. Suspended
  5. Terminated
Threading in Java

Example: Write a Java program to demonstrate Thread:

public class TestDemo
{
static
{
Thread t = Thread.currentThread();
System.out.println(“Name is “+t.getName());
System.out.println(“Priority is “+t.getName());
try
{
System.out.println(“Change Name”);
Thread.sleep(5000);
t.setName(“India”);
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args)
{
System.out.println(“Hello Main”);
Thread t= Thread.currentThread();
System.out.println(“Name of Thread after Change “+t.getName());
}

}

Output:

Name is main
Priority is main
Change Name
Hello Main
Name of Thread after Change India

There are types two types of Thread in Java

  1. Kernel Thread : Kernel Thread is directly directly related to hardware hence it is complex to manage.
  2. User Thread : User Thread is directly related to software. hence it is easy to maintain sometime it is called as light weight kernel thread.

User thread can be created by two ways:

  1. Direct Thread
  2. Indirect Thread

Syntax of direct Thread:

class userThread implements Runnable

{

}

Syntax of Indirect Thread:

class userThread extends Thread

{

}

Example: Write a Java Program to demonstrate user defined thread in Direct way?

public class DirectThrreadExample implements Runnable
{
public void run()
{
Thread t = Thread.currentThread();
System.out.println(t.getName());
System.out.println(“Task 1”);
}
}
public class DirectThread
{
static
{
Thread t= Thread.currentThread();
System.out.println(t.getName());

}
public static void main(String[] args)
{
DirectThrreadExample x=new DirectThrreadExample();
Thread t1=new Thread(x);
Thread t2=new Thread(x);
t1.start();
t2.start();
}

}

Output:

main

Thread-0

Task1

Thread-0

Task1

Example : Write a Java Program to demonstrate user Defined Thread in Indirect Ways?

public class IndirectThreadExample extends Thread {

public void run()
{
System.out.println(“Task 1”);
}

}
public class IndirectThread
{
public static void main(String[] args) {

IndirectThreadExample t1=new IndirectThreadExample();
t1.start();

}

}

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

2 Comments

  1. Pingback: How to Handle Deadlock in Java - ArzaTechs

  2. Pingback: Most Important topic for Java Interview - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *