Skip to main content

Threading

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

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of la...