1 / 10

Multithreading

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

foxcharles
Download Presentation

Multithreading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multithreading Dr. John P. Abraham

  2. Multithreading • Parallel execution • Downloading a video file and playing • Checking spelling while typing • System.threading • Thread class and monitor class

  3. 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

  4. 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.

  5. Thread Priorities and scheduling • Every thread is given a priority between lowest to highest. • Timeslicing is used among threads of equal priority.

  6. 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

  7. 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

  8. 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.

  9. 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.

  10. 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.

More Related