Java / Android Developer, Trainer, Writer, Speaker

Wednesday, February 18, 2015

On 12:25 PM by Unknown in , ,    15 comments

Like the article?

Today, we are going to learn about Thread in JAVA.



simpleThread.java Class



 public class simpleThread implements Runnable {
 /*
  What is Thread in JAVA Programming? 
  Ans: Thread is a path of execution within a program. Multi Threading is multiple path
  of execution at the same time. Thread is a way, how your computer allows you to do
  multiple things at once. This is the simplest definition of thread.
   
   Now, how we will be working with Thread in practice?
   
   Step 1: Creating a thread object from thread class
   Step 2: Then we will start thread
   Step 3: Just to start, thread will look for RUN method.
   Step 4: While finds run method, the code into the RUN method
   will be executed.
  
   we will be implementing Runnable interface here.
  * */
 
 
 
 public static void main(String[] args) {

  System.out.println("Downloading two files..:");
  // we are creating a thread object below
  Thread t1 = new Thread() {
   /* 
    After t1.start , execution comes here to look for run
    Then the codes in run should be executed.
    
    */
   public void run() {
    for (int k = 0; k <= 20; k++) {
     System.out.println("File 1 downloaded " + k * 5 + "%");
    
     try {
      // Let the thread sleep for 1200 mili seconds.
      sleep(1200);
     } catch (InterruptedException threadExep) {
      threadExep.printStackTrace();
     }
    }

   }

  };
  // now, this will call run method
  t1.start();

  Thread t2 = new Thread() {
   public void run() {
    for (int k = 0; k <= 20; k++) {
     System.out.println("File 2 Downloaded " + k * 5 + "%");

     try {
      sleep(1000);
     } catch (InterruptedException threadExep) {
      threadExep.printStackTrace();
     }
    }

   }

  };

  t2.start();

  
  /*
   Using Runnable Interface(run method must be overridden):
   
   Step 1: We will Create an object of the class. Which is obj1 here.
   Step 2: Create a thread object and pass the class object through it
   Step 3: Start the thread, do operation.
   
   */
  simpleThread obj1 = new simpleThread();
  Thread t3 = new Thread(obj1);
  t3.start();
  
  
  /*
   
    This can be written as: 
    
   Thread t3 = new Thread(new simpleThread());
   t3.start();  
  
   * */
   
 }

 @Override
 public void run() {
  for (int p = 0; p <= 10; p++) {
   System.out.println("This is my T3 thread!");
  
  }
  
 }


}

/*
 
  What we are doing here:
  Creating couple of thread T1, T2. Then both starts and prints a for loop 
  while both sleeps for 1200 and 1000 mili seconds respectively.
 See the outputs!
 */




Output is:


Downloading two files..:
File 1 downloaded 0%
File 2 Downloaded 0%
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
This is my T3 thread!
File 2 Downloaded 5%
File 1 downloaded 5%
File 2 Downloaded 10%
File 1 downloaded 10%
File 2 Downloaded 15%
File 1 downloaded 15%
File 2 Downloaded 20%
File 1 downloaded 20%
File 2 Downloaded 25%
File 2 Downloaded 30%
File 1 downloaded 25%
File 2 Downloaded 35%
File 1 downloaded 30%
File 2 Downloaded 40%
File 1 downloaded 35%
File 2 Downloaded 45%
File 1 downloaded 40%
File 2 Downloaded 50%
File 1 downloaded 45%
File 2 Downloaded 55%
File 1 downloaded 50%
File 2 Downloaded 60%
File 2 Downloaded 65%
File 1 downloaded 55%
File 2 Downloaded 70%
File 1 downloaded 60%
File 2 Downloaded 75%
File 1 downloaded 65%
File 2 Downloaded 80%
File 1 downloaded 70%
File 2 Downloaded 85%
File 1 downloaded 75%
File 2 Downloaded 90%
File 2 Downloaded 95%
File 1 downloaded 80%
File 2 Downloaded 100%
File 1 downloaded 85%
File 1 downloaded 90%
File 1 downloaded 95%
File 1 downloaded 100%

15 comments:

  1. Brother, your site is very useful for us. I saved on my laptop for your site. I see every day. I have links to a site that is any help to you gently.
    http://bit.ly/1SnNeZY

    ReplyDelete
  2. A C hello world program executable will actually have compiled code of printf() function attached to it. That is not the case with System / PrintStream class in Java. Your program calls the method of the class provided by JVM. Your program just has the call, no callee is attached to your executable (class/jar) for System.out.println() method.

    java training in chennai

    ReplyDelete
  3. ya you have clearly explained the concept, so please let you share some information too.

    Web Design Company in Chennai

    ReplyDelete
  4. Just wanted to say thank you for the post. We are a small team and fairly new at this area so can use all the help we can get setting up best practices.
    Selenium Training in Chennai

    ReplyDelete
  5. Very nice. Thanks for posting, Icons are the must imported graphic in design world. i find some free icon here.

    New Zealand education Consultants in Chennai

    ReplyDelete
  6. i have clear about coding samples which helps to know more new ideas to improve the business engagements easily.
    Digital Marketing Company in Chennai

    ReplyDelete
  7. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
    Skilled Manpower Services in Chennai

    ReplyDelete
  8. I want talk to you sir.plz give your contact number

    ReplyDelete
  9. very very amazing explaintion....many things gather about muti thereding and important of therding...yes realy i enjoy it


    Digital Marketing company in Chennai

    ReplyDelete
  10. Really Good blog post about java threads.provided a helpful information.I hope that you will post more updates like this.
    Informatica Training in Chennai

    ReplyDelete
  11. this is very nice post thanks for updating your information to us.it is such a wonderful information about the software development and java is one of growing technology this is useful for me thank you so much.

    Hadoop Training in Chennai

    ReplyDelete