Multithreaded Programming PowerPoint PPT Presentation

presentation player overlay
1 / 8
About This Presentation
Transcript and Presenter's Notes

Title: Multithreaded Programming


1
Multithreaded Programming
  • Chapter 11

2
Multithreading
  • Allows different sections of code to run at the
    same time (seem to run at the same time)
  • Parallel processors
  • OS
  • Allows the programmer use the CPU when other
    tasks have to wait for some type of input
  • Keyword (synchronize)
  • Locks other threads from accessing methods

3
Thread Class and Runnable Interface
  • Methods
  • getName
  • getPriority
  • isAlive
  • join
  • run // method that must be implemented
  • sleep // allows other threads access to the CPU
  • start // in parent class that calls the run method

4
Thread Class and Runnable Interface
  • Creating a thread
  • Implement the Runnable interface
  • Extend the Thread class
  • public void run( )
  • //handles the items for the thread
  • Called when we call start( )

5
An Example of implementing the interface Runnable
  • class Test implements Runnable
  • Thread t
  • public Test( )
  • t new Thread(this, anyName)
  • t.start( ) // starting thread which will call
    run
  • public void run ( )
  • try
  • // implement what should be done in this class
    within the thread
  • t.sleep(200) // release CPU for time (in
    milliseconds)
  • catch (InterruptedException ie)

6
An Example ofextending the Thread Class
  • class Test extends Thread
  • public Test( )
  • super( anyName)
  • start( ) // starting thread which will call
    run
  • public void run ( )
  • try
  • // implement what should be done in this class
    within the thread
  • this.sleep(200) // release CPU for time (in
    milliseconds)
  • catch (InterruptedException ie)

7
Creating Multiple Threads
  • Call objects several times
  • In main the threads execute on their own
  • In the applet youll need to have the applets
    thread give control to the other threads by
    making it a thread as well.

8
Other Information about Threads
  • Thread priorities
  • setPriority( )
  • MIN_PRIORITY, MAX_PRIORITY, NORM_PRIORITY
  • Synchronization
  • synchronized void call (String msg)
  • Makes sure that only one thread is allowed
    through the method at a time
  • Or synchronized block of code
  • Interthread Communication
  • wait ( ), notify ( ), notifyAll( )
  • Deadlock
Write a Comment
User Comments (0)
About PowerShow.com