Threading:-Thread
is a public class in java which internally implements Runnable interface,
Runnable interface having only one method that is run method. Purpose of thread
class and Runnable interface is to achieve multitasking programming, in a
normal programming we know the flow of program execution because normal methods
execute sequentially and this sequence is possible through stack all the
processes run in a common stack.
Thread is a lightweight process which is run in
separate stack space, so all the threads run in separate stack space
simultaneously that’s why threads run independently. If in a program more than
one thread executing this process called multi threading. Order of execution of
threads are depends on thread scheduler.
In short multi threading is many thread objects want
to execute same piece of code for example many people online book tickets here
peoples are thread object and tickets are that piece of code which is written
in run() method.
Threading
is achieved by two ways in a programming.
1) by extending Thread class.
2) by implementing Runnable interface
Example1:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
t1.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
12345678910
Example2:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
System.out.println(t1);
t1.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.print(i);
}
}
}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Thread[Thread-0,5,main]
12345678910
Example3:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
t1.setName("Amit");
System.out.println(t1);
t1.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.print(i);
}
}
}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Thread[Amit,5,main]
12345678910
Example4:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
t1.setPriority(1); //range of priority is b/w
1 to 10
t1.setName("Amit");
System.out.println(t1);
t1.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.print(i);
}
}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Thread[Amit,1,main]
12345678910
Example5:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
t1.setName("Amit");
TestThread t2=new TestThread();
t2.setName("Sumit");
t1.start();
t2.start();
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.print(i);
}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
1234512345
Example6:-
TestThread.java
class TestThread extends Thread
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
t1.setName("Amit");
TestThread t2=new TestThread();
t2.setName("Sumit");
t1.start();
t2.start();
}
public void run()
{
for(int i=1;i<=5;i++)
{
try
{
Thread.sleep(1500);
}catch(Exception e){}
System.out.println("by
"+Thread.currentThread().getName()+" i= "+i);
}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
by Sumit i= 1
by Amit i= 1
by Sumit i= 2
by Amit i= 2
by Amit i= 3
by Sumit i= 3
by Amit i= 4
by Sumit i= 4
by Amit i= 5
by Sumit i= 5
Example7:-
TestThread.java
class TestThread implements Runnable
{
int AVT=1,Req;
TestThread(int Req)
{
this.Req=Req;
}
public static void main(String[]args)
{
TestThread t=new TestThread(1);
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("Amit");
t2.setName("Sumit");
t1.start();
t2.start();
}
public void
run()
{
if(AVT>=Req)
{
System.out.println("Well-come
"+Thread.currentThread().getName());
try
{
Thread.sleep(1500);
}catch(Exception e){}
AVT=AVT-Req;
System.out.println(Req +"Ticket is allocated to
"+Thread.currentThread().getName());
}
else
{
System.out.println("Sorry ticket not
available");
}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Well-come Amit
Well-come Sumit
1Ticket is allocated to Sumit
1Ticket is allocated to Amit
Example8:-
TestThread.java
class TestThread implements Runnable
{
int AVT=1,Req;
TestThread(int Req)
{
this.Req=Req;
}
public static void main(String[]args)
{
TestThread t=new TestThread(1);
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("Amit");
t2.setName("Sumit");
t1.start();
t2.start();
}
synchronized public void
run()
{
if(AVT>=Req)
{
System.out.println("Well-come
"+Thread.currentThread().getName());
try
{
Thread.sleep(1500);
}catch(Exception e){}
AVT=AVT-Req;
System.out.println(Req +"Ticket is allocated to
"+Thread.currentThread().getName());
}
else
{
System.out.println("Sorry ticket not
available");
}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Well-come Amit
1Ticket is allocated to Amit
Sorry ticket not available
Example9:-
TestThread.java
class TestThread implements Runnable
{
int AVT=1,Req;
TestThread(int Req)
{
this.Req=Req;
}
public static void main(String[]args)
{
TestThread t=new TestThread(1);
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("Amit");
t2.setName("Sumit");
t1.start();
t2.start();
}
public void run()
{
System.out.println("Well-come
"+Thread.currentThread().getName());
synchronized(this)
{
if(AVT>=Req)
{
try
{
Thread.sleep(1500);
}catch(Exception e){}
AVT=AVT-Req;
System.out.println(Req +"Ticket is allocated to
"+Thread.currentThread().getName());
}
else{
System.out.println("Sorry ticket not
available");
}}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
Well-come Amit
Well-come Sumit
1Ticket is allocated to Amit
Sorry ticket not available
Example10:- TestThread.java
class TestThread implements Runnable
{
public static void main(String[]args)
{
TestThread t1=new TestThread();
Thread t2=new Thread(t1);
t2.setName("Amit");
TestThread t3=new TestThread();
Thread t4=new Thread(t3);
t4.setName("Sumit");
t4.start();
t2.start();
}
public void run()
{
for(int i=1;i<=5;i++)
{
try
{
Thread.sleep(1500);
}catch(Exception e){}
System.out.println("by
"+Thread.currentThread().getName()+" i= "+i);}}}
Output:-
C:\JAVATECH>javac TestThread.java
C:\JAVATECH>java TestThread
by Sumit i= 1
by Amit i= 1
by Sumit i= 2
by Amit i= 2
by Sumit i= 3
by Amit i= 3
by Amit i= 4
by Sumit i= 4
by Sumit i= 5
by Amit i= 5
Example11:-Chat.java
(Thread communication)
import java.util.*;
class Chat implements Runnable
{
String s="start";
Scanner sc=new Scanner(System.in);
synchronized void chatGarima()
{
while(!(s.equals("stop")))
{
if((s.equals("start")))
{
try
{
wait();
}
catch(Exception e)
{
}
}
System.out.println(Thread.currentThread().getName()+":"+s);
System.out.println(Thread.currentThread().getName()+":");
s=sc.nextLine();
notify();
try
{
wait();
}
catch(Exception e)
{
}
}
}
synchronized void chatRadha()
{
while(!(s.equals("stop")))
{
if((s.equals("start")))
{
System.out.println(Thread.currentThread().getName()+":");
s=sc.nextLine();
notify();
try
{
wait();
}
catch(Exception e)
{
}
}
System.out.println(Thread.currentThread().getName()+":"+s);
System.out.println(Thread.currentThread().getName()+":");
s=sc.nextLine();
notify();
try
{
wait();
}
catch(Exception e)
{
}
}
}
public void run()
{
if(Thread.currentThread().getName().equals("Garima"))
{
chatGarima();
}
else
{
chatRadha();
}
}
public static void main(String[]args)
{
Chat ct=new Chat();
Thread t1=new Thread(ct,"Garima");
Thread t2=new Thread(ct,"Radha");
t2.start();
t1.start();
}
}
Output:-
C:\JAVATECH>javac Chat.java
C:\JAVATECH>java Chat
Radha:
hello Garima
Garima:hello Garima
Garima:
hi Radha
Radha:hi Radha
Radha:
how r u
Garima:how r u
Garima:
i m fine
Radha:i m fine
Radha:
Comments
Post a Comment