Docstoc

Threads

Document Sample
Threads Powered By Docstoc
					      Multithreaded Programming
          using Java Threads

                        Rajkumar Buyya
Cloud Computing and Distributed Systems (CLOUDS) Laboratory
     Dept. of Computer Science and Software Engineering
               University of Melbourne, Australia
    http://www.cloubus.org/~raj or http://www.buyya.com

          Lecture: Christian Vecchiola csve@unimelb.edu.au



                                                              1
                        Agenda
   Introduction
   Defining Threads
   Java Threading API
       Priorities
   Synchronization
   Assignment 1:
       Multi-Threaded Math Server
   Advanced Topics:
       Concurrency Models: master/worker, pipeline, peer processing
       Multithreading Vs multiprocessing




                                                                       2
                Introduction

   Modern Systems
       Multiple applications run concurrently

                                                                   games

                               web & email
                                               office automation




                                             multimedsa
                                                                    pictures




                                     Multitasking

                                                                               3
                Introduction

   Modern Applications (I)
       Applications perform many tasks at once..

                               Background printing




                                GUI rendering



                               Application core logic



                                                        4
             Introduction

   Modern Applications (II)

                                Video Streaming




                               Favorities, Share,
                               Comments Posting




                                                    5
                            Introduction

   Modern Applications (III)
                                                          Process Request Client 1
                                   Web/FTP
                                    server


                                                            Process Request Client 2

    Client 1


                                                            Process Request Client N




      Client 2




                 Client N                Multithreading

                                                                                     6
                    Introduction

   Modern Applications & Systems
       Operating System Level
            Multitasking: multiple applications running at
             once
       Application Level
            Multithreading: multiple operations performed at
             the same time
                                                  Today’s focus
       Bottom Line:
            Illusion of concurrency


                                                                  7
              Defining Threads

   Threads
       Thread: set of instructions that are executed
        sequentially within an program.
         public class SingleThread
         {
            public static void main(String[] args) {
               …… …
               for (int i=0; i<args.Length; i++) {                One Thread
                    if (args[i].equals("-r") == true) {
                       SingleThread.executeOptionR(args);
                    }
               }
            }
            private static void executeOptionR(String[] args) {
               …… …
            }
         }



                                                                               8
                     Defining Threads

       Multithreading
            Multithreading: execution of multiple threads
             within the same process space.
public class MultiThread
{
   public static void main(String[] args) {
       …… …                                         public class Op1 implements Runnable
       for (int i=0; i<args.Length; i++) {          {
            if (args[i].equals("-op1“) == true) {      public void run() {
               Thread t1 = new Thread(Op1);               …… …                     Thread
Thread         t1.start();                             }
            }                                       }
            if (args[i].equals("-op2“) == true) {
               Thread t2 = new Thread(Op2);
               t2.start();                          public class Op2 implements Runnable
           }                                        {
       }                                               public void run() {
   }                                                      …… …
}
                                                                                   Thread
                                                       }
                                                    }
                                                                                        9
                            Defining Threads
           Hierarchy Parallelism                            Code Granularity
Sockets / MPI
    PVM          Task i-1       Task i   Task i+1   Large grain - task level
                                                    Program



                              func1(…)   func1(…)   Medium grain – control
                func1(…)
                {             {          {          level
  Threads                     …          …          Function (thread)
                …
                              …          …
                …
                              }          }
                }




 Compilers      a(0)=…         a(0)=…     a(0)=…    Fine grain – data level
                b(0)=…         b(0)=…     b(0)=…    Loop (Compiler)



    CPU            +             X         load     Very fine grain – instruction level
                                                    Pipeline hardware

                                                                                    10
                 Defining Threads

   Threads and Processes
       Each application that is executed on modern
        operating systems contains at least one
        thread.
                                                                          Address space
             Single-threaded Process
        1.     When a process starts a default




                                                     Execution Timeline
               (main) thread is created.
                                                                               Main Thread
        2.     This threads lasts for the entire
               execution of the process.
        3.     It executes the code that is loaded
               in the process space sequentially.



                                                                                             11
                                       Defining Threads

