0% found this document useful (0 votes)
5 views9 pages

Java Threads

The document contains multiple Java programs demonstrating various aspects of threading, including creating threads by extending the Thread class or implementing the Runnable interface, setting thread names, managing thread priorities, and checking thread states. It also explains the concept of multithreading, the lifecycle of threads, and provides examples of thread creation and execution. Additionally, it covers important methods like start() and run() for thread management.

Uploaded by

encyclopedia2k21
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)
5 views9 pages

Java Threads

The document contains multiple Java programs demonstrating various aspects of threading, including creating threads by extending the Thread class or implementing the Runnable interface, setting thread names, managing thread priorities, and checking thread states. It also explains the concept of multithreading, the lifecycle of threads, and provides examples of thread creation and execution. Additionally, it covers important methods like start() and run() for thread management.

Uploaded by

encyclopedia2k21
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

#program 1 [ Thread's ]

// Java program to create a thread by extending


// the Thread class

public class Main extends Thread {


public static void main(String[] args) {
Main thrd = new Main();

[Link]();

[Link]("Outside the thread");


}

public void run() {


[Link]("Thread Executed");
}
}

#program 2

// Java program to create a thread by implementing


// Runnable interface

public class Main implements Runnable {


public static void main(String[] args) {
Main m = new Main();

Thread thrd = new Thread(m);

[Link]();

[Link]("Outside the thread");


}

public void run() {


[Link]("Thread Executed");
}
}

#program 3

// Java program to set and print thread name

public class Main {


public static void main(String[] args) {
String threadName;
Thread thrd1 = new Thread("Thread1");
Thread thrd2 = new Thread("Thread2");

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

threadName = [Link]();
[Link]("Thread Name: " + threadName);

threadName = [Link]();
[Link]("Thread Name: " + threadName);
}
}
#program 4

// Java program to print the thread Id of a thread

class MyThread implements Runnable {


public void run() {
[Link]("Thread Id: " + [Link]().getId());
}
}

public class Main {


public static void main(String[] args) {
Thread t = new Thread(new MyThread());
[Link]();
}
}

#program 5

// Java program to create multiple threads

