Multithreading

PREVIOUS

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.

Advantage 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:
  • 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.

Note: At least one process is required for each thread.


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.
As shown in the above figure, thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.

Note: At a time one thread is executed only.

Life cycle of thread:

Every thread moves through several states from its creation to its termination.the possible states of thread are:
New->to create a thread
Start->thread class is executed
Ready->the thread is ready to execute
Running->now execute the program
Waiting->wait for more nano sec
Dead->when the thred complets its execution, it will move to the dead state
 

Multithreading:
**************
1. It is program control.In this the program is divided into two or more independent
    sub program called thread.
2. It will share the cpu timing
3. Os is responsible for scheduled and allocating resources for threads.

Advantangs of thread:
********************
1. Increase speed of executing
2. It allows running more tasks simultaneously
3. Reduce complexity of program
4. Maximize cpu utilization


Method:
********
1.run()

This is overriden in our thread extended from super class thread.
syntax:
******
public void run()
{
statement;
}

2.start()

To start the run() method.if is already started it throws illegal threadstateException
syntax:
******
obj.start();

3. sleep()
*********
Block the currently executing thread
syntax:
******
static void sleep(longint a)

4. join()
*******
To join the statement in the existing thread
syntax:
******
join()

5. stop()
********
Stop running thread
syntax:
******
obj.stop()



 
//single thread using Threads class
class x extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10; i++)
{
Thread.sleep(1000);
System.out.println("welcome");

}
}
catch(InterruptedException e)
{
}
}
}
class th1
{
public static void main(String sr[])
{
x  x1=new x();
x1.start();
}
}


//program to create a thread using the Thread class

class t2 extends Thread
{
double s;
 public void run()
{
try
{
for(int i=1;i<=10;i++)
{
Thread.sleep(1000);
s=Math.sqrt(i);

System.out.println("square root of  " +i + "is" +s);

}

}

catch(Exception e)
{}
}
}

class th2
{
public static void main(String ar[])
{

t2 t=new t2();
t.start();
}
}



 
//program to using runnable interface

class t3 implements Runnable
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
Thread.sleep(1000);
System.out.println("welcome to runnable");
}
}
catch(Exception e)
{}
}
}
class th3
{
public static void main(String ar[])
{

t3 t=new t3();
Thread j=new Thread(t);
j.start();
}
}


//multiple threads
class x extends Thread
{
public void run()
{
try
{          
for(int i=1;i<=3;i++)
{
Thread.sleep(1000);
System.out.println("wel");

}
}
catch(Exception e){}
}
}



class y extends Thread
{
public void run()
{
try
{
for(int i=1;i<=2;i++)
{
Thread.sleep(2000);
System.out.println("hello");

}
}
catch(Exception e)
{}
}
}

class multi
{
public static void main(String ar[])
{
x x1=new x();
y y1=new y();
x1.start();
y1.start();
}
}


// Prg using stop(), yield() methods:

class com extends Thread
{
public void run()
{
for(int i=1;i<=6;i++)
{
if(i==2)
stop();
System.out.println("Computer"+i);
}
}
}

class ma extends Thread
{
public void run()
{
for(int j=1;j<=6;j++)
{
if(j==3)
yield();
System.out.println("Modem"+j);
}
}
}
class ky extends Thread
{
public void run()
{
for(int k=1;k<=6;k++)
{
System.out.println("Keyboard"+k);
}
}
}

class st
{
public static void main(String ar[])
{
com c=new com();
ma c1=new ma();
ky c2=new ky();
c.start();
c1.start();
c2.start();
}
}


//using join() method

class j1 extends Thread
{
public void run()
{
try
{
for(int i=0;i<2;i++)
{
Thread.sleep(1000);
System.out.println("First thread");
}
}catch(Exception ex)
{
System.out.println("Exception");
}
}
}

class j2 extends Thread
{
public void run()
{
try
{
for(int i=0;i<5;i++)
{
Thread.sleep(2000);
System.out.println("second thread");
}
}catch(Exception ex)
{
System.out.println("Exception");
}
}
}

class jo
{
public static void main(String ar[])
{
j1 ss=new j1();
ss.start();
try
{
ss.join();
System.out.println("End of first");
}catch(Exception e)
{
System.out.println("Problem in thread");
}

j2 ss1=new j2();
ss1.start();
try
{
ss1.join();
System.out.println("End of second");
}catch(Exception e)
{
System.out.println("Problem in thread");
}
}
}

No comments:

Post a Comment