wellnwill Marathahalli Bangalore

Multithreading

In general multitasking is the ability of an entity to perform multiple functionality at the same time.

Advantage of multitasking:--

Multitasking improves the efficiency of an application. In computer programing, multitasking is a ability of a processor to execute multiple functionalities concurrently.

Implementation of multitasking

There are two ways to implement the concept of multitasking.

  • By using multiprocessing
  • By using multithreading/li>

Multiprocessing

A process represent an individual functionality or set of functionality having it’s own memory address and CPU scheduling.

Multiprocessing represents concurrent execution of multiple process.

Thread

A thread represents an individual functionality of a process and each thread has its own independent path of execution. A thread is also called sub process.

Multithreading

Multithreading represents concurrent execution of multiple thread. Ex-MS word—spell checking(thread) , autocorrect, auto save etc, VLC(audio and video thread).

In java, the functionality of a thread is represented by an object of java.lang.Thread class.

Commonly used methods of Thread class

Public static Thread currentThread();
Public static void sleep(long ms) throws InterruptedException
Public void setPriority(int priority);
Public void getPriority();
Public void setName(String threadName);
Public String getName();
Public void start();
Public void run();
Public void setDaemon(boolean);
Public Boolean isDaemon();
Public static void yield();
class Test231
{
public static void main(String[] args)
{
Thread t=Thread.currentThread();
System.out.println("currrent thread is : "+t);//[main,5,main]
System.out.println("currrent thread priority is : "+t.getPriority());//5
System.out.println("currrent thread name is : "+t.getName());//main
}
}

Creation of child thread

By extending Thread class and override its run() method.

By implementing Runnable interface and override its run() method.

When we run java class, three threads are created by default.

  • Thread scheduler
  • Garbage collector thread
  • Main thread

Thread scheduler manages the CPU time for all the thread which are currently running. Garbage collector thread manages the memory.

Main thread executes the instruction written by programmer. We can develop our own user defined thread from main thread.

start() method

This method is defined in thread class and is responsible for the following tasks: It creates a new stack for newly created thread.

The thread moves from New state to the Runnable (ready to run) state. Executes run() method in this stack.

run() method

This method is responsible to execute the task of thread. The statements of run() method are executed with the statements of another run() method or main() method.

Commonly used constructors of thread class:

public Thread()
public Thread(String ThreadName)
public Thread(Runnable r)
public Thread(Runnable r, String ThreadName) 

Creation of child thread


By extending Thread class

  • Create a class that must extends thread class.
  • Override the run() method of Thread class to define the task of thread(it will be treated as separate thread execution).
  • Create the object of sub class of Thread.
  • Start the thread by calling start() method on the Thread object.

By implementing Runnable interface

  • Create a class that must implements Runnable interface.
  • Override the run() method of Runnable interface in child class(it will be treated as separate thread execution).
  • Create the object of child class.
  • Create the object of thread class by passing the object of Runnable interface implementing class i.e. child class object.
  • Start the thread by calling start method on the Thread object.
                                                       
class Thread1 extends Thread
{
public void run()
{
System.out.println("Thread executuion");
System.out.println("thread name:"+Thread.currentThread());
}
}
class ThreadTest12
{
public static void main(String args[])
{
 Thread1 t1=new Thread1();
 Thread1 t2=new Thread1();
 t1.start();
 t2.start();
}
}

class Thread1 extends Thread
{
public void run()
{
System.out.println("thread executuion");
System.out.println("thread name:"+Thread.currentThread());
}
}
class ThreadTest12
{
public static void main(String args[])
{
 Thread1 t1=new Thread1();
 t1.setName("Robert");
 Thread1 t2=new Thread1();
 t2.setName("Mac");
 t1.start();//t1.run();
 t2.start();
}
}
Footer with Map