Monday, 17 June 2013

multithreading

saturday we had our class from 9-12:30
we were taught  multithreding 
multithreading ----------it is a conceptual programing where a program is divided into two or more sub programing which can  be implemented at the same time in parallel
in most our computers we have single processor and therefore i reality the processor is doing only one thing at a time however the processor switches between the processes so fast that it appears us that all the processes are processing at the  same time
:::multithreading in java is done in two ways
*by using thread class
*by using runnable interface


some examples 
1)using thread class

class First extends Thread
{
public void run()
{
for (int i=0;i<5;i++)
{
System.out.println("value of i"+i);
}}}
class Second extends Thread
{
public void run()
{
for (int j=0;j<5;j++)
{
System.out.println("value of j"+j);
}}}
class Third extends Thread
{
public void run()
{
for (int k=0;k<5;k++)
{
System.out.println("value of k"+k);
}}}
class Threads
{
public static void main(String arg[])
{
First thread1=new First();
Second thread2=new Second();
Third thread3=new Third();
thread1.start();
thread2.start();
thread3.start();
}}




2)using runnable class

 class First implements Runnable
{
public void run()
{
for (int i=0;i<5;i++)
{
System.out.println("value of i "+i);
}}}
class Second implements Runnable
{
public void run()
{
for (int j=0;j<5;j++)
{
System.out.println("value of j"+j);
}}}
class Third implements Runnable
{
public void run()
{
for (int k=0;k<5;k++)
{
System.out.println("value of k "+k);
}}}
class Testrunnable
{
public static void main(String arg[])
{
First fobj=new First();
Second sobj=new Second();
Third tobj=new Third();
Thread thread1=new Thread(fobj);
Thread thread2=new Thread(sobj);
Thread thread3=new Thread(tobj);
thread1.start();
thread2.start();
thread3.start();
}}
 

:::life cycle of a thread
during the life time of a thread there are different states in which the thread passes
1)new born state
2)runnable state
3)running state
4)dead state


1)new born state--------when we create a new process it enters this state
2)runnable state --------it means the process is ready to use  and is waiting for resources
if two threads have equal priority then are given time slots
3)running state  --------running means that  the  processor has given its time to the thread   




after this we had a gd session and we discussed some topics and got  a lot to learn

No comments:

Post a Comment