KickJava   Java API By Example, From Geeks To Geeks.

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

java.lang
Class Thread

java.lang.Object
  extended by java.lang.Thread
All Implemented Interfaces:
Runnable
See Also:
Top Examples, Source Code

public static int activeCount()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[178]A more elegant solution for locking performance
By Anonymous on 2003/03/14 08:58:38  Rate
//Plain synchronized methods create no problem With                                         
 //excessive locking. The object's memory layout is  
 //more complex, so construction and garbage collection  
 //are slower, extra memory is wasted with object headers  
 //and pointers, and additional method invocations  
 //are necessary  ( although inlining usually removes these ) . 
  
  
   
 import java.util.Date; 
  
  
    /** 
    * Illustrates a more elegant solution  
    * for locking performance, through  
    * additional encapsulation of the 
    * object model. activeCount 
    */
 
 public class Person2 
  {  
    private final PersonNames names =  
       new PersonNames (  ) ; 
    private final PersonDates dates =  
       new PersonDates (  ) ; 
  
  
    /** 
    * No locking here anymore. 
    */
 
    public void setNames  (  
       String name, String surname )  
     {  
       names.set ( name, surname ) ; 
     }  
  
  
    /** 
    * No locking here anymore. 
    */
 
    public void setDates  ( Date birth, Date death )  
     {  
       dates.set ( birth, death ) ; 
     }  
  }  
  
  
    /** 
    * Encapsulates the name information. 
    */
 
 class PersonNames 
  {  
    private String name, surname; 
  
  
    /** 
    * Now we can just synchronize the method. 
    */
 
    public synchronized void set  (  
       String name, String surname )  
     {  
       this.name = name; 
       this.surname = surname; 
     }  
  }  
  
  
    /** 
    * Encapsulates the date information. 
    */
 
 class PersonDates 
  {  
    private Date birth, death; 
     
    /** 
    * Now we can just synchronize the method. 
    */
 
    public synchronized void set  (  
       Date birth, Date death )  
     {  
       this.birth = birth; 
       this.death = death; 
     }  
  }  
  
  
 


public final void checkAccess()
See Also:
SecurityManager.checkAccess(java.lang.Thread), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public int countStackFrames()
See Also:
IllegalThreadStateException, suspend()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Thread currentThread()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public void destroy()
See Also:
NoSuchMethodError, suspend()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void dumpStack()
See Also:
Throwable.printStackTrace()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int enumerate(Thread[] tarray)
See Also:
SecurityManager.checkAccess(java.lang.ThreadGroup), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Map<Thread,StackTraceElement[]> getAllStackTraces()
See Also:
Throwable.getStackTrace(), RuntimePermission, SecurityManager.checkPermission(java.security.Permission), getStackTrace(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ClassLoader getContextClassLoader()
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), setContextClassLoader(java.lang.ClassLoader), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[662]Better to use the classloader associated with the current thread
By Anonymous on 2004/02/15 12:45:20  Rate
ClassLoader cl = Thread.currentThread (  ) .getContextClassLoader (  ) ; 
 cl.loadClass  ( "myClass" ) ;


[927]_
By Anonymous on 2004/09/25 22:11:33  Rate
It is better to do: 
  
  
  Thread.getContextClassLoader (  ) .loadClass ( "Foo" ) ; 
   
 Than: 
   
  Class.forName ( "foo" ) ; 
  
  
 This is why, 
  
  
 Class.forName will use the ClassLoader for the current class. Whereas  
 the other uses the classloader associated with the current thread. 
  
  
 The problem is that you usually want to load classes in the thread's  
 classloader because it "knows what's going on" better. 
  
  
 For example, you write code in a jar and put it in the app server's  
 shared library location. You have a web application that calls into it  
 with the name of a class you want to load that is defined in a jar in  
 WEB-INF/lib. If you use Class.forName, it will fail, whereas with  
 Thread one should succeed. 
  
  
 The same thing potentially could happen with shared jars in an ear and other situations. 
 


