//How to Create Thread?
There are 2 ways:-
1. extends Thread Class
2. implements Runnable Interface(2nd approach is better then ist approch)
1. Through Thread Class.............................
package multithreading;
public class A extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Govind");
}
}
}
===============================================================================
package multithreading;
public class B extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("ballabh");
}
}
}
=============================================================================
package multithreading;
public class C extends Thread{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("khan");
}
}
}
================================================================================
package multithreading;
public class AbcMain {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
a.start();
b.start();
c.start();
}
}
===============================================================================
(Ist Time Run)
then o/p=
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
then o/p=
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
========================================================================
(3rd Time Run)
then o/p=
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
=======================================================================
2. Through Runnable interface..................................
package multithreading;
public class M implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("govind");
}
}
}
===============================================================================
package multithreading;
public class N implements Runnable {
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("ballabh");
}
}
}
=============================================================================
package multithreading;
public class O implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("khan");
}
}
}
==============================================================================
package multithreading;
public class MnoMain {
public static void main(String[] args) {
M m=new M();
N n=new N();
O o=new O();
Thread t1=new Thread(m);
Thread t2=new Thread(n);
Thread t3=new Thread(o);
t1.start();
t2.start();
t3.start();
}
}
==============================================================================
(Ist Time Run)
then o/p=
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
then o/p=
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
========================================================================
(3rd Time Run)
then o/p=
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
=======================================================================
There are 2 ways:-
1. extends Thread Class
2. implements Runnable Interface(2nd approach is better then ist approch)
1. Through Thread Class.............................
package multithreading;
public class A extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Govind");
}
}
}
===============================================================================
package multithreading;
public class B extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("ballabh");
}
}
}
=============================================================================
package multithreading;
public class C extends Thread{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("khan");
}
}
}
================================================================================
package multithreading;
public class AbcMain {
public static void main(String[] args) {
A a=new A();
B b=new B();
C c=new C();
a.start();
b.start();
c.start();
}
}
===============================================================================
(Ist Time Run)
then o/p=
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
=======================================================================
(2nd Time Run)then o/p=
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
========================================================================
(3rd Time Run)
then o/p=
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
=======================================================================
2. Through Runnable interface..................................
package multithreading;
public class M implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("govind");
}
}
}
===============================================================================
package multithreading;
public class N implements Runnable {
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("ballabh");
}
}
}
=============================================================================
package multithreading;
public class O implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++)
{
System.out.println("khan");
}
}
}
==============================================================================
package multithreading;
public class MnoMain {
public static void main(String[] args) {
M m=new M();
N n=new N();
O o=new O();
Thread t1=new Thread(m);
Thread t2=new Thread(n);
Thread t3=new Thread(o);
t1.start();
t2.start();
t3.start();
}
}
==============================================================================
(Ist Time Run)
then o/p=
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
=======================================================================
(2nd Time Run)then o/p=
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
========================================================================
(3rd Time Run)
then o/p=
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
ballabh
govind
govind
govind
govind
govind
govind
govind
govind
govind
govind
khan
khan
khan
khan
khan
khan
khan
khan
khan
khan
=======================================================================
No comments:
Post a Comment