Thread in Java - TCCI

Page 1

Thread in Java - TCCI Thread is the most important part for concurrent execution. Thread is the light wait process that means one process can be divided in to small small threads which will execute concurrently for fast execution of tasks. All Java programs have at least one thread, known as the main thread, which is created by the JVM at the program’s start, when the main() method is invoked with the main thread. In Java, creating a thread is accomplished by implementing an interface and extending a class. Every Java thread is created and controlled by the java.lang.Thread class.

A Thread class extends Object class and implements Runnable interface. The one advantage of using Runnable interface over Thread class, is that the class which implements Runnable interface can extend one class or implement multiple interfaces which is not possible by extending thread class. Example 1. 2. 3. 4. 5. 6. 7.

class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start();


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.