0% found this document useful (0 votes)
52 views5 pages

Java p12

The document discusses implementing multithreading in Java by extending the Thread class and implementing the Runnable interface. It provides examples of creating a main thread, creating a new thread by extending Thread and implementing Runnable, and using methods like start(), run(), sleep(), and getName(). The conclusion states that multithreading allows for parallelism and optimized performance but requires understanding synchronization and thread safety.

Uploaded by

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

Java p12

The document discusses implementing multithreading in Java by extending the Thread class and implementing the Runnable interface. It provides examples of creating a main thread, creating a new thread by extending Thread and implementing Runnable, and using methods like start(), run(), sleep(), and getName(). The conclusion states that multithreading allows for parallelism and optimized performance but requires understanding synchronization and thread safety.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 12:- Write a program in java to implement Multithreading.

Implement a simple program which will just have a main thread.


Implement all methods in it like currentThread, sleep and setName.
Also implements Runnable interface and Thread class.

1. INTRODUCTION

Java is a multi-threaded programming language which means we can develop multi-


threaded program using Java. A multi-threaded program contains two or more parts that
can run concurrently and each part can handle a different task at the same time making
optimal use of the available resources specially when your computer has multiple CPUs.

Life Cycle of a Thread

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.

Threads can be created by using two mechanisms:


1. Extending the Thread class
2. Implementing the Runnable Interface
1.) Thread creation by extending the Thread class:
We create a class that extends the java.lang.Thread class. This class overrides the run()
Method available in the Thread class. A thread begins its life inside run() method. We
create an object of our new class and call start() method to start the execution of a thread.
Start() Invokes the run() method on the Thread object.

2. Thread creation by implementing the Runnable Interface:


We create a new class which implements java.lang.Runnable interface and override run()
method. Then we instantiate a Thread object and call start() method on this object.

Write a program in java to implement Main Thread.


public class CurrentThreadDemo {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Current thread: " + t);
// change the name of the thread
t.setName("My Thread");
System.out.println("After name change: " + t);
try {
for (int n = 5; n > 0; n--) {
System.out.println(n);
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}
}
}

//Write a program in java to implement multithreading by implementing Runnable


interface.
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
Write a program in java to implement multithreading by implementing Runnable
interface.
// Create a second thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
//Write a program in java to implement multithreading by extending Thread.
// Create a second thread by extending Thread
class NewThread extends Thread {
NewThread() {
// Create a new, second thread
super("Demo Thread");
System.out.println("Child thread: " + this);
start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
public class ExtendThread {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for (int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

Conclusion:-
In conclusion, multithreading in Java is a critical tool for achieving parallelism and
optimizing the performance of applications. It allows developers to harness the power of
multi-core processors, significantly improving resource utilization and responsiveness.
However, with this power comes responsibility. Multithreading introduces complexities
like race conditions and deadlocks, necessitating a firm grasp of synchronization
techniques and thread safety.

You might also like