Login:  Password:    
forgot my password
sign up!
Search: 

How to Make Threads with Java

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

1 0

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


Anurag Jain
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
Add your comment
[Fechar]

Este post é fechado - você precisa ter acesso ao post para incluir um comentário.


no comments have been posted - be the first!
Help us to improve! Give us your feedback:

Give your note to this post: 1 2 3 4 5 6 7 8 9 10
Is this post helpful? Yes No



[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








mrbool.com
contact us   |   publish your post   |   buy credits

Copyright 2013 - all rights reserved to www.web-03.net