public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
See Also:
setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long getId()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final String getName()
See Also:
setName(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int getPriority()
See Also:
setPriority(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public StackTraceElement[] getStackTrace()
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread.State getState()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final ThreadGroup getThreadGroup()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static boolean holdsLock(Object obj)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[521]Assertion Examples
By Anonymous on 2003/11/17 09:46:11  Rate
//Assertion Examples 
  
  
 // Ensure we hold a lock 
 assert Thread.holdsLock ( this ) ; 
 switch ( x )   {  
     case 1: 
         // Do something 
         break; 
     case 2: 
         // Do something 
         break; 
     default: // x must be 1 or 2 
         assert false; 
  }  
  
  
 // Display message on console 
 assert x == 10:"Value of x not 10";


[909]Synchronized between two different methods
By Anonymous on 2004/09/20 11:00:15  Rate
public class foo  {  
   
     // method1 (  )  and method2 (  )  need to be synchronized 
     private final Object METHOD_LOCK = new Object (  ) ; 
   
   
     public void method1 (  )   {  
         synchronized  ( METHOD_LOCK )   {  
             ... 
          }  
      }  
   
     public void method2 (  )   {  
         synchronized  ( METHOD_LOCK )   {  
             ... 
          }  
      }  
  } 


public void interrupt()
See Also:
SecurityException, wakeup, ClosedByInterruptException, interruptible channel, InterruptedException, sleep(long, int), sleep(long), join(long, int), join(long), join(), Object, wait(long, int), wait(long), wait(), checkAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static boolean interrupted()
See Also:
isInterrupted()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean isAlive()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean isDaemon()
See Also:
setDaemon(boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isInterrupted()
See Also:
interrupted()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void join()
                throws InterruptedException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1504]Joining two threads
By mohammed_coleman { at } hotmail { dot } com on 2005/08/02 04:00:24  Rate
public class TestRunner1 implements Runnable 
  {  
   public void run (  )  
    {  
     try 
      {   
       for  ( int i = 0; i  <  10; i++ )  
        {  
          System.out.println ( i ) ; 
          Thread.sleep ( 1000 ) ; 
        }  
      }  
     catch  ( InterruptedException iex )  
      {  
       System.out.println ( "SOMEONE INTERRUPTED MY SLEEP AARRGGHH!!!" ) ; 
      }  
    }  
  }  
  
  
  
 public class TestRunner2 implements Runnable 
  {  
  private Thread t; 
  public TestRunner2 (  Thread t  )  
   {  
   this.t = t; 
   }  
  public void run (  ) ; 
   {  
   try 
    {  
     t.join (  ) ; //will start after the end of t 
     for  ( int i = 0; i  <  10; i++ )  
      {  
       System.out.println ( i ) ; 
       Thread.sleep ( 1000 ) ; 
      }  
    }  
   catch  ( InterruptedException iex )  
    {  
     System.out.println ( "Sleep interrupted" ) ; 
    }  
   }  
  }  
  
  
  
 public class TestJoinMethod 
  {   
   public static void main  ( String [  ]  args )  
    {  
    TestRunner1 r1 = new TestRunner (  ) ; 
    Thread t1 = new Thread ( r1 ) ; 
    TestRunner2 r2 = new TestRunner2 ( t1 ) ; 
    Thread t2 = new Thread ( r2 ) ; 
    t1.start (  ) ; 
    t2.start (  ) ; 
    }  
  } 


public final void join(long millis)
                throws InterruptedException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void join(long millis,
                       int nanos)
                throws InterruptedException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1752]How can you pass data between two threads?
By Anonymous on 2006/05/04 15:41:29  Rate
I can think of 3 ways  
  
  
 1. Using join (  )  method we can get data from one thread to other thread.   
 2. Declare global structures  ( both threads can access it  )  and protect the access via semaphores so that there would'nt be any synchronization issues. 
 3. Have u heard of somethng called PIPEs in UNIX ? Pipes are typically used in threaded programs. In Java the combination of PipedInputStream and PipedOutputtStream closely resembles a UNIX system "pipe." The purpose of these "pipedstream" classes is to allow the program to read data into an input stream and write it out through an output stream in a single method call.


public static final int MAX_PRIORITY
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int MIN_PRIORITY
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int NORM_PRIORITY
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final void resume()
See Also:
suspend(), checkAccess(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void run()
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String), stop(), start(), Runnable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[99]This example shows that a lock on an object also locks all access to the object via synchronized methods
By Anonymous on 2002/10/17 18:06:29  Rate
//This example shows that a lock on an object also locks all access to the object via synchronized methods.  
  
  
 public class Test   {  
    public static void main (  String args [  ]   )  throws Exception  {  
       LockTest aLock = new LockTest (  ) ; 
       TryLock tester = new TryLock (  aLock  ) ; 
       tester.start (  ) ; 
        
       synchronized  (  aLock  )   {  
          System.out.println (  "In Block" ) ; 
          Thread.currentThread (  ) .sleep (  5000 ) ; 
          System.out.println (  "End Block" ) ; 
        }  
     }  
  }  
 class TryLock extends Thread  {  
    private LockTest myLock; 
     
    public TryLock (  LockTest aLock  )   {  
       myLock = aLock; 
     }  
    public void run (  )     {  
       System.out.println (  "Start run" ) ; 
       myLock.enter (  ) ; 
       System.out.println (  "End run" ) ; 
     }  
  }  
 class LockTest  {  
    public synchronized void enter (  )   {  
       System.out.println (  "In enter" ) ; 
     }  
  } 


public void setContextClassLoader(ClassLoader cl)
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), getContextClassLoader(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[842]Workaround for thread class loader problem
By Irina Sjogren is55 { at } hotmail { dot } com on 2004/07/21 22:50:30  Rate
Workaround for the problem: thread is created in web application's class and needs to call methods in several web application classes. Problem is that newly started thread is loaded using its own classloader, therefore when thread tries to execute any methods in web app classes, ClassNotFound exception is thrown.  
  
  
 Workaround: set thread's ContextClassLoader explicitly before starting thread:  
  
  
  
     public void startThread (   )    {  
         if (  mRunner == null  )  {  
             mRunner = new Thread (  this  ) ; 
             ClassLoader cl = this.getClass (  ) .getClassLoader (  ) ; 
             mRunner.setContextClassLoader (  cl  ) ; 
             mRunner.start (  ) ; 
          }   
      }  
 


public final void setDaemon(boolean on)
See Also:
checkAccess(), isDaemon(), SecurityException, IllegalThreadStateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
See Also:
ThreadGroup.uncaughtException(java.lang.Thread, java.lang.Throwable), getUncaughtExceptionHandler(), setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler), RuntimePermission, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setName(String name)
See Also:
getName(), checkAccess(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setPriority(int newPriority)
See Also:
ThreadGroup.getMaxPriority(), MIN_PRIORITY, MAX_PRIORITY, getThreadGroup(), getPriority(), checkAccess(), SecurityException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
See Also:
ThreadGroup.uncaughtException(java.lang.Thread, java.lang.Throwable), setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void sleep(long millis)
                  throws InterruptedException
See Also:
Object.notify()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void sleep(long millis,
                         int nanos)
                  throws InterruptedException
See Also:
Object.notify(), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1502]Thread interuption
By mohammed_coleman { at } hotmail { dot } com on 2005/08/02 03:50:01  Rate
public void run (  )  
  {  
  try 
   {  
    for  ( int i = 0; i  <  10; i++ )  
     {  
     System.out.println ( i ) ; 
     Thread.sleep ( 1000 ) ; 
     }  
   }  
  catch  ( InterruptedException iex )  
   {  
   System.out.println ( "OI! Someone interrupted my sleep!!!" ) ; 
   }  
  }  
 


