Multithreading Dr John P Abraham Multithreading Parallel execution
Multithreading Dr. John P. Abraham
Multithreading • Parallel execution – Downloading a video file and playing – Checking spelling while typing • System. threading – Thread class and monitor class
Thread States • Threadstart creates the thread object in Unstarted state • Thread’s start method is called to make it Running State • Now this thread can run concurrently with other threads running. • Running thread can enter Stopped State when the threadstart terminates or by calling the abort method
Other thread states • If a thread is not given a CPU it is in the Blocked state. Example when the thread issues an I/O or during sychronization. When finished it can return to running state. • Wait. Sleep. Join state. Thread encounters code segment it can’t execute yet. The thread issues a wait to the monitor. • Another thread can invoke the monitor method pulse or pulse all, to return the thread to running state. • The sleep method can place thread in wait. Sleep. Join state. • Suspend works similar the wait Sleep. Join state.
Thread Priorities and scheduling • Every thread is given a priority between lowest to highest. • Timeslicing is used among threads of equal priority.
Creating thread class • • • • Imports System. threading Public Class thread. Class Private sleeptime As Integer Private Shared randomobject As New Random Public Sub New() sleeptime = randomobject. Next(5001) End Sub Public Sub report() Dim current As Thread = Thread. Current. Thread Console. Write. Line(Now. Second & ": " & Now. Millisecond & " " & current. Name & " going to sleep for " & sleeptime) Thread. Sleep(sleeptime) Console. Write. Line(Now. Second & ": " & Now. Millisecond & " " & current. Name & " done sleeping ") End Sub End Class
Thread tester • • • Imports System. Threading Module thread. Tester Sub main() Dim messenger 1 As New thread. Class Dim messenger 2 As New thread. Class Dim messenger 3 As New thread. Class • • • Dim thread 1 As New Thread(Address. Of messenger 1. report) Dim thread 2 As New Thread(Address. Of messenger 2. report) Dim thread 3 As New Thread(Address. Of messenger 3. report) • • thread 1. Name = "thread 1" thread 2. Name = "thread 2" thread 3. Name = "thread 3" Console. Write. Line(Now. Second & ": " & Now. Millisecond & " Starting threads ") thread 1. Start() thread 2. Start() thread 3. Start() • Console. Write. Line(Now. Second & ": " & Now. Millisecond & " Threads started" & vb. Cr. Lf) • • End Sub End Module
Explanation • Each thread displays a message indicating that it is going to sleep for a random interval from 0 to 5000 milliseconds. • When it awakens it displays its name, done sleeping and terminates.
Class monitor • Multiple threads may access shared memory • Reading is no problem • If memory is changed (writing) data corruption can occur. • Solved by giving one thread at a time exclusive access to shared memory. Other threads are kept waiting. This is called mutex or thread synchronization. • In VB thread Monitor performs sychronization.
Acquiring the lock • When a thread needs exclusive access of shared object, it invokes the monitor method Enter to acquire the lock on the data object. • Each object has Sync. Block that maintains the state of the object’s lock. The monitor checks this state to determine the state of the lock.
- Slides: 10