Home Tutorial Java Core Java :Thread Methods

 
 

Java :Thread Methods
Posted on: October 17, 2012 at 12:00 AM
Advertisement
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.

static
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);
		System.out.println(thread.getName());
		/* Starting thread */
		thread.start();
	}

	public void run() {
		/* Display info about current thread */
		System.out.println(Thread.currentThread());
	}
}

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 */
		th1.start();
		th2.start();

		try {
			Thread.currentThread();
			/* delay for a second */
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		/* Display info about current thread */
		System.out.println(Thread.currentThread());
	}
}

Output :

thread3
Thread[thread3,5,main]
Thread[thread2,5,main]
Thread[thread1,5,main]
Thread[main,5,main]

Advertisement

Related Tags for Java :Thread Methods:


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.

Posted on: October 17, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com