public void start()
See Also:
stop(), run(), IllegalThreadStateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final void stop()
See Also:
SecurityManager.checkPermission(java.security.Permission), SecurityManager.checkAccess(Thread), ThreadGroup.uncaughtException(java.lang.Thread, java.lang.Throwable), ThreadDeath, start(), run(), checkAccess(), interrupt(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final void stop(Throwable obj)
See Also:
SecurityManager.checkPermission(java.security.Permission), SecurityManager.checkAccess(Thread), stop(), start(), run(), checkAccess(), interrupt(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final void suspend()
See Also:
checkAccess(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread()
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(Runnable target)
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1503]Test runnable
By mohammed_coleman { at } hotmail { dot } com on 2005/08/02 03:52:40  Rate
public class TestRunner implements Runnable 
  {  
  public void run (  )  
   {  
   try 
    {  
     for  ( int i = 0; i  <  10; i++ )  
      {   
      System.out.println ( i ) ; 
      Thread.sleep ( 1000 ) ; 
      }   
    }  
   catch  ( IntteruptedException iex )  
    {  
    System.out.println ( "How dare someone interrupt my sleep!!!" ) ; 
    }  
   }  
  }  
  
  
  
 public class TestThread 
  {  
  public static void main  ( String [  ]  args )  
   {  
   TestRunner tRunner = new TestRunner (  ) ; 
   Thread t1= new Thread ( tRunner ) ; 
   t1.start (  ) ; 
   }  
  } 


public Thread(Runnable target,
              String name)
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(String name)
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(ThreadGroup group,
              Runnable target)
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(ThreadGroup group,
              Runnable target,
              String name)
See Also:
SecurityManager.checkAccess(java.lang.Thread), ThreadGroup.checkAccess(), setPriority(int), setDaemon(boolean), run(), Runnable.run(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(ThreadGroup group,
              Runnable target,
              String name,
              long stackSize)
See Also:
SecurityException, OutOfMemoryError, StackOverflowError, Thread(ThreadGroup,Runnable,String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Thread(ThreadGroup group,
              String name)
See Also:
Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


[1505]Test thread yield
By mohammed_coleman { at } hotmail { dot } com on 2005/08/02 04:30:10  Rate
public class TestRunner implements Runnable 
  {  
   public void run (  )  
    {  
    System.out.println ( Thread.currentThread (  ) .getName (  )  + " In run" ) ; 
    Thread.yield (  ) ; 
    System.out.println ( Thread.currentThread (  ) .getName (  )  + " Leaving run" ) ; 
    }  
  }  
  
  
  
 public class TestYield  
  {  
  public static void main  ( String [  ]  args )  
   {   
   TestRunner r1 = new TestRunner (  ) ; 
   Thread t2 = new Thread ( r1 ) ; 
   t2.setName ( "BlahBlah" ) ; 
   Thread t1 = new Thread ( r1 ) ; 
   t1.setName ( "TestThreadHaah" ) ; 
   t1.start (  ) ; 
   t2.start (  ) ; 
   }  
  
  
  } 

Popular Tags