Thread already joined at **
use strict;
use threads;
use threads::shared;
my $MAXTHREAEDNUM = 20;
my $CURTHREADNUM :shared = 0;
my @threads;
sub startThread
{
## 當前新建的執行緒數加一:
{
lock($CURTHREADNUM);
$CURTHREADNUM++;
}
print"here is the $CURTHREADNUM thread!n";
sleep(3);
## 當前活動的執行緒數減一:
{
lock($CURTHREADNUM);
$CURTHREADNUM--;
}
}
{
my $threadid;#壹
for(my $i=0;$i<5;$i++)
{
while(1)
{
if ( $CURTHREADNUM < $MAXTHREAEDNUM)
{
$threadid = threads->create('startThread');
# my $threadid = threads->create('startThread'); #貳
if ( $threadid < 0 )
{
print "To Create Thread Error,Please sleep 30's,Try Again n";
}
push(@threads, $threadid);
last;
}
else
{
print "The max thread num limit, waiting 1 second,Try Again. n" ;
sleep(1);
}
}
}
my $i=0;
foreach my $thread (@threads)
{
$i++;
print "Begin to join $i: ".$thread."n";
$$thread->join();
}
}
######################################################################
# program section
open(STDERR, ">&STDOUT");
print "程式號PID:$$ n";
exit(main());
定義壹執行日誌如下:
---------------------------------------------
程式號PID:1660
Begin to join 1: REF(0x18a66a0)
here is the 1 thread!
here is the 2 thread!
here is the 3 thread!
here is the 4 thread!
here is the 5 thread!
Begin to join 2: REF(0x18a66a0)
Thread already joined at thread_test.pl line 58.
-------------------------------------------------------
可以看出 join 1和join 2的$thread值是一樣的,即REF(0x18a66a0),也就是說REF(0x18a66a0)執行緒在1處做了join後,2處試圖再做一次。
Thread already joined at thread_test.pl line 58.錯誤就可以理解了。
為什麼陣列@threads中的值是唯一的呢?(把上述指令碼 $$thread->join();去掉,可以看出@threads中各元素值均相同)
線上程建立後,即 $threadid = threads->create('startThread');返回的為1物件,定義外部變數時,只會改變變數值,不會改變其地址,
所以printer $threadid 將會為一常量值(第一個返回物件的地址),如下示:
----------------------------------------------------
程式號PID:3288
The created theadid:REF(0x18a66b8)
The created theadid:REF(0x18a66b8)
here is the 1 thread!
here is the 2 thread!
The created theadid:REF(0x18a66b8)
The created theadid:REF(0x18a66b8)
here is the 3 thread!
here is the 4 thread!
here is the 5 thread!
The created theadid:REF(0x18a66b8)
---------------------------------------------------
但printer $threadid會有變化,如下示:
----------------------------------------------------
The created theadid:threads=SCALAR(0x18a7024)
The created theadid:threads=SCALAR(0x18a703c)
here is the 1 thread!
here is the 2 thread!
The created theadid:threads=SCALAR(0x18a7054)
The created theadid:threads=SCALAR(0x18a706c)
here is the 3 thread!
here is the 4 thread!
here is the 5 thread!
The created theadid:threads=SCALAR(0x1a9bc78)
----------------------------------------------------
push(@threads, $threadid);
$threadid為threads->create('startThread');的返回物件,將物件加入陣列需要加上"",即為$threadid;
上述語句若改成push(@threads, $threadid);join將會報錯:Can't call method "join" without a package or object reference at ...
若將定義改為貳:
執行正常,結果日誌如下:
------------------------------------------------------------
程式號PID:2988
here is the 1 thread!
Begin to join 1: REF(0x18a66b8)
here is the 2 thread!
here is the 3 thread!
here is the 4 thread!
here is the 5 thread!
Begin to join 2: REF(0x275e24)
Begin to join 3: REF(0x18a6b8c)
Begin to join 4: REF(0x18a6bb0)
Begin to join 5: REF(0x18a6bd4)
-----------------------------------------------------------
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16723161/viewspace-1023254/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Swift 3必看:集合方法flatten()重新命名為joined()Swift
- already opened by ClassLoader
- efcore This MySqlConnection is already in useMySql
- Threadthread
- thread.Interrupt()與thread.Abort()thread
- 奇怪報錯資訊“db already exists with different case already have”解決方法
- warning: already initialized constant FileUtils::VERSIONZed
- Hello,Threadthread
- Thread類thread
- Thread jointhread
- tomcat one connection one thread one request one threadTomcatthread
- thread local in pythonthreadPython
- Thread.jointhread
- Thread知識thread
- Redis 啟動報錯Address already in useRedis
- Uncaught SyntaxError: Identifier 'Geometry' has already been declaredErrorIDE
- VS error LNK2005:**already defined in **.objErrorOBJ
- Hystrix Thread Pool 解析thread
- rt-thread bootloadthreadboot
- Thread類及使用thread
- Thread(執行緒)thread執行緒
- Thread原始碼剖析thread原始碼
- DUBBO Thread pool is EXHAUSTED!thread
- Android thread class & threadloopAndroidthreadOOP
- jmeter報錯“Uncaught exception in thread Thread[AWT-EventQueue-0,6,main]“JMeterExceptionthreadAI
- Swap file "/etc/sysconfig/.iptables.swp" already exists!
- kafka java.rmi.server.ExportException: Port already in useKafkaJavaServerExportException
- studio if you already have a 64-bit JDK installedJDK
- 解決 eclipse出現 Address already in use: bindEclipse
- Java-關於ThreadJavathread
- Thread的interrupt機制thread
- alter database disable thread 2Databasethread
- Thread、ThreadLocal原始碼解析thread原始碼
- git使用報錯fatal: remote origin already exists.GitREM
- FeignClientSpecification‘ could not be registered. A bean with that name has already been definedclientBean
- (RT-Thread學習筆記1)基於 CubeMX 移植 RT-Thread Nanothread筆記NaN
- Reactor中的Thread和SchedulerReactthread
- Three ways to create Multi Thread in JavathreadJava
- Thread和Runnable的區別thread