The Java Thread class is one of the most important classes in Java. The Thread class has various static methods. These methods affect the currently running thread.
Runnable interface
Java provides a standard interface for threads called Runnable. This interface is implemented by the Thread class. Any subclass must override the run() method to create an instance. When a thread starts, its name will be printed out, if it has one. If not, a random name will be assigned to it.
In addition to providing an abstract base class, the Runnable interface provides a set of generalized rules for active objects. The most common use case is to override the run method. The run() method in an object implementing the Runnable interface will invoke the thread in a separate thread. This is similar to creating a named class.
The main advantage of the runnable interface over the abstract Thread class is that it allows multiple threads to share objects. This makes it easier to share resources. This also means that a class extending Runnable can have less memory than a Thread class.
Life cycle of a java thread
Java threads can transition to different states throughout its life cycle. They can be “new,” “runnable,” “waiting,” and “unblocking.” When a thread is runnable, it is active, and is executing code. When it is in the “waiting” state, it is waiting for another thread to finish executing.
Start() method
A Java thread may go through a series of states during its lifetime. Each of these states may be changed by a method call. This state diagram shows the different stages of a thread’s life. Each state represents one of the main aspects of the thread’s life.
Java threads are lightweight processes that share the resources of a process. A Java application may contain several java threads, each with its own Start() method. Every java application is a multithreaded application, which means that multiple threads are running in the background. The Java run-time system depends on threads to reduce inefficiency and waste of CPU cycles.
package com.theprogramguide; public class ThreadDemo1 extends Thread { public void run( ) { } public static void main(String[ ] args) { ThreadDemo1 thread1 = new ThreadDemo1( ); thread1.start( ); } }
Stop() method
The Stop() method for a java thread is used to terminate a running thread. Often used in applets, it kills a thread asynchronously. For example, an applet can be told to stop running when the browser changes pages. However, the Stop() method for a java thread can sometimes cause an IllegalThreadStateException when it’s used improperly.
yield() method
A thread yield method suspends the executing thread while it waits for another thread to complete its task. It also gives other threads a chance to finish their work, by giving them the CPU. It also puts the current thread back into the processor’s ready queue. Using yield() is an easy way to free up CPU cycles and speed up your application.
package com.theprogramguide; public class ThreadDemo1 extends Thread { public void run() { for (int i = 1; i <= 15; i++) { System.out.println("" + this.getName() + "-----" + i); if (i == 10) { this.yield(); } } } public static void main(String[ ] args) { ThreadDemo1 thread1 = new ThreadDemo1( ); ThreadDemo1 thread2 = new ThreadDemo1( ); thread1.start( ); thread2.start( ); } }
sleep() method
The sleep() method allows you to temporarily halt the execution of a thread. This technique does not affect the lock and monitor ownership of the thread. When you use sleep, you can specify a time in milliseconds or nanoseconds. The range for these parameters is 0 to 999999. When a thread gets interrupted while it is sleeping, it will throw an InterruptedException.
package com.theprogramguide; public class ThreadDemo1 extends Thread { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[ ] args) { ThreadDemo1 thread1 = new ThreadDemo1( ); thread1.start( ); } }
join() method
In Java, you can join one thread to another using the join() method. This method is used when one thread wants to wait for another thread to complete its work. The idea is similar to a relay race, where the second runner waits for the first runner to arrive. However, when you call join(), you should be aware that if any other thread interrupts the current thread, it will throw an exception called InterruptedException. When this exception occurs, the thread that was interrupted is cleared from the interrupt state.
package com.theprogramguide; public class ThreadJoin extends Thread { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(this.getName() + "-" + i); } } public static void main(String[ ] args) throws InterruptedException{ ThreadJoin thread1 = new ThreadJoin( ); ThreadJoin thread2 = new ThreadJoin( ); thread1.start(); thread1.join(); thread2.start( ); } }
Output:
Thread-0-1
Thread-0-2
Thread-0-3
Thread-0-4
Thread-0-5
Thread-1-1
Thread-1-2
Thread-1-3
Thread-1-4
Thread-1-5