                        Threads and Processes
                                                            Multithreaded Process
                           Common address space            1.    When a process starts a default
                                                                 (main) thread is created.
                                                           2.    From this thread other threads can
                         Main Thread
                                                                 be created.
Execution Timeline




                                           Thread          3.    Each of these threads can run for
                                                                 different periods
                                  Thread
                                                           4.    Each of these threads can create
                                                  Thread
                                                                 new threads
                                                           5.    All the threads have access to a
                                                                 shared memory space.
                              Thread


                                                                Multiple Execution
                                                                     Streams

                                                                                             12
                 Defining Threads

   Why to use threads?
       Originally conceived to simplify and improve
        algorithms programming
                                       algoritmo   process

             independent components
               that can be performed
                    concurrently




       Major applications:                        threads


            GUI programming
            Throughput computing
                                                             13
               Defining Threads

   Applications
       Threads are mostly used to perform:
            Parallelism and concurrent execution of
             independent tasks / operations.
            Implementation of reactive user interfaces.
            Non blocking I/O operations.
            Asynchronous behavior.
            Timer and alarms implementation.



                                                           14
           Defining Threads

   Example: Web/FTP Server
                                                           Main Thread
                                       while <running>
                                       {
                                          <wait for request>
                                          <create a new worker thread>
                                          <start the thread>
                                       }                       <request 1>
                  Execution Timeline



        Web/FTP
         server                                                Worker
                                                               Thread     <request 2>

                                                   <request N>
                                                                         Worker
                                                                         Thread
                                                  Worker
                                                  Thread




                                                                                        15
             Defining Threads

   Summing Up
       A piece of code that run in concurrent with other
        threads.
       Each thread is a statically ordered sequence of
        instructions.
       Threads are being extensively used express
        concurrency on both single and multiprocessors
        machines.
       Programming a task having multiple threads of
        control – Multithreading or Multithreaded
        Programming.

                                                            16
             Java Threading API

   Overview
     Objects and classes for thread management
            java.lang.Thread
            java.lang.Runnable
            java.lang.ThreadGroup
       Language constructs for synchronization
            synchronized keyword
            Object.[wait(), notify(), notifyAll()]
       Advanced features:
            thread-safe collections


                                                      17
                  Java Threading API
        Thread Creation
             Extends the java.lang.Thread class
             Implement the java.lang.Runnable interface
                                                                            Method 2
  Method 1                                    import java.lang.Runnable;
import java.lang.Thread;
                                              public class Worker implements Runnable
public class Worker extends Thread            {
{                                                public Worker() {
   public Worker() {                                ……
      ……                                         }
   }                                             // main method of the thread
   // main method of the thread                  public void run() {
   public void run() {                              ……
      ……                                         }
   }                                             public static void main(String[] args) {
   public static void main(String[] args) {         Worker wk = new Worker();
      Worker thread = new Worker();                 Thread thread = new Thread(wk);
      thread.start();                               thread.start();
   }                                             }
}                                             }

                                                                                            18
            Java Threading API

   Thread Creation
       Extending java.lang.Thread
           Objects are threads
           Define a new class that extends Thread
           Implement the run method.
       Advantages:
           Easy to create and start…
       Disadvantages:
           The class must inherit from Thread

                                                     19
            Java Threading API

   Thread Creation
       Extending java.lang.Thread
         import java.lang.Thread;

         public class Worker extends Thread
         {
            public Worker() {
               ……
            }
            // main method of the thread
            public void run() {
               System.out.println("This class extends Thread");
            }
            public static void main(String[] args) {
               Worker thread = new Worker();
               thread.start();
            }
         }




                                                                  20
            Java Threading API

   Thread Creation
       Implementing java.lang.Runnable
           Objects are not threads
           Define a new class that implements Runnable
           Implement the run method.
       Advantages:
           The class can inherit from any type…
       Disadvantages:
           The setup requires more work…

                                                          21
            Java Threading API

   Thread Creation
       Implementing java.lang.Runnable
         import java.lang.Runnable;

         public class Worker implements Runnable
         {
            public Worker() {
               ……
            }
            // main method of the thread
            public void run() {
               System.out.println("This class implement Runnable");
            }
            public static void main(String[] args) {
               Worker worker = new Worker();
               Thread thread = new Thread(worker);
               thread.start();
            }
         }



                                                                      22
              Java Threading API
   Thread Life Cycle Management
                                                 I/O completion

         new                                         suspend()

                                           timer expire
                  start()                                               blocked
                                          / interrupted



         notify()            ready
                                                     sleeping

                            dispatch
                                         sleep()
    waiting
                                            block on I/O
                                                                 suspend()


               wait()         running
                                              stop()
                                                                      dead
                                        completion                                23
                Java Threading API

   Example: 3 Threads Program
                                              A.java
                                               B.java
     public class A extends Thread {
      public class B extends Thread {
       public class C extends Thread {
                                               C.java
         public void run() {
          public void run() {
             for(int i=0; i<5;
           public void run() { i++) {
              for(int i=0; i<5; i++) {
                 System.out.println(“\tFrom Thread A i=“+i);
               for(int i=0; i<5; i++) {
                  System.out.println(“\tFrom Thread B i=“+i);
             }     System.out.println(“\tFrom Thread C i=“+i);
              }
             System.out.println(“Exit from Thread A i=“+i);
               }
              System.out.println(“Exit from Thread B i=“+i);
         }     System.out.println(“Exit from Thread C i=“+i);
          }
     }     }
      }
       }


     public class ThreadTest {
                                    ThreadTest.java
         public void main(String[] args) {

             new A().start();
             new B().start();
             new C().start();
         }
     }

                                                                 24
          Java Threading API

   Example: 3 Threads Program
                     Run 1                               Run 2
