Advertisement



< Prev
Next >


Unsycnhronized run of multiple threads




A multithreaded Java program contains multiple threads running concurrently within a program, where each thread is performing some specific task. In this article we are going to teach you how to run multiple threads in a Java program.




Unsynchronized run of multiple threads, each thread based on one object


We are going to create three threads based on an the same object of a class that has implemented Runnable interface. Method start() on each thread will be called one by one. Each thread will run only for a short while, before it is replaced by another thread, in the middle of its execution, this is called unsychronized run of multiple threads
//class A implementing Runnable interface
  
class A implements Runnable
{

public void run()
{
for(int i=0;i<3;i++)
System.out.println(Thread.currentThread().getName()); //accessing the reference to the currently										
}						      //executing thread by calling static method
						      //currentThread() of Thread class.
						      


public static void main(String... ar)
{
A ob= new A();

Thread t1= new Thread(ob,"Thread1");
Thread t2= new Thread(ob,"Thread2");
Thread t3= new Thread(ob,"Thread3");	    //Calling Thread's constructor & passing the object
					    //of A class that  implemented  Runnable interface
					    //& the name of new thread.

						
t1.start(); //Starting the first thread
t2.start(); //Starting the second thread
t3.start(); //Starting the third thread
}
}




Output is -


Thread1
Thread3
Thread3
Thread3
Thread1
Thread1
Thread2
Thread2
Thread2


Program Analysis



To see the synchronized run of this program, please read our next article.


Advertisement




Unsynchronized run of multiple threads, each thread based on a different object.


In this program we are going to create three threads based on a different object of a class that has implemented Runnable interface. In the run() method, each thread goes to sleep for 1 second and at that time, it is replaced by another thread, which starts executing the run() method.
//class A implementing Runnable interface

class A implements Runnable
{

public void run()
{
for( int i=0;i<3;i++)
try
{
	Thread.sleep(1000);
	System.out.println(Thread.currentThread().getName());//accessing the reference to the 								
}						 //currently executing thread by calling 
}						 //static method currentThread() of Thread class.

catch(Exception e)
{
System.out.println(e);
}

}


public static void main(String... ar)
{
A ob1= new A();
A ob2= new A();
A ob3= new A();


Thread t1= new Thread(ob1,"ThreadA");
Thread t2= new Thread(ob2,"ThreadB");
Thread t3= new Thread(ob3,"ThreadC");	    //Calling Thread's constructor & passing the object
					    //of A class that  implemented  Runnable interface
					    //& the name of new thread.

t1.start(); //Starting the first thread
t2.start(); //Starting the second thread
t3.start(); //Starting the third thread

}
}






Output is -:


ThreadA
ThreadB
ThreadC
ThreadB
ThreadC
ThreadA
ThreadC
ThreadB
ThreadA


Program Analysis



To see the synchronized run of this program, please read the next article



Please share this article -





< Prev
Next >
< Synchronizing a block
Synchronized multiple threads >



Advertisement

Please Subscribe





Please subscribe our social media channels for notifications, we post a new article everyday.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement