MultiThreading in Java

Multithreading is a type of execution model that allows multiple threads to exist within the context of a process such that they execute independently but share their process resources. A thread maintains a list of information relevant to its execution including the priority schedule, exception handlers, a set of CPU registers, and stack state in the address space of its hosting process.
Multithreading is also known as threading.

Following are some of the common advantages of Multithreading:

  • Enhanced performance by decreased development time
  • Simplified and streamlined program coding
  • Improvised GUI responsiveness
  • Simultaneous and parallelized occurrence of tasks
  • Better use of cache storage by utilization of resources
  • Decreased cost of maintenance
  • Better use of CPU resource

Multithreading does not only provide you with benefits, it has its disadvantages too. Let us go through some common disadvantages:

  • Complex debugging and testing processes
  • Overhead switching of context
  • Increased potential for deadlock occurrence
  • Increased difficulty level in writing a program
  • Unpredictable results

Example

class demo extends Thread
{
 public void run()
 {
 for(int i=1;i<=10;i++)
 {
  System.out.println(i);
 }
 }
 }

class demo1 extends Thread
{
 public void run()
 {
 for(int i=10;i>=1;i--)
 {
  System.out.println(i);
 }
 }
 }

class mt
{
 public static void main(String args[])
 {
  demo d=new demo();
  Thread t=new Thread(d);
  t.setPriority(Thread.MIN_PRIORITY);
  demo1 d1=new demo1();
  Thread t1=new Thread(d1);
  t1.setPriority(Thread.MAX_PRIORITY);
  t1.start();
  t.start();
   }
}

Output

Comments

Popular posts from this blog

Hamming code in java

Why companies hiring Interns, build their BRAND faster?

Bully Algorithm in Java program