Multithreading Example In Java
Multithreading is another important part of Java. Multi-threading is a process of executing multiple threads in Java Programming Languages. Multi-threading also knows as Thread - based Multitasking.
Before we are going to make Thread or Multi-Threading Program in Java, click on the Advertisement, available on this page. This is motivate me for develop projects or simple example in Java Programming Languages.
Now, Let's Start
"Threadtest.java"
public class Threadtest implements Runnable{
public Threadtest()
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
try{
for(int i=5;i>=1;i--)
{
System.out.println("Child thread");
Thread.sleep(500);
}
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
}
public static void main(String arg[])
{
Threadtest ob=new Threadtest();
try{
for(int i=5;i>=1;i--)
{
System.out.println("Main thread");
Thread.sleep(1000);
}
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
}
}
"Threadtest1.java"
public class Threadtest1 extends Thread{
public Threadtest1()
{
super();
start();
}
public void run()
{
try{
for(int i=5;i>=1;i--)
{
System.out.println("Child thread"+i);
Thread.sleep(500);
}
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
}
public static void main(String arg[])
{
Threadtest1 ob=new Threadtest1();
try{
for(int i=5;i>=1;i--)
{
System.out.println("Main thread"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException ex)
{
System.out.println(ex);
}
}
}
"Threaddemo.java"
public class Threadtdemo extends Thread
{
public Threadtdemo()
{
//
Thread t = new Thread(this);
super();
start();
}
public void run()
{
try
{
for(int i=10;i>=0;i--)
{
System.out.println("Child Thread"+i);
Thread.sleep(500);
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
Threadtdemo ob = new Threadtdemo();
Threadtdemo ob1 = new Threadtdemo();
try
{
for(int i=10;i>=0;i--)
{
System.out.println("Main Thread" + i);
Thread.sleep(1000);
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}
Compile And Run On this Programs for Better understanding Multi-Threading programming in Java, If you face any error, so feel free to comment below...
If this information is important to you , and you want to , I will continue writing some more details of Android Programming Language, so click on the advertising, available on this page. This is motivate me for writing some more useful stuff, Thank's
Comments
Post a Comment