概述
Option Explicit On
Option Strict On
Imports System.Threading
Public Class Printer
' Lock token.锁标记
Private threadLock As Object = New Object()
Public Sub PrintNumbers()
SyncLock threadLock
Console.WriteLine("-> {0} is executing PrintNumbers()",_
Thread.CurrentThread.Name)
Console.Write("Your numbers: ")
For i As Integer = 0 To 10
Dim r As Random = New Random()
Thread.Sleep(100 * r.Next(5))
Console.Write(i & ",")
Next
Console.WriteLine()
End SyncLock
End Sub
End Class
#End Region
Module Program
Sub Main()
Console.WriteLine("***** Synchronizing Threads *****")
Console.WriteLine()
Dim p As Printer = New Printer()
' Make 11 threads that are all pointing to the same
' method on the same object.
Dim threads(10) As Thread
For i As Integer = 0 To 10
threads(i) = New Thread(AddressOf p.PrintNumbers)
threads(i).Name = String.Format("Worker thread #{0}",i)
Next
' Now start each one. For Each t As Thread In threads t.Start() Next Console.ReadLine() End SubEnd Module
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!
最后
以上就是成就期待为你收集整理的vb.net 锁定计算机,vb.net 多线程与SyncLock锁的全部内容,希望文章能够帮你解决vb.net 锁定计算机,vb.net 多线程与SyncLock锁所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复