[raj@mundroo]raj: java ThreadTest   [raj@mundroo]raj: java ThreadTest
    From Thread A: i= 1                 From Thread A: i= 1
    From Thread A: i= 2                 From Thread A: i= 2
    From Thread A: i= 3                 From Thread A: i= 3
    From Thread A: i= 4                 From Thread A: i= 4
    From Thread A: i= 5                 From Thread A: i= 5
Exit from Thread A                      From Thread C: k= 1
    From Thread C: k= 1                 From Thread C: k= 2
    From Thread C: k= 2                 From Thread C: k= 3
    From Thread C: k= 3                 From Thread C: k= 4
    From Thread C: k= 4                 From Thread C: k= 5
    From Thread C: k= 5             Exit from Thread C
Exit from Thread C                      From Thread B: j= 1
    From Thread B: j= 1                 From Thread B: j= 2
    From Thread B: j= 2                 From Thread B: j= 3
    From Thread B: j= 3                 From Thread B: j= 4
    From Thread B: j= 4                 From Thread B: j= 5
    From Thread B: j= 5             Exit from Thread B
Exit from Thread B                  Exit from Thread A


                                                                        25
           Java Threading API

   Thread Priorities
       Java allows developer to set the priority with which
        it will be executed by the JVM.
       By default each thread is assigned the
        NORM_PRIORITY and threads are scheduled in FCFS
        mode.
       There is a range of values for priorities:
           MIN_PRIORITY = 1

           NORM_PRIORITY = 5

           MAX_PRIORITY = 10




                                                          26
             Java Threading API

   Thread Priorities
       Let’s modify the previous example…
         public class ThreadPriorities {
                                              ThreadPriorities.java
             public void main(String[] args) {

                 A threadA = new A();
                 B threadB = new B();
                 C threadC = new C();
                 threadA.setPriority(Thread.MAX_PRIORITY);
                 threadB.setPriority(threadA.getPriority()+1);
                 threadC.setPriority(Thread.MIN_PRIORITY);
                 System.out.println(“Started Thread A…");
                 threadA.start();
                 System.out.println("Started Thread B…");
                 threadB.start();
                 System.out.println("Started Thread A…");
                 threadC.start();
                 System.out.println("End of main Thread");
             }
         }
                                                                      27
             Java Threading API

   Thread Priorities
       Priority values are…
                         INDICATIVE VALUES!

       This means..
            The number of available priorities depends on
                 The implementation of the JVM
                 The hosting OS
                 The mapping of java Threads to physical OS threads
            Only the ordering of priority values is guaranteed

                                                                       28
             Java Threading API

   Other operations
       Identification
            get/set Name and Id
            isActive / isInterrupted
            activeCount / currentThread
       StackTrace access
       Direct operations
            suspend / resume / interrupt
            start / stop / sleep
       Synchronization
            join
            holdsLock
       … more

                                            29
                 Synchronization

   Overview
     Required when accessing shared resources

     Prevents...
            ... spurious write/reads
            ... races on data
            ... inconsistency of data
       Ensures that...
            ... only the allowed number of threads (generally
             one) access the data at the same time.
       Requires primitives at language level
                                                             30
                 Synchronization

   Overview
       Examples:
            Printer (two person jobs cannot be printed at the same
             time).
            Simultaneous operations on your bank account.



             Can the following operations be done
             at the same time on the same account?
                 - Deposit()
                 - Withdraw()
                 - Enquire()

                                                                      31
                      Synchronization

           Scenario: online bank
                        Internet
                       Bank Server

     Client 1                                                      Bank Operator 1

                                                Bank Local Area Network



Client 2




                                                           Bank Operator M

           Client N
                                Bank Database
                                                                                     32
               Synchronization

   Scenario: online bank
       Assumption
           The internet bank server processes each request
            with a different thread
       Case study
           A customer operates on account X
           An operator updates account X
           (Alternatively: two or more customers/operators
            update the same account)


                                                              33
                 Synchronization

   Scenario: online bank
       Problem!
           If two threads access the same bank account the
            final value of the account could be unpredictable!
           In particular the value could be:
                Not valid
                Or an amount that does not map to any operation
                 performed
           This happens because there is no exclusive
            access to the bank account data from each
            thread.
                                                                   34
                Synchronization

   Scenario: online bank
       Synchronization allows to solve the problem:
            It allow each thread to have exclusive access
            It makes the accesses of threads sequential
            It preserves the consistency of data
       But…
            It does not enforce an ordering for accessing the
             data from each thread



                                                             35
                  Synchronization

   Synchronization API
       Java provides different facilities for
        synchronization
            synchronized
                 keyword (method modifier)
                 transforms the entire scope of the method a protected region
                  that is executed atomically
                 used for simple monitors and binary semaphores
            Object.[wait, notify, notifyAll]
                 “special methods”
                 implement the primitives wait and signal (Dijkstra)
                 used for integer semaphore, and more complex
                  synchronization patterns
                                                                            36
                  Synchronization

   Scenario: online bank
       Solution (naïve)
            The threads operating on the bank account share
             the same Account instance
            The account class is “synchronized”
                 deposit()
                 enquire()
                 withdraw()
             are “modified” with the synchronized keyword.



                                                             37
                         Synchronization

   Scenario: online bank
     <account-ref:account>
     op: account.withdraw(50);                     <balance>
                                                   + deposit(int amount)
    Thread                                         + withdraw(int amount)
                                                   + enquire()

             Client Request 1                   Account

                                                             Shared Instance



     <account-ref:account>
     op: account.enquire();
                                  <account-ref:account>
                                  op: account.deposit(1000)
    Thread
                                 Thread
         Client Request 2
                                      Client Request N

                                                                               38
                 Synchronization

   Scenario: online bank
       Account class
         public class Account {
                                              Account.java
             int balance;

             public synchronized void deposit(int amount) {
                this.balance += amount;
             }

             public synchronized void withdraw(int amount)
                this.balance -= amount;
             }

             public synchronized void enquire( ) {
                return this.balance;
             }
         }



                                                              39
                  Synchronization

   Scenario: online bank
       Putting all together..
         …
         public class InternetBankingSystem {

             public void main(String[] args) {

                 String id = args[0];
                 Account account = getAccountFromDB(id);

                 // perform three different operations on the same account
                 // asynchronously

                 WithdrawThread w = new WithdrawThread(account, 50);
                 DepositThread d = new DepositThread(account, 10000);
                 EnquireThread e = new EnquireThread(account);

                 w.start();
                 d.start();
                 e.start();
             }
         }
                                                                             40
                  Synchronization

   Scenario: online bank
       Threads…                            WithdrawThread.java
         import Account;

         public class WithdrawThread extends Thread {

             private Account account;

             public withdrawThread(Account acc, int amnt) {

                 this.account = acc;
                 this.amount = amnt;
             }

             public void run() {

                 this.account.withdraw(this.amount);
             }
         }



                                                                  41
                Synchronization

   Object.[wait, notify, notifyAll]
       These methods are “special”:
            The JVM ensure that they are executed atomically
            They have a specific implementation
       When to use them?
            They can be used to emulate synchronized
            They can implement different synchronization
             patterns
            They require more work and a better
             understanding
                                                            42
                                    Assignment 1

            Multi-Threaded Math Server
                                                                         Process Request Client 1
                           sin(5.0) ?      MathServer
                                                                      double y = sin(5.0);


                                                                          Process Request Client 2

                                                                       double y = cos(3.0);
             Client 1
