Methods in Thread Class

Let us first look into some of the important static methods in Thread Class

1.  public static native void sleep(long millis) throws InterruptedException:Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.InterruptedException is thrown if any thread has interrupted the current thread.

We have one more flavour for the sleep method:

public static void sleep(long millis, int nanos) throws InterruptedException:Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.InterruptedException is thrown if any thread has interrupted the current thread.

2. public static native void yield():A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this
hint.

it just releases the CPU hold by Thread to give another thread an opportunity to run though it’s not guaranteed who will get the CPU. It totally depends upon thread scheduler and it’s even possible that the thread which calls the yield() method gets the CPU again. Hence, it’s not reliable to depend upon yield() method, it’s just on best effort basis.

3. public static native Thread currentThread():Returns a reference to the currently executing thread object.

4. public static boolean interrupted():Tests whether the current thread has been interrupted.

Instance methods:

1. public synchronized void start():Invoking the start() method on a thread object executes the run() method as a new thread of execution.

2. public void run(): When you call the start method for a thread object, it will call a native code method that causes the OS to initiate another thread from which the run() method executes. We can call start method only once on a thread or it will throw IllegalStateException.

You can also call the run method directly, but if you call the run() method directly, it would simply operate like any other method and will run as part of the same thread that called it.

3. public final native boolean final isAlive():The final isAlive() method returns true if the thread is still running or the Thread has not terminated.

4. public final void join() throws InterruptedException:The final join() method waits until thread on which it is called is terminated. For example, thread1.join() suspends the current thread until thread1 dies.InterruptedException is thrown if any thread has interrupted the current thread.

We have two more flavours for the join method:

public final synchronized void join(long millis) throws InterruptedException:Waits at most millis milliseconds for this thread to die.

public final synchronized void join(long millis, int nanos) throws InterruptedException:Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.

5. public void interrupt(): A thread can signal another thread that it should stop executing by calling the interrupt() method for that thread. This doesn’t stop the thread, but sets a flag in the thread. This flag must be checked in the run() method to have any effect and the thread should then terminate itself.

6. public boolean isInterrupted():returns true if the interrupted flag has been set. This method does not reset the flag.

7. public int getPriority(): returns the priority of the thread.

8. public int setPriority(int priority): changes the priority of the thread.

9. public String getName(): returns the name of the thread.

10. public void setName(String name): changes the name of the thread.

11. public boolean isDaemon(): tests if the thread is a daemon thread.

12. public void setDaemon(boolean b): marks the thread as daemon or user thread.

Note:
yield does not have any synchronization semantic. If thread holds lock, it will continue to hold it. Only wait methods of the Object class release the intrinsic lockof the current instance (the thread may have other locks acquired, they don’t get released). Yield, sleep, join do not bother about locks.

Leave a Reply

Your email address will not be published. Required fields are marked *