[go: up one dir, main page]

0% found this document useful (0 votes)
8 views2 pages

2.1 Threads

Java is a multi-threaded programming language that allows for the concurrent execution of multiple threads, enabling different tasks to be handled simultaneously. The life cycle of a thread includes several states: New, Runnable, Waiting, Timed Waiting, and Terminated, each representing a stage in the thread's execution. Advantages of Java multithreading include non-blocking user experience, time efficiency through simultaneous operations, and thread independence in case of exceptions.

Uploaded by

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

2.1 Threads

Java is a multi-threaded programming language that allows for the concurrent execution of multiple threads, enabling different tasks to be handled simultaneously. The life cycle of a thread includes several states: New, Runnable, Waiting, Timed Waiting, and Terminated, each representing a stage in the thread's execution. Advantages of Java multithreading include non-blocking user experience, time efficiency through simultaneous operations, and thread independence in case of exceptions.

Uploaded by

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

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

threaded program using Java.


Multithreading in Java is a process of executing multiple threads simultaneously.

writing in MS Word------listen music----research

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.

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.

New − A new thread begins its life cycle in the new state. It remains in this state
until the program starts the thread. It is also referred to as a born thread.

Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.

Waiting − Sometimes, a thread transitions to the waiting state while the thread
waits for another thread to perform a task. A thread transitions back to the
runnable state only when another thread signals the waiting thread to continue
executing.

Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state
when that time interval expires or when the event it is waiting for occurs.

Terminated (Dead) − A runnable thread enters the terminated state when it completes
its task or otherwise terminates.

-Shared memory area

multiprocessing and multithreading is used to achieve multitasking

Advantages of Java Multithreading


1) It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.

2) You can perform many operations together, so it saves time.

3) Threads are independent, so it doesn't affect other threads if an exception


occurs in a single thread.

THREAD CREATING
Threads can be created by using two mechanisms :

1. Extending the Thread class


2. Implementing the Runnable Interface
1-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-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.

You might also like