1.线程池中空闲的终止终止线程处于什么状态?
2.Qt——QThread源码浅析
3.ç¨vb6å¯ä¸å¯ä»¥è®¾è®¡å¤çº¿ç¨åºçåºç¨ç¨åºï¼å¦ä½å®ç°
线程池中空闲的线程处于什么状态?
一:阻塞状态,线程并没有销毁,也没有得到CPU时间片执行;
源码追踪:
for (;;) {
...
workQueue.take();
...
}
public E take()...{
...
while (count.get() == 0) { / /这里就是任务队列中的消息数量
notEmpty.await();
}
...
}
public final void await()...{
...
LockSupport.park(this);
...
}
继续往下:
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
U.park(false, 0L);
setBlocker(t, null);
}
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
//线程调用该方法,线程将一直阻塞直到超时,线程线程或者是源码中断条件出现。
public native void park(boolean isAbsolute,终止终止 long time);
上面就是java线程池中阻塞的源码追踪;
二.对比object的wait()方法:
@FastNative
public final native void wait(long timeout, int nanos) throws InterruptedException;
还有Thread的sleep() 方法:
@FastNative
private static native void sleep(Object lock, long millis, int nanos)throws...;
可见,线程池中使用的阻塞方式并不是Object中的wait(),也不是Thread.sleep() ;
这3个方法最终实现都是通过c&c++实现的native方法.
三.在<<Java虚拟机(第二版)>>中,对线程状态有以下介绍:
.4.3 状态转换
Java语言定义了5种线程状态,在任意一个时间点,线程线程一个线程只能有且只有其中的源码正数的源码和补码是相同的一种
状态,这5种状态分别如下。终止终止
1)新建(New):创建后尚未启动的线程线程线程处于这种状态。
2)运行(Runable):Runable包括了操作系统线程状态中的源码Running和Ready,也就是终止终止处于此
状态的线程有可能正在执行,也有可能正在等待着CPU为它分配执行时间。线程线程
3)无限期等待(Waiting):处于这种状态的源码线程不会被分配CPU执行时间,它们要等待被
其他线程显式地唤醒。终止终止以下方法会让线程陷入无限期的线程线程等待状态:
●没有设置Timeout参数的Object.wait()方法。
●没有设置Timeout参数的源码分红盘网站源码Thread.join()方法。
●LockSupport.park()方法。
4)限期等待(Timed Waiting):处于这种状态的线程也不会被分配CPU执行时间,不过无
须等待被其他线程显式地唤醒,在一定时间之后它们会由系统自动唤醒。以下方法会让线程
进入限期等待状态:
●Thread.sleep()方法。
●设置了Timeout参数的Object.wait()方法。
●设置了Timeout参数的乐博电玩 源码Thread.join()方法。
●LockSupport.parkNanos()方法。
●LockSupport.parkUntil()方法。
5)阻塞(Blocked):线程被阻塞了,“阻塞状态”与“等待状态”的区别是:“阻塞状态”在等
待着获取到一个排他锁,这个事件将在另外一个线程放弃这个锁的时候发生;而“等待状
态”则是在等待一段时间,或者唤醒动作的发生。在程序等待进入同步区域的锁屏android源码时候,线程将
进入这种状态。
结束(Terminated):已终止线程的线程状态,线程已经结束执行。
Qt——QThread源码浅析
在探索Qt的多线程处理中,QThread类的实现源码历经变迁。在Qt4.0.1和Qt5.6.2版本中,尽管QThread类的jvm.dll 源码声明相似,但run()函数的实现有所不同。从Qt4.4开始,QThread不再是抽象类,这标志着一些关键调整。
QThread::start()函数在不同版本中的核心代码保持基本一致,其中Q_D()宏定义是一个预处理宏,用于获取QThread的私有数据。_beginthreadex()函数则是创建线程的核心,调用QThreadPrivate::start(this),即执行run()函数并发出started()信号。
QThread::run()函数在Qt4.4后的版本中,不再强制要求重写,而是可以通过start启动事件循环。在Qt5.6.2版本中,run函数的定义更灵活,可以根据需要进行操作。
关于线程停止,QThread提供了quit()、exit()和terminate()三种方式。quit()和exit(0)等效,用于事件循环中停止线程,而terminate()则立即终止线程,但不推荐使用,因为它可能引发不稳定行为。
总结起来,QThread的核心功能包括线程的创建、run函数的执行以及线程的结束控制。从Qt4.4版本开始,QThread的使用变得更加灵活,可以根据需要选择是否重写run函数,以及如何正确地停止线程。不同版本间的细微差别需要开发者注意,以确保代码的兼容性和稳定性。
ç¨vb6å¯ä¸å¯ä»¥è®¾è®¡å¤çº¿ç¨åºçåºç¨ç¨åºï¼å¦ä½å®ç°
å¤çº¿ç¨æ¯å¯ä»¥åï¼åªæ¯ä¸å¤ªå®å ¨ï¼å®¹æéæ³æä½ï¼
æºä»£ç å¦ä¸ï¼
çªä½ä¸ç代ç ï¼
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 "å¤çº¿ç¨æ£å¨è¿è¡...ï¼ççå¾çæ¡æ§ä»¶çåè²ææï¼", , "ä¿¡æ¯"
'ç»æ¢çº¿ç¨è¿è¡
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 "kernel" () 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
DoEvents
Bkgcolor = Longcounter Mod
Form1.Picture1.BackColor = RGB(Bkgcolor, 0, 0)
LongTick = GetTickCount
While GetTickCount - LongTick < '延æ¶æ¯«ç§,ä¸å
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
DoEvents
Bkgcolor = Longcounter Mod
Form1.Picture2.BackColor = RGB(0, Bkgcolor, 0)
LongTickValue = GetTickCount
While GetTickCount - LongTickValue <
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
DoEvents
Bkgcolor = Longcounter Mod
Form1.Picture3.BackColor = RGB(0, 0, Bkgcolor)
LongTick = GetTickCount
While GetTickCount - LongTick <
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 .5. Copyright(C).Ldt By CY-soft --
' Email:4y4ycoco@.com
' Test Onï¼ VB6.0+Win AND VB6.0+WinXP It's Pass ï¼
Option Explicit
'å建线ç¨API
'æ¤APIç»è¿æ¹é ï¼lpThreadAttributesæ¹ä¸ºAnyåï¼lpStartAddressæ¹ä¸ºä¼ å¼å¼ç¨ï¼
'å 为å½æ°çå ¥å£å°åç±å½¢ååéä¼ éï¼å¦æç¨ä¼ åé£å°ä¼ éå½¢ååéçå°åèä¸æ¯å½æ°çå ¥å£å°å
Private Declare Function CreateThread Lib "kernel" (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 "kernel" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
'æ¿æ´»çº¿ç¨API
Private Declare Function ResumeThread Lib "kernel" (ByVal hThread As Long) As Long
'æ起线ç¨API
Private Declare Function SuspendThread Lib "kernel" (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 "线ç¨å建失败ï¼", , "é误"
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
2024-12-24 11:07
2024-12-24 10:43
2024-12-24 10:33
2024-12-24 10:11
2024-12-24 09:57
2024-12-24 09:30
2024-12-24 09:04
2024-12-24 08:57