cos(3.0) ?
                                                                         Process Request Client N

                                                                      double y = sqrt(2.0);

                Client 2

                                           Available operations:
         sqrt(2.0) ?
                                                 -   sin(double x)
                                                 -   cos(double x)
                             Client N            -   sqrt(double x)
                                                 -   etc, etc...
                                                                                              43
                    Assignment 1

   Multi-Threaded Math Server
       Hints:
            Use Sockets for communication…
            Use one thread for each request…
            Implement a very simple protocol (i.e.):
                 Request: <op> <value>
                 Response: <result>
                Advanced Topics

   Overview
       Concurrency Models:
            The master/worker model
            The peer model
            A thread pipeline
       Multithreading vs Multiprocessing




                                            45
                Advanced Topics

   Master/Worker Model
       1 Master thread
            Coordinates the work
            Creates / controls worker thread
            Entry point of the system
       N Slave threads
            Depend upon on master
            Perform the execution of the task
            Notify (return results to) the master on
             completion
                                                        46
                      Advanced Topics

         Master/Worker Model
               Program                         Resources
                           Workers

                           <Task X>

 Input/                   Thread
                                      Files
Request      Master
                           <Task Y>                 Databases
          <Main Thread>
                          Thread
          Thread                       Disks
                           <Task Z>

                          Thread
                                                   Special
                                                   Devices




                                                                47
               Advanced Topics

   Peer Model
       All threads are “equal”
            The system is completely decentralized
            All the threads can process each task
            Each of the threads..
                picks up the input

                processes it

            There is no coordinator thread



                                                      48
           Advanced Topics

     Master/Worker Model
             Program                 Resources

                  Peers

                 <Task X>
 Input/                     Files
