110 likes | 135 Vues
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
E N D
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. • WaitSleepJoin 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 the thread in waitSleepJoin state. • Suspend works similar the wait SleepJoin 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 threadClass • 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.CurrentThread • Console.WriteLine(Now.Second & ":" & Now.Millisecond & " " & current.Name & " going to sleep for " & sleeptime) • Thread.Sleep(sleeptime) • Console.WriteLine(Now.Second & ":" & Now.Millisecond & " " & current.Name & " done sleeping ") • End Sub • End Class
Thread tester • Imports System.Threading • Module threadTester • Sub main() • Dim messenger1 As New threadClass • Dim messenger2 As New threadClass • Dim messenger3 As New threadClass • Dim thread1 As New Thread(AddressOf messenger1.report) • Dim thread2 As New Thread(AddressOf messenger2.report) • Dim thread3 As New Thread(AddressOf messenger3.report) • thread1.Name = "thread1" • thread2.Name = "thread2" • thread3.Name = "thread3" • Console.WriteLine(Now.Second & ":" & Now.Millisecond & " Starting threads ") • thread1.Start() • thread2.Start() • thread3.Start() • Console.WriteLine(Now.Second & ":" & Now.Millisecond & " Threads started" & vbCrLf) • 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 SyncBlock that maintains the state of the object’s lock. The monitor checks this state to determine the state of the lock.