class MyThread implements Runnable {


public void run() {
int i = 0;

for (i = 1; i <= 3; i++)


[Link]("Thread " + [Link]().getId() + "
is running");
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

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

#program 6

// Java program to sleep a thread

class MyThread implements Runnable {


public void run() {
int i = 0;

try {
for (i = 1; i <= 3; i++) {
[Link]("Thread " + [Link]().getId() + "
is running");

[Link](1000);
}
} catch (Exception e) {
}
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

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

#program 7

// Java program to set a thread name


// using setName() method

class MyThread implements Runnable {


public void run() {
int i = 0;

try {
[Link]("Thread : " + [Link]().getId());
} catch (Exception e) {

}
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

[Link]("Thread1");
[Link]("Thread2");
[Link]("Thread3");

[Link]("Thread Name: " + [Link]());


[Link]("Thread Name: " + [Link]());
[Link]("Thread Name: " + [Link]());

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

#program 8

// Java program to get thread priority

class MyThread implements Runnable {


public void run() {
try {
[Link]("Thread : " + [Link]().getId());
} catch (Exception e) {

}
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

[Link]("Thread Priority: " + [Link]());


[Link]("Thread Priority: " + [Link]());
[Link]("Thread Priority: " + [Link]());

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

#program 9

// Java program to set thread priority

class MyThread implements Runnable {


public void run() {
try {
[Link]("Thread : " + [Link]().getId());
} catch (Exception e) {

}
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

[Link](1);
[Link](2);
[Link](3);

[Link]("Thread Priority: " + [Link]());


[Link]("Thread Priority: " + [Link]());
[Link]("Thread Priority: " + [Link]());

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

#program 10
// Java program to get information of the
// currently executing thread

class MyThread implements Runnable {


public void run() {
try {
[Link]("Thread : " + [Link]());
} catch (Exception e) {

}
}
}

public class Main {


public static void main(String[] args) {
Thread t1 = new Thread(new MyThread());
Thread t2 = new Thread(new MyThread());
Thread t3 = new Thread(new MyThread());

[Link]("First Thread");
[Link]("Second Thread");
[Link]("Third Thread");

[Link](1);
[Link](2);
[Link](3);

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

#program 11

// Java program to check thread is


// alive or not

class MyThread implements Runnable {


public void run() {
try {
[Link]("Thread is running.");
} catch (Exception e) {

}
}
}

public class Main {


public static void main(String[] args) {
Thread t = new Thread(new MyThread());

[Link]("Thread isAlive: " + [Link]());


[Link]();
[Link]("Thread isAlive: " + [Link]());
}
}
# Thread Concept in Java

1.A Thread is a very light-weighted process, or we can say the smallest


part of the process that allows a program
to operate more efficiently by running multiple tasks simultaneously.

[Link] the tasks are executed without affecting the main program. In a
program or process, all the threads have their
own separate path for execution, so each thread of a process is
independent.

[Link] benefit of using thread is that if a thread gets an exception


or an error at the time of its execution, it doesn't
affect the execution of the other threads

[Link] the threads share a common memory and have their own stack, local
variables and program counter.

[Link] multiple threads are executed in parallel at the same time, this
process is known as Multithreading.

6. In a simple way, a Thread is a:

Feature through which we can perform multiple activities within a single


process.

Lightweight process.

Series of executed statements.

Nested sequence of method calls.

[Link] Model

Just like a process, a thread exists in several states. These states are
as follows:

1) New (Ready to run) : A thread is in New when it gets CPU time.

2) Running : A thread is in a Running state when it is under execution.

3) Suspended : A thread is in the Suspended state when it is temporarily


inactive or under execution.

4) Blocked : A thread is in the Blocked state when it is waiting for


resources.

5) Terminated : A thread comes in this state when at any given time, it


halts its execution immediately.

8. Creating Thread :

A thread is created either by "creating or implementing" the Runnable


Interface or by extending the Thread class.
These are the only two ways through which we can create a thread.

Thread Class :

A Thread class has several methods and constructors which allow us to


perform various operations on a thread.
The Thread class extends the Object class. The Object class implements
the Runnable interface.
The thread class has the following constructors that are used to perform
various operations.

Thread()
Thread(Runnable, String name)
Thread(Runnable target)
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, String name)
Thread(ThreadGroup group, Runnable target, String name, long stackSize)

#Runnable Interface(run() method)

The Runnable interface is required to be implemented by that class whose


instances are intended to be
executed by a thread. The runnable interface gives us the run() method to
perform an action for the thread.

start() method

The method is used for starting a thread that we have newly created. It
starts a new thread with a new callstack.
After executing the start() method, the thread changes the state from New
to Runnable. It executes the run() method
when the thread gets the correct time to execute it.

// Implementing runnable interface by extending Thread class


public class ThreadExample1 extends Thread {
// run() method to perform action for thread.
public void run()
{
int a= 10;
int b=12;
int result = a+b;
[Link]("Thread started running..");
[Link]("Sum of two numbers is: "+ result);
}
public static void main( String args[] )
{
// Creating instance of the class extend Thread class
ThreadExample1 t1 = new ThreadExample1();
//calling start method to execute the run() method of the Thread
class
[Link]();
}
}

#program
class NewThread implements Runnable {
String name;
Thread thread;
NewThread (String name){
[Link] = name;
thread = new Thread(this, name);
[Link]( "A New thread: " + thread+ "is created\n" );
[Link]();
}
public void run() {
try {
for(int j = 5; j > 0; j--) {
[Link](name + ": " + j);
[Link](1000);
}
}catch (InterruptedException e) {
[Link](name + " thread Interrupted");
}
[Link](name + " thread exiting.");
}
}
class ThreadExample2 {
public static void main(String args[]) {
new NewThread("1st");
new NewThread("2nd");
new NewThread("3rd");
try {
[Link](8000);
} catch (InterruptedException excetion) {
[Link]("Inturruption occurs in Main Thread");
}
[Link]("We are exiting from Main Thread");
}
}

#program

public class Main extends Thread {


public static void main(String[] args) {
Main thread = new Main();
[Link]();
[Link]("This code is outside of the thread");
}
public void run() {
[Link]("This code is running in a thread");
}
}

#program

public class Main implements Runnable {


public static void main(String[] args) {
Main obj = new Main();
Thread thread = new Thread(obj);
[Link]();
[Link]("This code is outside of the thread");
}
public void run() {
[Link]("This code is running in a thread");
}
}

#program

public class Main extends Thread {


public static int amount = 0;

public static void main(String[] args) {


Main thread = new Main();
[Link]();
[Link](amount);
amount++;
[Link](amount);
}

public void run() {


amount++;
}
}

You might also like