Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to Make Threads with Java

In this article you will see how to make threads with java.

All the 3 methods to make threads have been discussed. The main benefit of thread is it can run even if your method has completed its function. It can be useful in a number of situations like you want to perform 2 operation at similar time etc.

Threads actually run even if your main function has ended .It run independent of others.You are going to require threads in your day to day java programs.

Listing 1: Define Class and the main method

public class Threads 
{
public static void main(String args[])
{
	MainThread t=new MainThread();
	t.start();
	
	Thread t1=new Thread(new Runn());
	t1.start();
	
	Thread t2 = new Thread(){
	    public void run(){
	      System.out.println("Anonymous");  
	    }
	  };
	  t2.start();
}
}
 

Here,

  1. We made a class named Thread.
  2. Now we discuss the first method of defining thread.
  3. We make an object of a class called MainThread(We will define it later) .This MainThread class extends the thread functionality
  4. Now we will start the thread by calling the start method. When we call the start method on thread then the run method of the MainThread is called and executed
  5. Now we discuss the second method of defining thread.
  6. We define a class call Runn which will implement the Runnable interface and override its run method(We will define it later).Now we make an object of thread and pass the Runn class which implemented the Runnable interface.
  7. Now when we call the start method then the run method of Runn class gets called and is executed
  8. Now we discuss the third method of defining thread.
  9. Here we define an anonymous thread i.e. we give thread definition along with declaration.
  10. Here we define a thread and give the definition of the run method there only.
  11. Now we call the start method of this anonymous thread so that the run method gets called

Now we define the MainThread class

Listing 2: MainThread class

class MainThread extends Thread
{
	public void run()
	{
		System.out.println("Hi you reached MainThread");
	}
}

Here,

  1. We made a class named MainThread and we extend the thread class so as to avail the thread facility.
  2. Now we will define the run method and we put a simple message so that when this thread run then the message is displayed
  3. Now we define the Runn class

    Listing 3: Runn class

    class Runn implements Runnable
    {
    
    	public void run() 
    	{
    		System.out.println("Runnable interface");
    	}
    	
    } 

    Here,

    1. We made a class named Runn and we implement the Runnable interface so as to avail the thread facility.
    2. Now we will define the run method and we put a simple message so that when this thread run then the message is displayed

    Listing 4: Full Source code

    class MainThread extends Thread
    {
    	public void run()
    	{
    		System.out.println("Hi you reached MainThread");
    	}
    }
    
    class Runn implements Runnable
    {
    
    	public void run() 
    	{
    		System.out.println("Runnable interface");
    	}
    	
    }
    public class Threads 
    {
    public static void main(String args[])
    {
    	MainThread t=new MainThread();
    	t.start();
    	
    	Thread t1=new Thread(new Runn());
    	t1.start();
    	
    	Thread t2 = new Thread(){
    	    public void run(){
    	      System.out.println("Anonymous");  
    	    }
    	  };
    	  t2.start();
    }
    }
    
     

    Output

    Hi you reached MainThread
    Runnable interface
    Anonymous



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login