Request         Thread
                                          Databases
                 <Task Y>
 Input/         Thread
Request                      Disks

                 <Task Z>
 Input/                                  Special
                Thread                   Devices
Request




                                                      49
               Advanced Topics

   Pipeline Model
       Processing happens in “stages”
            The system features a sequential behavior
            Each thread processes a request and passes it to
             the next thread in the chain
            Each thread represents a stage of the processing
            The number of concurrent requests processed is
             equal to the number of stages of the pipeline




                                                            50
                 Advanced Topics

         Pipeline Model
              Program


                 <Task X>            <Task Y>             <Task Y>
 Input/
Request         Thread              Thread               Thread




                Special
                Devices     Disks            Files       Databases

                                                     Resources
                                                                     51
                Advanced Topics

   Multithreading vs Multiprocessing
       Multithreading
            Use of multiple threads of execution
       Multiprocessing
            Use of multiple processing units (CPUs) for
             executing applications
       Deployment
            Shared Memory Systems
            Distributed Memory Systems

                                                           52
                 Advanced Topics

   Multithreading on Multi-Processors
                 Process Parallelism


        P1


        P2


        P3


     Processes                                                                   CPU
                                         time line

         The number of processes is generally bigger than the number of processors


                                                                                       53
                 Advanced Topics

   Multithreading on Uni-Processors
                 Process Concurrency


        P1


        P2


        P3


     Processes                                                               CPU
                                           time line

                   Number of Simultaneous execution units > number of CPUs


                                                                                   54
              Advanced Topics

   Multiprocessing and Multithreading
                                    Clusters and Grids

                         Application
                                                                 Application
        Application



     Application




                                                                     CPU Pool
                   CPU Pool

           Better response time in                         Higher throughput
      multiple application environments                  for parallel applications


                                                                                     55
                 References

   Rajkumar Buyya, Thamarai Selvi,
    Xingchen Chu, Mastering OOP with
    Java, McGraw Hill (I) Press, New Delhi,
    India, 2009.
   Sun Java Tutorial – Concurrency:
       http://java.sun.com/docs/books/tutorial/esse
        ntial/concurrency/



                                                   56
