KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > Runnable

java.lang
Interface Runnable

All Known Implementing Classes:
AsyncBoxView.ChildState, FutureTask, RenderableImageProducer, Thread, TimerTask
See Also:
Top Examples, Source Code

void run()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[209]GOF:implement the Singleton pattern
By Anonymous on 2005/01/19 10:48:25  Rate
// implement the Singleton pattern 
  
  
 public class Singleton  {  
     private static final Singleton INSTANCE =  
                               new Singleton (  ) ; 
  
  
    // Private constructor supresses  
    // default public constructor 
     private Singleton (   )   {  
      }  
  
  
     public static Singleton getInstance (   )   {  
         return INSTANCE; 
      }  
   }  
  
  
  
 //run


[400]Runnable example
By Dileep on 2005/03/07 11:58:10  Rate
public class RunnableExample implements java.lang.Runnable  {  
     public void RunnableExample (  )   {  
      }  
      
     public void run (  )   {  
            for  ( int i = 0; i  <  4; i++ )   {  
                 System.out.println ( i ) ;  
                 try  {  
                     Thread.sleep (  ( long )  ( Math.random (  )  * 1000 )  ) ; 
                  }  catch  ( InterruptedException e )   {  }  
              }  
      }  
      
     public static void main ( String [  ]  args )   {  
         Thread mythread = new Thread ( new RunnableExample (  ) , "RunnableExample" ) ; 
         mythread.start (  ) ; 
      }  
  } 


[1138]It is always better to implement a Runnable than to extend a Thread
By Anonymous on 2004/11/23 09:00:51  Rate
It is always better to implement a Runnable than to extend a Thread. 
  
  
 Why ? 
  
  
 1. separation of functionalities : you do not want to extend Thread 
 functionalities, but just use Thread as a means of executing them; 
 2. once a Thread is initiated, it must be started somewhere, sometime. 
 It does not get gc'd before start has been called. Since runnable is  
 an interface, no problems there. 
 3. except for techn. classes, most of the time you are implementing 
 business functionality and then inherentence is one of the benefits of 
 oo you do not want to loose. Implementing an interface keeps that  
 option open. 
  
  
 Most of the times, you can't extend Thread, since your class is  
 already extending some other class, the Applet, for example. 
  
  
 One disadvantage of implementing Runnable is that your run method must 
 then be public, while the run method on an extended Thread nead only  
 be protected.


[1352]_
By nnz on 2005/03/17 09:13:06  Rate
package com.wincor.caronte.exm; 
  
  
 /** 
  *  
  * @author Nunzio Fiore 
  * @version 0.1 
  */
 
 public class RunnableExample implements Runnable 
  {  
    
    
   public RunnableExample (  )  
    {  
    }  
    
   public void run (  )  
    {  
     for  ( int i = 0; i  <  4; i++ )   
      {  
       System.out.println ( "I'm "+ Thread.currentThread (  ) .getName (  )  +" and I'm printing this number: "+ i ) ; 
       try 
        {   
         System.out.println ( "I'm "+ Thread.currentThread (  ) .getName (  )  +" and I'm waiting for : "+Math.random (  ) *1000 ) ; 
         Thread.sleep (  ( long )  ( Math.random (  ) *1000 )  ) ; 
        } catch  ( InterruptedException e )  
        {  
          
        }  
      }  
    }  
    
   public static void main ( String [  ]  args )   
    {  
      
     for  ( int i = 0; i  <  4; i++ )   
      {  
     Thread mythread = new Thread ( new RunnableExample (  ) , "threadNum"+i ) ; 
     mythread.start (  ) ;   
     //System.out.println ( "threadNum"+i+" thread started" ) ; 
      }  
     }  
  } 


[1672]_
By Anonymous on 2005/11/04 20:23:08  Rate
The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritances. The only interface can help.
Popular Tags