0% found this document useful (0 votes)
25 views

Core Java

The document discusses the main thread in Java programs and how to create additional threads. It notes that the main thread is created automatically and is responsible for running the main method. It provides examples of creating new threads by extending the Thread class and implementing the Runnable interface. The document also discusses methods for starting, pausing, and checking the status of threads.

Uploaded by

chaubeyabhi7070
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Core Java

The document discusses the main thread in Java programs and how to create additional threads. It notes that the main thread is created automatically and is responsible for running the main method. It provides examples of creating new threads by extending the Thread class and implementing the Runnable interface. The document also discusses methods for starting, pausing, and checking the status of threads.

Uploaded by

chaubeyabhi7070
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name :- Sneha Yadav

Roll no:-2106054
Core java assigment

THE MAIN THREAD

• Java provides built-in support for multithreaded


programming. A multi-threaded program contains
two or more parts that can run concurrently.
• Each part of such a program is called a thread, and
each thread defines a separate path of execution.
• When a Java program starts up, one thread begins
running immediately. This is usually called
the main thread of our program because it is the
one that is executed when our program begins.
• The main thread is important because it is
responsible for running the code that initializes the
program and sets it in motion.
• The main thread is created automatically when a
Java program is launched, and it is responsible for
executing the program's entry point method
(usually called main()).
• Here is an example of a simple Java program that
creates a new thread and prints a message from
the main thread and the new thread:
public class MainThreadExample {
public static void main(String[] args) {
// Print a message from the main
thread
System.out.println("Hello from the
main thread!");

// Create a new thread


Thread thread = new Thread(new
Runnable() {
public void run() {
// Print a message from the new
thread
System.out.println("Hello from a new
thread!");
}
});

// Start the new thread


thread.start();
}
}

• When this program is run, it will print the following


output:

Hello from the main thread!


Hello from a new thread!

• In this example, the main thread is the thread that


executes the main() method, and the new thread is
a separate thread that is created and started in the
main() method.

CREATING OF THREADS

• A thread is a separate path of execution in a Java


program.
• To create a thread, you need to create a new class
that extends the Thread class or implement the
Runnable interface.
• To start a thread, you need to call its start()
method, which will call the thread's run() method
where you can put the code that the thread should
execute.
• Here is an example of creating a thread by
extending the Thread class:

public class MyThread extends


Thread {
public void run() {
// put your code here that you
want to execute in the thread
}
}

• Here is an example of creating a thread by


implementing the Runnable interface:

public class MyThread implements


Runnable {
public void run() {
// put your code here that you want to
execute in the thread
}
}

• Once you have created your thread class, you can


start a new thread by creating a new instance of
the class and calling its start() method.
• Here is an example of starting a thread:

MyThread thread = new MyThread();


thread.start();

• By default, when a thread finishes executing its


run() method, it will stop.
• You can interrupt a thread by calling its interrupt()
method, which will cause the thread to throw an
InterruptedException.
• You can also stop a thread by calling its stop()
method, but this method is deprecated and should
not be used because it can cause problems with
the program's memory.
EXTEND THE THREAD CLASS
IN JAVA

• To extend the Thread class in Java, you create a


new class that extends the Thread class and
overrides the run() method.
For example:-
class MyThread extends Thread {
public void run() {
// Code to be executed in the new thread
}
}
• The run() method contains the code that will be
executed in the new thread. For example, you
could print a message in the run() method:
class MyThread extends Thread {
public void run() {
System.out.println("Hello from the new
thread!");
}
}
• Extending the Thread class allows you to create a
new thread by instantiating an object of your
subclass.
For example:-
MyThread thread = new MyThread();

• If you want to create a new thread but your class


already extends another class, you can implement
the Runnable interface instead.
For example:-
class MyClass implements Runnable {
public void run() {
// Code to be executed in the new thread
}
}
• When you implement the Runnable interface, you
need to create an instance of the Thread class
and pass your Runnable implementation to the
Thread constructor.
For example:-
MyClass runnable = new MyClass();
Thread thread = new Thread(runnable);

• The Thread class provides several methods that


you can use to manage the new thread, such as
start() to start the thread, sleep() to pause the
thread for a specified time, and interrupt() to
interrupt the thread.
For example, you can start the thread by
calling the start() method:

thread.start();

• You can also use the join() method to wait for a


thread to finish executing before continuing with
the current thread.
For example:-
thread.join();

• The Thread class also has several methods that


allow you to check the status of a thread, such as
isAlive() to check if the thread is currently running
and getState() to get the current state of the
thread.
For example:- if (thread.isAlive()) {
System.out.println("The thread is still running.");
}
• Extending the Thread class or implementing the
Runnable interface allows you to create multiple
threads that can execute concurrently, which can
improve the performance of your application. For
example, you could create two threads and start
them at the same time:
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
• When creating multiple threads, it is important to
carefully manage the access and modification of
shared data to avoid synchronization issues. For
example, you can use the synchronized keyword
to ensure that only one thread can access a
shared resource at a time:
synchronized (sharedObject) {
// Code that accesses the shared object
}

You might also like