day 6

DAY 6
JAVA

Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
  • Process-based Multitasking(Multiprocessing)
  • Thread-based Multitasking(Multithreading)

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.

Java Thread class
Thread class is the main class on which java's multi threading system is based. Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.

MULTITHREADING:
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.

Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
Thread creation by extending the Thread class

We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.



class ExtendingThread extends Thread
{
String s[]={"Welcome","to","Java","Programming","Language"};
 public static void main(String args[])
ExtendingThread t=new ExtendingThread("Extending Thread Class"); 
 } 
 public ExtendingThread (String n)
{ super(n); 
   start(); 
 } 
 public void run() 
 { 
String name=getName(); 
  for(int i=0;i<s.length;i++)
{
 try { 
 sleep(500);
 } 
 catch(Exception e)  { 

 System.out.println(name+":"+s[i]); 

                          } 
                      } 
      } 
Thread creation by implementing the Runnable Interface
We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object.

class MyRunnableThread implements Runnable{
    public static int myCount = 0;   

 public MyRunnableThread(){             }    
public void run() {       
 while(MyRunnableThread.myCount <= 10)
{            try
{                
System.out.println("Expl Thread: "+(++MyRunnableThread.myCount));   
             Thread.sleep(100);            }
catch (InterruptedException iex)
{                System.out.println("Exception in thread: "+iex.getMessage()); 
           }       
 }    
} }
public class RunMyThread {  
  public static void main(String a[]){     
   System.out.println("Starting Main Thread...");        
MyRunnableThread mrt = new MyRunnableThread();        
Thread t = new Thread(mrt);       
 t.start();       
 while(MyRunnableThread.myCount <= 10)
{          
  try{               
 System.out.println("Main Thread: "+(++MyRunnableThread.myCount)); 
               Thread.sleep(100);            }
catch (InterruptedException iex){              
  System.out.println("Exception in main thread: "+iex.getMessage());
            }        }    
    System.out.println("End of Main Thread...");    }}



Thread Class vs Runnable Interface



1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.

2. We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.


Comments

Popular posts from this blog

Day 7

Industrial training in CDAC