Threads in Java - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Threads in Java

Description:

Some texts call a thread a lightweight process ... Java provides various mechanisms for concurrent Threads to coordinate their efforts... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 9
Provided by: davidleb
Category:
Tags: java | threads

less

Transcript and Presenter's Notes

Title: Threads in Java


1
Threads in Java
2
Java Threads
  • A thread is a single sequential flow of control
    within a program.
  • Some texts call a thread a lightweight process
  • A process carries a lot of baggage a thread is
    finer grained

3
Java Threads
  • Useful for user interfaces that must remain
    responsive
  • for servers with more than one simultaneous
    client
  • for polling loops much more efficient!
  • for increased parallel-processing performance
  • when modeling a naturally concurrent, parallel,
    or asynchronous situation.

4
Simple Thread Example
  • Simplest way to use threads is to have a class
    which extends Thread
  • And have a method called run()
  • Create an instance of this class and call start()

5
The Synchronized Keyword
  • public synchronized void foo()
  • bar()
  • Is equivalent to
  • public void foo()
  • synchronized( this ) // this is the monitor
    object
  • bar()

6
Thread methods
  • Java provides various mechanisms for concurrent
    Threads to coordinate their efforts
  • sleep( long ms ) throws InterruptedException
  • public static void yield()
  • public synchronized void start()
  • public final boolean isAlive()
  • public final void join() throws
    InterruptedException
  • public final void suspend() // deprecated.
  • public final void resume() // deprecated.
  • wait(lock), notify() and notifyAll()

7
Java Thread Life Cycle
8
Deadly Embrace
  • A is waiting for B
  • B is waiting for C
  • C is waiting for A
  • Everything stops! The curse of concurrent
    programming.
Write a Comment
User Comments (0)
About PowerShow.com