0% found this document useful (0 votes)
17 views6 pages

Understanding Java Multithreading Basics

Thread is a lightweight subprocess that can run concurrently, and multithreading allows multiple threads to execute simultaneously within the same program. The life cycle of a thread includes states such as New, Runnable, Running, Wait, and Dead, and threads can be created by extending the Thread class or implementing the Runnable interface. Examples of both methods demonstrate how to create and start threads in Java.

Uploaded by

Rupali Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Understanding Java Multithreading Basics

Thread is a lightweight subprocess that can run concurrently, and multithreading allows multiple threads to execute simultaneously within the same program. The life cycle of a thread includes states such as New, Runnable, Running, Wait, and Dead, and threads can be created by extending the Thread class or implementing the Runnable interface. Examples of both methods demonstrate how to create and start threads in Java.

Uploaded by

Rupali Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Thread

Thread is a light-weight sub process. It can run concurrently with the other
parts.

Multithreading

The process of executing multiple threads simultaneously is known as


multithreading.
● Multithreading means executing two parts of the same program
concurrently.
● It is a part of multitasking.
● A program may contain more than one thread.

For Example: A browser can serve a web page while downloading a file. Each
of these actions is a thread.

Life Cycle of Thread

New State: Soon after the creation of a thread, it is said to be in the new state.
Runnable State: The life of a thread starts after invoking the start () method.
The start() method invokes the run () method.
Running State: When the thread is currently running, it is in the state of
running.
Wait State: When other threads are holding the resources, the current thread
is said to be in wait state
Dead State: When the thread has completed the execution of run () method, it
is said to be in dead state.

Creating Thread

There are two ways to create a thread in Java.


1. Extending the thread class.
2. Implementing runnable interface.

1. Extending Thread Class


Thread class has constructor and method to create and perform the operation
on class.

Program on using thread class

// [Link]

public class MyThread extends Thread

String message;

MyThread(String msg)

message=msg;

}
public void run()

for(int count=0;count<=5;count++)

[Link]("Run method: " + message);

public class MainThread

public static void main(String[] args)

MyThread t1 = new MyThread("Run");

MyThread t2 = new MyThread("Thread");

[Link]();

[Link]();

}
}

Output:
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Run
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Thread

2. Implementing Runnable Interface


Runnable interface is available in [Link] package. The purpose of Runnable
interface is to provide a set of rules common for objects to execute the
functionality while they are active.

Example : Sample program to implement runnable interface

class MyThread implements Runnable

String message;

MyThread(String msg)

message = msg;

}
public void run()

for(int count=0;count<=5;count++)

[Link]("Run method: " + message);

public class MainMyThread

public static void main(String[] args)

MyThread dt1=new MyThread("Run");

MyThread dt2=new MyThread("Thread");

Thread t1 = new Thread(dt1);

Thread t2 = new Thread(dt2);

[Link]();
[Link]();

Output:
Run method: Thread
Run method: Run
Run method: Thread
Run method: Run
Run method: Thread
Run method: Run
Run method: Thread
Run method: Thread
Run method: Thread
Run method: Run
Run method: Run
Run method: Run

You might also like