Introduction to threads in Java

Discussion in 'Java' started by pradeep, Jul 31, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    A thread, by definition is a light weight process. They are used to increase functionality and performance by performing multiple tasks at the same time, i.e. concurrently. There are two methods for implementing threads in Java,

    • Implementing an interface
    • Extending a class
    I'd assume that the reader is familiar with the basic Object Oriented Paradigm concepts and understands terms like 'extending', 'interface' and 'class'. Now you may start wondering why are there two ways to create threads. This is because if a class is already an inherited class of some class other than 'Thread', then it cannot extend 'Thread' because multiple inheritance is not allowed in the Java programming language. So, in such cases we use 'Runnable' interface instead.

    Now, let's jump onto the coding part on how to actually create threads. The first method is to extend or inherit the 'Thread' class. The 'Thread' class is defined in the package java.lang, which needs to be imported. Take a look at the code below to get a better idea,

    Code:
     import java.lang.*;
      
     public class myExample extends Thread
      {
          public void run()
          {
          ....
          }
      }
      
    The other way to do this is to implement 'Runnable', as shown below,

    Code:
    import java.lang.*;
      public class myExample implements Runnable
      {
          Thread T;
          public void run()
          {
          ....
          }
      }
      
    Notice that both the methods make use of the 'run()' function, which is responsible for the actions of the thread. The 'Runnable' interface actually is nothing but a class containing only one abstract method, 'public abstract void run();'. Remember an interface only provides a design framework upon which classes can be implemented. It is also interesting to note that actually the 'Thread' class also implements the 'Runnable' interface.
     
  2. sendtonirav

    sendtonirav New Member

    Joined:
    Sep 10, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Below, it says <next thread> and <previous thread> !!
    Is it in any waiz related to the above topic ???????? ......

    Hey guys don't get furious on readin this !!! I m just tryin to be funny !!!!

    Any waiz, good work u all are doin !!! KEEP GOIN GUYS !!!
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    :p By the wat welcome to G4EF forum.
     
  4. haroras

    haroras New Member

    Joined:
    Aug 4, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi Pradeep ,
    Can you please illustrate this with greater detail .. taking examples on each of impl., that would help understand the concepts in a practical way ?
    thanks .. HS
     
  5. kell05

    kell05 New Member

    Joined:
    Aug 1, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    It is better in the long term to use the implements runnable interface as this will make your code more extendible in the future as you can also have threads that subclass other classes other than just Thread. What is not mentioned is that the thread has a shared state from the calling class which brings in the Synchronized keyword when changing the state.
     
  6. swaran

    swaran New Member

    Joined:
    Mar 7, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    hey pradeep don't you think that java.lang is provided by default....it doesn't need to be imported on any program....
     
  7. bapsbps

    bapsbps New Member

    Joined:
    Mar 31, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    In java we declare thread in two ways
    There are two methods for implementing threads in Java,

    • Implementing an interface
    • Extending a class
     
  8. frank239

    frank239 New Member

    Joined:
    Apr 23, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    there are java recomonded thread ... searching for good java script editing software.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice