什么是多线程编程

什么是多线程编程,第1张

线程编程技术是Java语言的重要特点。多线程编程的含义是将程序任务分成几个并行的子任务。特别是在网络编程中,你会发现很多功能是可以并发执行的。比如网络传输速度较慢、用户输入速度较慢,你可以用两个独立的线程去完成这两个功能,而不影响正常的显示或其它功能。多线程是与单线程比较而言的,普通的Windows采用单线程程序结构,其工作原理是:主程序有一个消息循环,不断从消息队列中读入消息来决定下一步所要干的事情,一般是针对一个函数,只有等这个函数执行完之后,主程序才能接收另外的消息来执行。比如子函数功能是在读一个网络数据,或读一个文件,只有等读完这个数据或文件才能接收下一个消息。在执行这个子函数过程中你什么也不能干。但往往读网络数据和等待用户输入有很多时间处于等待状态,多线程利用这个特点将任务分成多个并发任务后,就可以解决这个问题。Java中的线程类1.扩展java.lang.Thread类,用它覆盖Thread类的run方法。2.生成实现java.lang.Runnable接口的类并将其它的实例与java.lang.Thread实例相关联。Thread类是负责向其它类提供线程支持的最主要的类,要使用一个类具有线程功能,在Java中只要简单地从Thread类派生一个子类就可以了扩展Thread类,如printThread.java。Thread类最重要的方法是run方法。run方法是新线程执行的方法,因此生成java.lang.Thread的子类时,必须有相应的run方法。//PrintThread.javapublic class PrintThread extends Thread//继承Tread类private int count=0//定义一个count变量用于统计打印的次数并共享变量public static void mainString args//main方法开始PrintThread p=new PrintThread//创建一个线程实例p.start//执行线程for{}//主线程main方法执行一个循环,for执行一个死循环count++System.out.printcount+″:Main\n″//主线程中打印count+“main”变量的值,并换行public void run//线程类必须有的run()方法for{}count++System.out.printcount+″:Thread\n″上面这段程序便是继承java.lang.Tread并覆盖run的方法。用Java虚拟机启动程序时,这个程序会先生成一个线程并调用程序主类的main方法。这个程序中的main方法生成新线程,连接打印“Thread”。在启动线程之后,主线程继续打印“Main”。编译并执行这个程序,然后立即按“Ctrl+C”键中断程序,你会看到上面所述的两个线程不断打印出:XXX:main…..XXX:Thread….XXX代表的是数字,也就是上面count的值。在笔者的机器上,不同时刻这两个线程打印的次数不一样,先打印20个main(也就是先执行20次主线程)再打印出50次Thread,然后再打印main……提示:为了便于查看该程序的执行结果,你可以将执行结果导入一个文本文件,然后打开这个文件查看各线程执行的情况。如运行:javac PrintThread.javaJava PrintThread1.txt第一个命令javacPrintThread.java是编译java程序,第二个是执行该程序并将结果导入1.txt文件。当然你可以直接执行命令:java

多线程是可以啊,只是不太安全,容易非法 *** 作,

源代码如下:

窗体中的代码:

Option Explicit

'开始

Private Sub Command1_Click()

On Error Resume Next

With myThreadleft

.Initialize AddressOf Fillleft'传递过程地址给线程

.ThreadEnabled = True

End With

With myThreadright

.Initialize AddressOf Fillright

.ThreadEnabled = True

End With

With myThreadbottom

.Initialize AddressOf Fillbottom

.ThreadEnabled = True

End With

MsgBox "多线程正在运行...,看看图片框控件的变色效果!", 64, "信息"

'终止线程运行

Set myThreadleft = Nothing

Set myThreadright = Nothing

Set myThreadbottom = Nothing

End Sub

'结束

Private Sub Command2_Click()

Unload Me

End Sub

模块中的代码:

Option Explicit

'时间计数API

Private Declare Function GetTickCount Lib "kernel32" () As Long

'声明cls_thread类的对象变量

Public myThreadleft As New cls_thread, myThreadright As New cls_thread, myThreadbottom As New cls_thread

Sub Main()

Load Form1

Form1.Show

End Sub

Public Sub Fillleft()

Static Bkgcolor As Long

