thread presentation
DESCRIPTION
Threads in javaTRANSCRIPT
![Page 1: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/1.jpg)
Multithreading in Java
![Page 2: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/2.jpg)
Multitasking - Multithreading
Handling multiple tasks – MultitaskingAbility to initiate multiple processes - Multithreading
Hello Class
![Page 3: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/3.jpg)
Process & Threads
Process
Threads
Spell Checker in word can be considered as a thread
An application running is called a process, a process can have multiple threads.
![Page 4: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/4.jpg)
Example - Thread
NFS ROAD RASH
Games are best example for applying Multithreading
![Page 5: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/5.jpg)
What are Threads?• Two or more tasks executing concurrently within a single
program.• It is an independent path of execution in a program.• You always have thread in your application even if you don’t
create it.• main method is a thread and also known as main thread.• Threads are scheduled by OS to be processed by the
Processor, so its behavior depends on the OS. It cannot be guaranteed about threads execution.• In java it is created & controlled by java.lang.Thread class
![Page 6: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/6.jpg)
Creating Threads
Thread t = new Thread();
public void run(){// starting point for thread}
t.start();
Just creates Thread Object
Thread
-To start the thread start method should be invoked-No guarantee about when it will start, depends on OS scheduler
![Page 7: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/7.jpg)
Starting up with Threads• There are two ways to create thread in java;• By Extending the Thread class (java.lang.Thread)• instance of a class that inherits from thread
• Implement the Runnable interface (java.lang.Runnable)• instance of thread where you pass the behavior of the thread into the
thread constructor
• Let us see examples
![Page 8: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/8.jpg)
Lifecycle of a Thread
![Page 9: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/9.jpg)
Thread - sleep()• A method used to tell the current thread to sleep for certain
time.• Sleep method accepts time in milliseconds.• Can throw InterruptedException.• Cannot guarantee that a thread goes to sleep for specified
time.• Once sleep state is complete, the thread can move to
Runnable or Running state.
![Page 10: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/10.jpg)
Action Listener
![Page 11: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/11.jpg)
Action Listener Example• When you create a button on a Frame, and when you press it
and something happens that is called ActionListener. We tell the button to do something when it is clicked. Or it can be performed on TextBox, CheckBox, Radio Buttons, etc.
![Page 12: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/12.jpg)
ActionListener• Action Listeners are the event handlers to be implemented,
that you decide what to should be done or performed on certain user operations. The event occurs whenever an action is performed by user.
• Example 1• Example 2
![Page 13: Thread presentation](https://reader035.vdocument.in/reader035/viewer/2022081504/5563a575d8b42a2b6a8b5290/html5/thumbnails/13.jpg)
Done!