The End




          57
Backup slides




                58
    A single threaded program

class ABC
{
….
    public void main(..)    begin

    {
    …                      body

    ..
                           end
    }
}

                                    59
A Multithreaded Program

                      Main Thread




                                       start
           start       start




Thread A             Thread B                  Thread C




 Threads may switch or exchange data/results
                                                          60
     Single and Multithreaded
             Processes
       threads are light-weight processes within a process

Single-threaded Process                  Multiplethreaded Process
                            Threads of
                            Execution




Single instruction stream                Multiple instruction stream
                            Common
                            Address Space

                                                                 61
Multithreaded Server: For Serving
  Multiple Clients Concurrently


 Client 1 Process                  Server Process

                                       Server
                                      Threads
                       Internet


 Client 2 Process




                                                    62
    Web/Internet Applications:
Serving Many Users Simultaneously

                                     PC client


        Internet
        Server
                   Local Area Network




                                PD
                                A
                                                 63
  Modern Applications need Threads (ex1):
Editing and Printing documents in background.
                              Printing Thread



 Editing Thread




                                                64
Multithreaded/Parallel File Copy

  reader()
  {                                    writer()
      - - - - - - - - -    buff[0]     {
      -                                    - - - - - - - - - -
      lock(buff[i]);                       lock(buff[i]);
      read(src,buff[i]);   buff[1]
                                           write(src,buff[i]);
      unlock(buff[i]);                     unlock(buff[i]);
      - - - - - - - - -                    - - - - - - - - - -
      -                                }
  }




            Cooperative Parallel Synchronized
                        Threads
                                                                 65
           Threading Mechanisms...
   Create a class that extends the Thread class
   Create a class that implements the Runnable
    interface

       Thread                  Runnable             Thread



      MyThread                   MyClass




(objects are threads)   (objects with run() body)

         [a]                          [b]

                                                             66
         1st method: Extending Thread
                     class
   Create a class by extending Thread class and override
    run() method:
     class MyThread extends Thread
     {
          public void run()
        {
            // thread body of execution
        }
     }
   Create a thread:
     MyThread thr1 = new MyThread();
   Start Execution of threads:
     thr1.start();
   Create and Execute:
     new MyThread().start();



                                                        67
                    An example
class MyThread extends Thread {
      public void run() {
            System.out.println(" this thread is running ... ");
      }
}

class ThreadEx1 {
      public static void main(String [] args ) {
        MyThread t = new MyThread();
         t.start();
     }
}




                                                                  68
          2nd method: Threads by
      implementing Runnable interface
   Create a class that implements the interface Runnable and
    override run() method:
class MyThread implements Runnable
{
  .....
  public void run()
  {
      // thread body of execution
  }
}
 Creating Object:
    MyThread myObject = new MyThread();
 Creating Thread Object:
    Thread thr1 = new Thread( myObject );
 Start Execution:
    thr1.start();




                                                                69
                   An example
class MyThread implements Runnable {
      public void run() {
            System.out.println(" this thread is running ... ");
      }
}

class ThreadEx2 {
      public static void main(String [] args ) {
            Thread t = new Thread(new MyThread());
             t.start();
      }
}




                                                                  70
     Java Threading API
  new
           start()
                                                       I/O completed

                       ready
                                Time expired/   suspend()
notify()                         interrupted

                                  sleeping        blocked
