Java
Thread
By. Mohamed Essam
What is Multithreading?
Multithreading enables multiple tasks in a
program to be executed concurrently.
What is Multithreading?
One of the powerful features of Java is its built-in support for
multithreading—the concurrent running of multiple tasks
within a program. In many programming languages, you have
to invoke system-dependent procedures and functions to
implement multithreading.
Thread Concepts
 A program may consist of many tasks that can run
concurrently. A thread is the flow of execution, from
beginning to end, of a task.
 A thread provides the mechanism for running a task. With
Java, you can launch multiple threads from a program
concurrently. These threads can be executed
simultaneously in multiprocessor systems.
Thread Concepts
Here multiple threads are running on multiple CPUs. (b)
Here multiple threads share a single CPU.
Thread life cycle
Thread life cycle
 New(Thread Born): in this phase, the
thread is created using class Thread
class.
 It remains in this state till the program
starts the thread.
Thread life cycle
 Runnable: the instance of the thread is invoked.
Thread life cycle
 Running: when the thread start executing ,then the
state is changed to “running” state .
 The scheduler selects one thread from the pool, and
it start executing in the application.
Thread life cycle
 Waiting : this is the state when thread has to be wait.
 Dead: this is the state when the thread is terminated.
There are to ways to create thread
1) It can created by extended the thread class and
overriding it’s run method.
2) Another way to create thread is to implement the
runnable interface
Creating Tasks and Threads
 To create tasks, you have to first define a class for tasks,
which implements the Runnable interface.
 The Runnable interface is rather simple. All it contains is
the run method. You need to implement this method to tell
the system how your thread is going to run
Creating Tasks and Threads
Any Questions?
Mohamed Essam
!
CREDITS: This presentation template was created by Slidesgo,
including icons by Flaticon, and infographics & images by Freepik
THANKS!
Contacts
Mhmd96.essam@gmail.com
Please keep this slide for attribution

Thread

  • 1.
  • 2.
    What is Multithreading? Multithreadingenables multiple tasks in a program to be executed concurrently.
  • 3.
    What is Multithreading? Oneof the powerful features of Java is its built-in support for multithreading—the concurrent running of multiple tasks within a program. In many programming languages, you have to invoke system-dependent procedures and functions to implement multithreading.
  • 4.
    Thread Concepts  Aprogram may consist of many tasks that can run concurrently. A thread is the flow of execution, from beginning to end, of a task.  A thread provides the mechanism for running a task. With Java, you can launch multiple threads from a program concurrently. These threads can be executed simultaneously in multiprocessor systems.
  • 5.
    Thread Concepts Here multiplethreads are running on multiple CPUs. (b) Here multiple threads share a single CPU.
  • 6.
  • 7.
    Thread life cycle New(Thread Born): in this phase, the thread is created using class Thread class.  It remains in this state till the program starts the thread.
  • 8.
    Thread life cycle Runnable: the instance of the thread is invoked.
  • 9.
    Thread life cycle Running: when the thread start executing ,then the state is changed to “running” state .  The scheduler selects one thread from the pool, and it start executing in the application.
  • 10.
    Thread life cycle Waiting : this is the state when thread has to be wait.  Dead: this is the state when the thread is terminated.
  • 11.
    There are toways to create thread 1) It can created by extended the thread class and overriding it’s run method. 2) Another way to create thread is to implement the runnable interface
  • 12.
    Creating Tasks andThreads  To create tasks, you have to first define a class for tasks, which implements the Runnable interface.  The Runnable interface is rather simple. All it contains is the run method. You need to implement this method to tell the system how your thread is going to run
  • 13.
  • 14.
  • 15.
    CREDITS: This presentationtemplate was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik THANKS! Contacts Mhmd96.essam@gmail.com Please keep this slide for attribution