Your SlideShare is downloading. ×
Developing Multithreaded Applications
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Developing Multithreaded Applications

963
views

Published on

Published in: Technology, News & Politics

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×