Your SlideShare is downloading. ×
0
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
MELJUN CORTES Visual Basic 2005 Multithreading
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

MELJUN CORTES Visual Basic 2005 Multithreading

46

Published on

MELJUN CORTES Visual Basic 2005 Multithreading

MELJUN CORTES Visual Basic 2005 Multithreading

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
46
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
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. Visual basic 2005 IX. MULTITHREADING MELJUN CORTES
  • 2. In this chapter you will learn: •What threads are and why they are useful. •How threads enable you to implement and manage concurrent activities. •The life cycle of a thread. •Thread priorities and scheduling. •To create and execute Threads. •Thread synchronization. •What producer/consumer relationships are and how they are implemented with multithreading. •To ensure that the User Interface thread performs all GUI modifications that are initiated by other threads in a Windows application. Multithreading Objectives MULTITHREADING Visual Basic 2005 2
  • 3. Multithreading It would be nice if we could perform one action at a time and perform it well The human body performs a great variety of operations in parallel or concurrently Ironically, most programming languages do not enable programmers to specify concurrent activities Rather, programming languages generally provide only a simple set of control statements that enable programmers to perform one action at a time Overview MULTITHREADING Visual Basic 2005 3
  • 4. Multithreading The FCL provides concurrency primitives Multithreading is the ability of an application to contain "threads of execution," each of which designates a portion of a program that may execute concurrently with other threads Multithreading is available to all .NET programming languages The FCL's multithreading capabilities are located in the System.Threading namespace Overview MULTITHREADING Visual Basic 2005 4
  • 5. Multithreading At any time, a thread is said to be in one of several thread states Two classes critical for multithreaded applications are Thread and Monitor, both of the System.Threading namespace Life Cycle of a Thread MULTITHREADING Visual Basic 2005 5
  • 6. Multithreading Thread life cycle in UML state diagram: Unstarted Start Re su m e Su sp en d Running WaitSleepJoin Suspended Lo c Iss k un ue a I/O vaila Lo req ble, c ue I/O k av st com aila ble pl e tio , n t or te Ab ple m Co pt ru ter s n re ,I i All l exp e n uls erva Joi , P int p, lse lee Pu Sleep ,S ai t W Stopped Blocked Life Cycle of a Thread MULTITHREADING Visual Basic 2005 6
  • 7. Multithreading A Thread object begins its life cycle in the Unstarted state The thread remains in the Unstarted state until the Thread's Start method is called, which places the thread in the Running state A Running thread enters the Stopped (or Aborted) state when its ThreadStart delegate terminates Life Cycle of a Thread MULTITHREADING Visual Basic 2005 7
  • 8. Multithreading A thread is considered to be Blocked if it is unable to use a processor, even if one is available There are three ways in which a Running thread enters the WaitSleepJoin state: ◦ The thread can call Monitor method Wait ◦ The thread can call Thread method Sleep ◦ The thread can call Thread method Join Life Cycle of a Thread MULTITHREADING Visual Basic 2005 8
  • 9. Multithreading If a Running Thread's Suspend method is called, the Running thread enters the Suspended state A Suspended thread returns to the Running state when another thread in the program invokes the Suspended Thread's Resume method Life Cycle of a Thread MULTITHREADING Visual Basic 2005 9
  • 10. Multithreading Every thread has a priority in the range between ThreadPriority.Lowest and ThreadPriority.Highest These values come from the ThreadPriority enumeration (namespace System.Threading): ◦ ◦ ◦ ◦ ◦ Lowest BelowNormal Normal AboveNormal Highest Thread Priorities and Thread Scheduling MULTITHREADING Visual Basic 2005 10
  • 11. Multithreading The Windows operating system supports a concept called timeslicing that enables threads of equal priority to share a processor With timeslicing, each thread receives a brief burst of processor time, called a quantum, during which the thread can execute Thread Priorities and Thread Scheduling MULTITHREADING Visual Basic 2005 11
  • 12. Multithreading The job of the thread scheduler is to keep the highest-priority thread running at all times And also, to ensure that all such threads execute for a quantum in round-robin fashion Thread Priorities and Thread Scheduling MULTITHREADING Visual Basic 2005 12
  • 13. Multithreading Thread-priority scheduling, mulitlevel priority queue Ready threads Priority Highest Priority Highest A A Priority AboveNormal Priority AboveNormal B B C C Priority Normal Priority Normal Priority BelowNormal Priority BelowNormal D D Priority Lowest Priority Lowest E E F F G G Thread Priorities and Thread Scheduling MULTITHREADING Visual Basic 2005 13
  • 14. Multithreading To create a thread, we can use the following declaration: Dim thread1 As New Thread(New ThreadStart(AddressOf Class1.method)) Dim thread1 As New Thread(New ThreadStart(AddressOf Class1.method)) thread1 object variable Thread class ThreadStart delegate AddressOf keyword Method to execute in the thread Creating and Executing Threads MULTITHREADING Visual Basic 2005 14
  • 15. Multithreading Threads sleeping and printing Imports System.Threading Imports System.Threading Module ModThreadTester Module ModThreadTester Sub Main() Sub Main() ' Create and name each thread. Use MessagePrinter's ' Create and name each thread. Use MessagePrinter's ' Print method as argument to ThreadStart delegate. ' Print method as argument to ThreadStart delegate. Dim printer1 As New MessagePrinter Dim printer1 As New MessagePrinter Dim thread1 As New Thread(New ThreadStart(AddressOf printer1.Print)) Dim thread1 As New Thread(New ThreadStart(AddressOf printer1.Print)) thread1.Name = "thread1" thread1.Name = "thread1" Dim printer2 As New MessagePrinter Dim printer2 As New MessagePrinter Dim thread2 As New Thread(New ThreadStart(AddressOf printer2.Print)) Dim thread2 As New Thread(New ThreadStart(AddressOf printer2.Print)) thread2.Name = "thread2“ thread2.Name = "thread2“ Dim printer3 As New MessagePrinter Dim printer3 As New MessagePrinter Dim thread3 As New Thread(New ThreadStart(AddressOf printer3.Print)) Dim thread3 As New Thread(New ThreadStart(AddressOf printer3.Print)) thread3.Name = "thread3“ thread3.Name = "thread3“ .. .. .continued .continued Creating and Executing Threads MULTITHREADING Visual Basic 2005 15
  • 16. Multithreading Threads sleeping and printing Console.WriteLine("Starting threads in Main") Console.WriteLine("Starting threads in Main") ' call each thread's Start method to place each ' call each thread's Start method to place each ' thread in Running state ' thread in Running state thread1.Start() thread1.Start() thread2.Start() thread2.Start() thread3.Start() thread3.Start() Console.WriteLine("Threads started, Main ends" & vbCrLf) Console.WriteLine("Threads started, Main ends" & vbCrLf) Console.Read() Console.Read() End Sub End Sub .. .. .continued .continued Creating and Executing Threads MULTITHREADING Visual Basic 2005 16
  • 17. Multithreading Threads sleeping and printing Class MessagePrinter ' Print method of this class used to control threads Class MessagePrinter ' Print method of this class used to control threads Private sleepTime As Integer Private sleepTime As Integer Private Shared random As New Random Private Shared random As New Random ' constructor to initialize a MessagePrinter ' constructor to initialize a MessagePrinter Sub New() Sub New() ' pick random sleep time between 0 and 5 ' pick random sleep time between 0 and 5 sleepTime = random.Next(5001) sleepTime = random.Next(5001) End Sub ' New End Sub ' New object object seconds seconds ' method Print controls thread that prints messages ' method Print controls thread that prints messages Public Sub Print() Public Sub Print() ' obtain reference to currently executing thread ' obtain reference to currently executing thread Dim current As Thread = Thread.CurrentThread Dim current As Thread = Thread.CurrentThread Console.WriteLine(current.Name & " going to sleep for " & _ Console.WriteLine(current.Name & " going to sleep for " & _ sleepTime & " milliseconds") sleepTime & " milliseconds") Thread.Sleep(sleepTime) ' sleep for sleepTime milliseconds Thread.Sleep(sleepTime) ' sleep for sleepTime milliseconds ' display the thread's name ' display the thread's name Console.WriteLine(current.Name + " done sleeping") Console.WriteLine(current.Name + " done sleeping") End Sub ' Print End Sub ' Print End Class ' MessagePrinter End Class ' MessagePrinter End Module End Module Creating and Executing Threads MULTITHREADING Visual Basic 2005 17
  • 18. Multithreading Threads sleeping and printing Creating and Executing Threads MULTITHREADING Visual Basic 2005 18
  • 19. Multithreading Often, multiple threads of execution manipulate shared data If threads with access to shared data simply read that data, then any number of threads can access the data simultaneously without problems However, when multiple threads share data and the data is modified by one or more of the threads, then indeterminate results may occur Thread Synchronization and Class Monitor MULTITHREADING Visual Basic 2005 19
  • 20. Multithreading The problem can be solved by giving one thread at a time exclusive access to code that manipulates the shared data During this time, other threads wishing to manipulate the data are kept waiting When the thread with exclusive access to the data completes its data manipulations, one of the waiting threads is allowed to proceed Thread Synchronization and Class Monitor MULTITHREADING Visual Basic 2005 20
  • 21. Multithreading Each thread accessing the shared data excludes all other threads from doing so simultaneously, this is called mutual exclusion or thread synchronization Visual Basic programmers use the .NET Framework's monitors to perform synchronization Thread Synchronization and Class Monitor MULTITHREADING Visual Basic 2005 21
  • 22. Multithreading Class Monitor provides the methods for locking objects to implement synchronized access to shared data Locking an object means that only one thread can access that object at a time When a thread wishes to acquire exclusive control over an object, the thread invokes the Monitor method Enter to acquire the lock on the data object Thread Synchronization and Class Monitor MULTITHREADING Visual Basic 2005 22
  • 23. Multithreading Visual Basic provides another means of manipulating an object's lock, the SyncLock statement SyncLock objectReference SyncLock objectReference ' code that requires synchronization goes here ' code that requires synchronization goes here End SyncLock End SyncLock The objectReference is the same reference that normally would be passed to Monitor methods Enter, Exit, Pulse and PulseAll Thread Synchronization and Class Monitor MULTITHREADING Visual Basic 2005 23
  • 24. Multithreading The producer portion of an application generates data the consumer portion of the application uses that data A producer thread calls a produce method to generate data and place it in a shared region of memory called a buffer Producer/Consumer Relationship without Thread Synchronization MULTITHREADING Visual Basic 2005 24
  • 25. Multithreading A consumer thread calls a consume method to read the data If the producer wishes to put the next data in the buffer but determines that the consumer has not yet read the previous data from the buffer, the producer thread should call Wait Producer/Consumer Relationship without Thread Synchronization MULTITHREADING Visual Basic 2005 25
  • 26. Multithreading Producer Producer data Buffer Buffer Consumer Consumer data Producer/Consumer Relationship without Thread Synchronization MULTITHREADING Visual Basic 2005 26
  • 27. Multithreading Buffer Producer produces 1 and stores to buffer Producer produces 1 and stores to buffer 1 Consumer consumes 1 and retrieves from buffer Consumer consumes 1 and retrieves from buffer Producer produces 2 and stores to buffer Producer produces 2 and stores to buffer 2 Producer produces 3 and stores to buffer Producer produces 3 and stores to buffer 3 Consumer consumes 3 and retrieves from buffer Consumer consumes 3 and retrieves from buffer Producer produces 4 and stores to buffer Producer produces 4 and stores to buffer 4 Producer produces 5 and stores to buffer Producer produces 5 and stores to buffer 5 Consumer consumes 5 and retrieves from buffer Consumer consumes 5 and retrieves from buffer Consumer still consumes 5 and retrieves from buffer Consumer still consumes 5 and retrieves from buffer 5 Producer/Consumer Relationship without Thread Synchronization MULTITHREADING Visual Basic 2005 27
  • 28. Multithreading Buffer Occupied Count Producer produces 1 and stores to buffer Producer produces 1 and stores to buffer 1 1 Producer tries to write. Buffer full, Producer waits Producer tries to write. Buffer full, Producer waits 1 1 Consumer consumes 1 and retrieves from buffer Consumer consumes 1 and retrieves from buffer 1 0 Producer produces 2 and stores to buffer Producer produces 2 and stores to buffer 2 1 Consumer consumes 2 and retrieves from buffer Consumer consumes 2 and retrieves from buffer 2 0 Producer produces 3 and stores to buffer Producer produces 3 and stores to buffer 3 1 Producer tries to write. Buffer full, Producer waits Producer tries to write. Buffer full, Producer waits 3 1 Consumer consumes 3 and retrieves from buffer Consumer consumes 3 and retrieves from buffer 3 0 Producer/Consumer Relationship with Thread Synchronization MULTITHREADING Visual Basic 2005 28
  • 29. Multithreading There are too many interactions that occur with the operating system, the network, the user and other components, which can cause the threads to operate at different speeds When threads wait, programs can become less productive, user-interactive programs can become less responsive and network applications can suffer longer delays because the processor is not used efficiently Producer/Consumer Relationship: Circular Buffer MULTITHREADING Visual Basic 2005 29
  • 30. Multithreading To minimize the waiting for threads that share resources and operate at the same relative speeds, we can implement a circular buffer This provides extra locations in which the producer can place values (if it "gets ahead" of the consumer) and from which the consumer can retrieve those values (while it is "catching up" to the producer) Producer/Consumer Relationship: Circular Buffer MULTITHREADING Visual Basic 2005 30
  • 31. Multithreading Buffers Occupied Count All buffers empty, Consumer waits All buffers empty, Consumer waits -1 -1 -1 0 Producer produces 1 and stores to buffer Producer produces 1 and stores to buffer 1 -1 -1 1 Consumer consumes 1 and retrieves from buffer Consumer consumes 1 and retrieves from buffer 1 -1 -1 0 Producer produces 2 and stores to buffer Producer produces 2 and stores to buffer 1 2 -1 1 Consumer consumes 2 and retrieves from buffer Consumer consumes 2 and retrieves from buffer 1 2 -1 0 Producer produces 3 and stores to buffer Producer produces 3 and stores to buffer 1 2 3 1 Producer produces 4 and stores to buffer Producer produces 4 and stores to buffer 4 2 3 2 Producer produces 5 and stores to buffer Producer produces 5 and stores to buffer 4 5 3 3 Consumer consumes 3 and retrieves from buffer Consumer consumes 3 and retrieves from buffer 4 5 6 2 Producer/Consumer Relationship: Circular Buffer MULTITHREADING Visual Basic 2005 31
  • 32. Multithreading End of Multithreading MULTITHREADING Visual Basic 2005 32

×