waiting
                dispatch
                           sleep()
   wait()                                   suspend()
                     running                     Block on I/O
                               completion

                               stop()           dead
                                                                       71
         Three threads example
   class A extends Thread
   {
       public void run()
         {
            for(int i=1;i<=5;i++)
             {
                 System.out.println("\t From ThreadA: i= "+i);
             }
              System.out.println("Exit from A");
         }
   }

   class B extends Thread
   {
       public void run()
         {
            for(int j=1;j<=5;j++)
             {
                 System.out.println("\t From ThreadB: j= "+j);
             }
              System.out.println("Exit from B");
         }
   }




                                                                  72
   class C extends Thread
   {
       public void run()
         {
            for(int k=1;k<=5;k++)
             {
                 System.out.println("\t From ThreadC: k= "+k);
             }

             System.out.println("Exit from C");
        }
   }

   class ThreadTest
   {
         public static void main(String args[])
          {
               new A().start();
               new B().start();
               new C().start();
          }
   }




                                                                  73
                                  Run 1
    [raj@mundroo] threads [1:76] java ThreadTest
       From ThreadA: i= 1
       From ThreadA: i= 2
       From ThreadA: i= 3
       From ThreadA: i= 4
       From ThreadA: i= 5
Exit from A
       From ThreadC: k= 1
       From ThreadC: k= 2
       From ThreadC: k= 3
       From ThreadC: k= 4
       From ThreadC: k= 5
Exit from C
       From ThreadB: j= 1
       From ThreadB: j= 2
       From ThreadB: j= 3
       From ThreadB: j= 4
       From ThreadB: j= 5
Exit from B




                                                    74
                                   Run2
[raj@mundroo] threads [1:77] java ThreadTest
       From Thread A: i= 1
       From Thread A: i= 2
       From Thread A: i= 3
       From Thread A: i= 4
       From Thread A: i= 5
       From Thread C: k= 1
       From Thread C: k= 2
       From Thread C: k= 3
       From Thread C: k= 4
       From Thread C: k= 5
Exit from Thread C
       From Thread B: j= 1
       From Thread B: j= 2
       From Thread B: j= 3
       From Thread B: j= 4
       From Thread B: j= 5
Exit from Thread B
Exit from Thread A




                                               75
                   Process Parallelism
         int add (int a, int b, int & result)
         // function stuff
         int sub(int a, int b, int & result)             Data
         // function stuff                   Processor
                                       IS1                 a
                                               add
pthread t1, t2;
                                                           b
pthread-create(&t1, add, a,b, & r1);                       r1
                                             Processor
pthread-create(&t2, sub, c,d, & r2);                        c
pthread-par (2, t1, t2);               IS2
                                               sub         d
                                                           r2
          MISD and MIMD Processing
                                                                 76
                           Data Parallelism
           sort( int *array, int count)                        Data
           //......
                                                    Processor
           //......                                             do
                                                      Sort
                                                                  “
pthread-t, thread1, thread2;                                      “
“                                              IS
“                                                                dn/2
pthread-create(& thread1, sort, array, N/2);
pthread-create(& thread2, sort, array, N/2);        Processor
pthread-par(2, thread1, thread2);
                                                                dn2/+1
                                                      Sort        “
                                                                  “
                                                                 dn
                    SIMD Processing
                                                                         77
                   Thread Priority

   In Java, each thread is assigned priority, which
    affects the order in which it is scheduled for
    running. The threads so far had same default
    priority (NORM_PRIORITY) and they are served
    using FCFS policy.
       Java allows users to change priority:
            ThreadName.setPriority(intNumber)
                 MIN_PRIORITY = 1
                 NORM_PRIORITY=5
                 MAX_PRIORITY=10



                                                   78
         Thread Priority Example
class A extends Thread
{
    public void run()
     {
         System.out.println("Thread A started");
         for(int i=1;i<=4;i++)
           {
               System.out.println("\t From ThreadA: i= "+i);
           }
            System.out.println("Exit from A");
     }
}
class B extends Thread
{
     public void run()
       {
          System.out.println("Thread B started");
          for(int j=1;j<=4;j++)
            {
                System.out.println("\t From ThreadB: j= "+j);
            }
             System.out.println("Exit from B");
       }
}



                                                                79
           Thread Priority Example
