Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.
Example of Multi-thread:
package demotest; public class GuruMultithread implements Runnable{ /** * @param args */ public static void main(String[] args) { Thread guruthread1 = new Thread(); guruthread1.start(); Thread guruthread2 = new Thread(); guruthread2.start(); } @Override public void run() { // TODO Auto-generated method stub } }
A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.
There are various stages in life cycle of thread as shown in above diagram:
Threads can be created by using two mechanisms :
One way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin execution of the new thread.
class ExtendingThread extends Thread { String s[]={"Welcome","to","Java","Programming","Language"}; public static void main(String args[]) { ExtendingThread t=new ExtendingThread("Extending Thread Class"); } public ExtendingThread (String n) { super(n); start(); } public void run() { String name=getName(); for(int i=0;i<s.length;i++) { try { sleep(500); } catch(Exception e) { } System.out.println(name+":"+s[i]); } } }
The Clock
applet displays the current time and updates its display every second. You can scroll the page and perform other tasks while the clock updates. The reason is that the code that updates the clock's display runs within its own thread. The Clock
applet uses a technique different from SimpleThread
’s for providing the run
method for its thread. Instead of subclassing Thread
, Clock
implements the Runnable
interface and therefore implements the run
method defined in it. Clock
then creates a thread with itself as the Thread
’s target. When created in this way, the Thread
gets its run method from its target. The code that accomplishes this is highlighted:
import java.awt.Graphics; import java.util.*; import java.text.DateFormat; import java.applet.Applet; public class Clock extends Applet implements Runnable { private Thread clockThread = null; public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { //the VM doesn’t want us to sleep anymore, //so get back to work } } } public void paint(Graphics g) { //get the time and convert it to a date Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); //format it and display it DateFormat dateFormatter = DateFormat.getTimeInstance(); g.drawString(dateFormatter.format(date), 5, 10); } //overrides Applet’s stop method, not Thread’s public void stop() { clockThread = null; } }
Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
For indepth understanding of Java click on
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.