Developing Multithreaded Applications
Where multithreading can be useful… <ul><li>Non-blocking I/O </li></ul><ul><ul><ul><ul><li>The blocking read() method </li...
Process-Based Multitasking <ul><li>Processes in a Multitasking Environment </li></ul>
Thread-Based Multitasking <ul><li>Threads in a Multithreaded Environment </li></ul>
Ways of Creating a Thread : 2 <ul><li>Extend java.lang.Thread </li></ul><ul><li>Implement  java.lang.Runnable </li></ul>ja...
Extending java.lang.Thread <ul><li>class MyThread extends Thread </li></ul><ul><li>{ </li></ul><ul><li>public void run()  ...
Implementing java.lang.Runnable <ul><li>class  MyThread implements Runnable </li></ul><ul><li>{ </li></ul><ul><li>public v...
Life-Cycle of a Thread <ul><li>Intermediate States : suspend, waiting/blocking etc. </li></ul>
Let ur thread participate in the race : call start() <ul><li>A thread exists once it has been constructed, but at that poi...
Terminating a Thread <ul><li>Normal termination of life-cycle. </li></ul><ul><li>Abnormal or forced termination </li></ul>...
Pausing, Suspending & Resuming a thread <ul><li>A running thread can be told to pause or resume. </li></ul>main thread Thr...
Upcoming SlideShare
Loading in...5
×

Developing Multithreaded Applications

980

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
980
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Developing Multithreaded Applications"

  1. 1. Developing Multithreaded Applications
  2. 2. Where multithreading can be useful… <ul><li>Non-blocking I/O </li></ul><ul><ul><ul><ul><li>The blocking read() method </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Is blocking nature of read desirable? Shouldn’t I/O be dealt in an asynchronous way? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Ways to overcome : - </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>I/O Multiplexing </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Pooling </li></ul></ul></ul></ul></ul><ul><li>Alarms & Timers </li></ul><ul><li>Independent Tasks </li></ul><ul><ul><li>Think of Parallel processing instead of linear. </li></ul></ul><ul><li>Parallelizable Algorithms </li></ul><ul><ul><ul><li>Ex : parallelization of loops. </li></ul></ul></ul>
  3. 3. Process-Based Multitasking <ul><li>Processes in a Multitasking Environment </li></ul>
  4. 4. Thread-Based Multitasking <ul><li>Threads in a Multithreaded Environment </li></ul>
  5. 5. Ways of Creating a Thread : 2 <ul><li>Extend java.lang.Thread </li></ul><ul><li>Implement java.lang.Runnable </li></ul>java.lang.Object java.lang.Thread A user-defined class java.lang.Runnable A user-defined class
  6. 6. Extending java.lang.Thread <ul><li>class MyThread extends Thread </li></ul><ul><li>{ </li></ul><ul><li>public void run() // overriding run </li></ul><ul><li>{ </li></ul><ul><li>// code that the thread will execute </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>------------------------------------------------ </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>Thread t = new MyThread(); </li></ul><ul><li>t.start(); // creating a thread executing a task parallel to others </li></ul><ul><li>// code the main thread will execute </li></ul><ul><li>} </li></ul>
  7. 7. Implementing java.lang.Runnable <ul><li>class MyThread implements Runnable </li></ul><ul><li>{ </li></ul><ul><li>public void run() // implementing run() </li></ul><ul><li>{ </li></ul><ul><li>// code to be executed </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>--------------------------------- </li></ul><ul><li>public static void main(String[] args) </li></ul><ul><li>{ </li></ul><ul><li>Thread t = new Thread(new MyThread(),”Thread2”); </li></ul><ul><li>t.start(); </li></ul><ul><li>// Let main thread do something else </li></ul><ul><li>} </li></ul>
  8. 8. Life-Cycle of a Thread <ul><li>Intermediate States : suspend, waiting/blocking etc. </li></ul>
  9. 9. Let ur thread participate in the race : call start() <ul><li>A thread exists once it has been constructed, but at that point it is not executing any code. The thread is in a waiting state. </li></ul><ul><li>Thread t = new MyThread(); </li></ul><ul><li>Once start() is invoked, the thread is said to be alive. </li></ul><ul><li>Check the state of the thread before & after invoking start(). </li></ul><ul><li>use isAlive() </li></ul><ul><li>Other threads can change the attributes of a waiting thread. </li></ul>
  10. 10. Terminating a Thread <ul><li>Normal termination of life-cycle. </li></ul><ul><li>Abnormal or forced termination </li></ul><ul><li>Once a thread is started, it call only one method i.e., run(). When it completes run, the thread is said to be terminated. </li></ul><ul><li>What can cause abnormal termination of a thread? </li></ul><ul><li>Can run( ) throw a checked exception? </li></ul><ul><li>How can a thread be forced to terminate? </li></ul>
  11. 11. Pausing, Suspending & Resuming a thread <ul><li>A running thread can be told to pause or resume. </li></ul>main thread Thread t1 I must do the things first, I’m going to suspend t1. t1.suspend(); // main did its work t1.resume(); <ul><li>A running thread can pause its own execution for a specified time using sleep() </li></ul>

×