class C extends Thread
{
     public void run()
       {
           System.out.println("Thread C started");
           for(int k=1;k<=4;k++)
             {
                  System.out.println("\t From ThreadC: k= "+k);
             }
              System.out.println("Exit from C");
       }
}
class ThreadPriority
{
        public static void main(String args[])
         {
                  A threadA=new A();
                  B threadB=new B();
                  C threadC=new C();
                threadC.setPriority(Thread.MAX_PRIORITY);
                threadB.setPriority(threadA.getPriority()+1);
                threadA.setPriority(Thread.MIN_PRIORITY);
                System.out.println("Started Thread A");
                 threadA.start();
                System.out.println("Started Thread B");
                 threadB.start();
                System.out.println("Started Thread C");
                 threadC.start();
                 System.out.println("End of main thread");
         }
}

                                                                  80
                    Synchronization

   If one thread tries to read the data and other
    thread tries to update the same data, it leads to
    inconsistent state.
   This can be prevented by synchronising access
    to the data.
   Use “Synchronized” method:
       public synchronized void update()
       {
               …
       }


                                                   81
  Monitor (shared object access):
serializes operation on shared object
  class Account { // the 'monitor'
    int balance;

      // if 'synchronized' is removed, the outcome is unpredictable
       public synchronized void deposit( ) {
          // METHOD BODY : balance += deposit_amount;
        }

        public synchronized void withdraw( ) {
          // METHOD BODY: balance -= deposit_amount;
        }
        public synchronized void enquire( ) {
          // METHOD BODY: display balance.
        }
  }


                                                                      82
    the driver: 3rd Threads sharing
            the same object
class InternetBankingSystem {
     public static void main(String [] args ) {
        Account accountObject = new Account ();
       Thread t1 = new Thread(new MyThread(accountObject));
       Thread t2 = new Thread(new YourThread(accountObject));
       Thread t3 = new Thread(new HerThread(accountObject));
        t1.start();
        t2.start();
        t3.start();
      // DO some other operation
    } // end main()
}

                                                                83
             Shared account object
               between 3 threads
class MyThread implements Runnable {
 Account account;
      public MyThread (Account s) { account = s;}
      public void run() { account.deposit(); }
} // end class MyThread

class YourThread implements Runnable {                accoun
 Account account;
      public YourThread (Account s) { account = s;}   t
      public void run() { account.withdraw(); }
} // end class YourThread
                                                      (shared
                                                      object)
class HerThread implements Runnable {
 Account account;
      public HerThread (Account s) { account = s; }
      public void run() {account.enquire(); }
} // end class HerThread
                                                                84
        Multithreaded Server   Multithreaded Server


                               Server Process
         Client Process
                                     Server
                                    Threads

Client Process


                                               User Mode

                                              Kernel Mode
                  Message Passing
                     Facility



                                                            85
 Assignment 1: Multithreaded MathServer –
Demonstrates the use of Sockets and Threads



  A Client Program       “sqrt 4.0”
  What is sqrt(10)?                      Multithreaded
                                           MathServer
                              “2.0”
                                      (sin, cos, sqrt, etc.)
  A Client Program
  What is sin(10)?




              A Client
           Program in “C”                      A Client
           What is sin(10)?                Program in “C++”
                                            What is sin(10)?

                                                               86
A Multithreaded Program
                  MathThreads


          start     start       start




MathSin           MathCos               MathTan




           join      join        join



                  MathThreads

                                                  87
        The peer model

        Program                     Resources
                  Workers
Input              taskX
                            Files

                                    Databases
                   taskY



                            Disks
                   taskZ

                                            Special
                                            Devices




                                                      88
                            A thread
                 A thread pipeline pipeline

    Program      Filter Threads
                  Stage 1           Stage 2           Stage 3

Input (Stream)




    Resources           Files             Files                 Files

                        Databases         Databases             Databases


                            Disks             Disks                Disks



                 Special Devices    Special Devices    Special Devices



                                                                            89
             Advanced Topics

   Multithreding on Uni-Processors
     Process Concurrency

      P1

      P2                                           CPU

      P3



                            time


Number of Simultaneous execution units > number of CPUs
                                                          90
  Multi-Processing (clusters & grids)
   and Multi-Threaded Computing
        Threaded Libraries, Multi-threaded I/O

                              Application

         Application                                  Application

Application
                                         CPU
                             CPU
                     CPU                       CPU        CPU        CPU

              Better Response Times in         Higher Throughput for
              Multiple Application             Parallelizeable Applications
              Environments
                                                                         91

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:10/3/2011
language:English
pages:91