Share on Google+Share on Google+

Java :Thread Methods

This section explains methods of Thread class.

Java :Thread Methods

This section explains methods of Thread class.

Thread Methods :

Thread class provides many method to handle different thread operations. Here we are defining some of them -

static int activeCount() - It returns number of active threads in your current thread group.

Thread currentThread() - It returns reference of the current executing object.

String getName() - t returns name of the current thread.

int getPriority() - It returns the priority of the thread.

boolean isDaemon() - it checks that the thread is a daemon thread or not.

run() : If we are constructing thread by using a separate Runnable run object, then run method of that Runnable object is called, otherwise, this method does nothing and returns.

setName(String name) - this method is used when you want to change name of your thread.

setPriority(int newPriority) - This method is used to set the priorty of the current thread.

sleep(long millis) - It puts the current executing thread to sleep for a given number of milliseconds.

start() - It starts the execution of the thread.JVM calls the run method of your  thread.

yield() - This method pauses and allow other threads the temporal executing thread object.

Some other methods are - checkAccess(), countStackFrames(),,destroy(), dumpStack(), enumerate(Thread[] tarray), getAllStackTraces()
getContextClassLoader(), getDefaultUncaughtExceptionHandler(), getId(), getStackTrace(), getState(), getThreadGroup(), getUncaughtExceptionHandler(),  holdsLock(Object obj), interrupt(), interrupted(), isAlive(), isInterrupted(), join(), join(long millis),
join(long millis, int nanos), resume(),
setContextClassLoader(ClassLoader cl), setDaemon(boolean on),
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh),
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh), sleep(long millis, int nanos), stop(), stop(Throwable obj), suspend() , toString().

Example :

class RunnableThread implements Runnable {

	Thread thread;

	public RunnableThread() {

	public RunnableThread(String threadName) {
		/* Creating new thread */
		thread = new Thread(this, threadName);
		/* Starting thread */

	public void run() {
		/* Display info about current thread */

public class RunnableThreadExample {

	public static void main(String[] args) {
		Thread th1 = new Thread(new RunnableThread(), "thread1");
		Thread th2 = new Thread(new RunnableThread(), "thread2");
		RunnableThread th3 = new RunnableThread("thread3");
		/* Threads start */

		try {
			/* delay for a second */
		} catch (InterruptedException e) {
		/* Display info about current thread */

Output :



Posted on: October 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.