Explain the thread implementation example in java ?
Definition of Thread in java: The lowest and lightest processing unit that a scheduler can control independently is a thread. Threads are components of a process that only enable the concurrent, efficient execution of programs with other components or threads of the process. The simplest approach to complete challenging tasks is by using threads. It is thought to be the simplest method for utilizing a machine’s numerous CPUs. They are separate from one another yet share the same address space.
There can be multiple ways through which a thread can be implemented in java :
- A Java class extending the Thread class
- A Java class implementing the runnable interface
A Java class extending the Thread class
class planforexams_thread_one extends Thread { public void run() { System.out.println(" Planforexams.com - Thread one is running "); } public static void main(String args[]) { planforexams_thread_one threadObj1 =new planforexams_thread_one(); threadObj1.start(); } }
A Java class implementing the runnable interface
class planforexams_thread_two implements Runnable { public void run() { System.out.println("Planforexams.com - Thread two is running."); } public static void main(String args[]) { planforexams_thread_two threadObj2 =new planforexams_thread_two(); threadObj2 =new Thread(obj); threadObj2 .start(); } }
Leave a Reply