Dim LongTick As Long, Longcounter As Long

On Error Resume Next

For Longcounter = 0 To 3000

DoEvents

Bkgcolor = Longcounter Mod 256

Form1.Picture1.BackColor = RGB(Bkgcolor, 0, 0)

LongTick = GetTickCount

While GetTickCount - LongTick <10 '延时10毫秒,下同

Wend

Next

Set myThreadleft = Nothing '如果循环结束则终止当前线程运行,下同

End Sub

Public Sub Fillright()

Static Bkgcolor As Long

Dim LongTickValue As Long, Longcounter As Long

On Error Resume Next

For Longcounter = 0 To 3000

DoEvents

Bkgcolor = Longcounter Mod 256

Form1.Picture2.BackColor = RGB(0, Bkgcolor, 0)

LongTickValue = GetTickCount

While GetTickCount - LongTickValue <10

Wend

Next

Set myThreadright = Nothing

End Sub

Public Sub Fillbottom()

Static Bkgcolor As Long

Dim LongTick As Long, Longcounter As Long

On Error Resume Next

For Longcounter = 0 To 3000

DoEvents

Bkgcolor = Longcounter Mod 256

Form1.Picture3.BackColor = RGB(0, 0, Bkgcolor)

LongTick = GetTickCount

While GetTickCount - LongTick <10

Wend

Next

Set myThreadright = Nothing

End Sub

类模块中的代码:

'功能:创建多线程类,用于初始化线程。 类名:cls_Thread

'参数:LongPointFunction 用于接收主调过程传递过来的函数地址值

'调用方法:1.声明线程类对象变量 Dim mythread as cls_Thread

' 2.调用形式:With mythread

' .Initialize AddressOf 自定义过程或函数名 '(初始化线程) .

' .ThreadEnabled = True '(设置线程是否激活)

' End With

' 3.终止调用: Set mythread = Nothing

' Crate By : 陈宇 On 2004.5.10 Copyright(C).Ldt By CY-soft 2001--2004

' Email:4y4ycoco@163.com

' Test On: VB6.0+Win98 AND VB6.0+WinXP It's Pass !

Option Explicit

'创建线程API

'此API经过改造,lpThreadAttributes改为Any型,lpStartAddress改为传值引用:

'因为函数的入口地址由形参变量传递,如果用传址那将传递形参变量的地址而不是函数的入口地址

Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, LpthreadId As Long) As Long

'终止线程API

Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long

'激活线程API

Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long

'挂起线程API

Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long

Private Const CREATE_SUSPENDED = &H4'线程挂起常量

'自定义线程结构类型

Private Type udtThread

Handle As Long

Enabled As Boolean

End Type

Private meTheard As udtThread

'初始化线程

Public Sub Initialize(ByVal LongPointFunction As Long)

Dim LongStackSize As Long, LongCreationFlags As Long, LpthreadId As Long, LongNull As Long

On Error Resume Next

LongNull = 0

LongStackSize = 0

LongCreationFlags = CREATE_SUSPENDED '创建线程后先挂起,由程序激活线程

'创建线程并返线程句柄

meTheard.Handle = CreateThread(LongNull, LongStackSize, ByVal LongPointFunction, LongNull, LongCreationFlags, LpthreadId)

If meTheard.Handle = LongNull Then

MsgBox "线程创建失败!", 48, "错误"

End If

End Sub

'获取线程是否激活属性

Public Property Get ThreadEnabled() As Boolean

On Error Resume Next

Enabled = meTheard.Enabled

End Property

'设置线程是否激活属性

Public Property Let ThreadEnabled(ByVal Newvalue As Boolean)

On Error Resume Next

'若激活线程(Newvalue为真)设为TRUE且此线程原来没有激活时激活此线程

If Newvalue And (Not meTheard.Enabled) Then

ResumeThread meTheard.Handle

meTheard.Enabled = True

Else '若激活线程(Newvalue为真)且此线程原来已激活则挂起此线程

If meTheard.Enabled Then

SuspendThread meTheard.Handle

meTheard.Enabled = False

End If

End If

End Property

'终止线程事件

Private Sub Class_Terminate()

On Error Resume Next

Call TerminateThread(meTheard.Handle, 0)

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11003623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存