简体   繁体   English

Perl与Java中的多线程

[英]Multi-Threading in Perl vs Java

I am new to Perl and I am writing a program that requires the use of threading. 我是Perl的新手,正在编写一个需要使用线程的程序。 I have found the threads feature in Perl, but I find that I am still a bit confused. 我已经在Perl中找到了线程功能 ,但是我仍然感到有些困惑。 As stated in the title of this post Java is probably the most common to use threads. 如本文标题所述,Java可能是最常使用的线程。 I am not saying that it is perfect, but I can get the job done using the Thread class. 我并不是说这是完美的,但是我可以使用Thread类完成工作。

In Java I have a method called startThreads and in that method I created the threads and then started them. 在Java中,我有一个名为startThreads的方法,在该方法中,我创建了线程,然后启动了它们。 After starting them there is a while loop that is checking if the treads are done. 启动它们之后,有一个while循环正在检查胎面是否完成。 If all the threads have exited properly then the while loop exits, but if not then the while loop is watching what threads have timed out and then safely interrupts those threads and sets their shutdown flags to true. 如果所有线程都已正确退出,则while循环将退出,但如果不是,则while循环将监视哪些线程已超时,然后安全地中断这些线程并将其关闭标志设置为true。

The problem: In Perl I want to use the same algorithm that I have stated above, but of course there are differences in Perl and I am new to Perl. 问题:在Perl中,我想使用与上面所述相同的算法,但是Perl当然有所不同,并且我是Perl的新手。 Is it possible to have the while loop running while the other threads are running? 是否有可能在其他线程正在运行时运行while循环? How is it done? 怎么做?

You can implement your while loop in Perl using threads->list() but you should consider a different approach, first. 您可以使用threads->list()在Perl中实现while循环,但首先应考虑其他方法。

Instead of waiting for threads, how about waiting for results? 不用等待线程,而是等待结果呢?

The basic idea here is that you have code which takes work units from a queue and which puts the results of the work units (either objects or exceptions) into a output queue. 此处的基本思想是,您具有从队列中获取工作单元并将工作单元的结果(对象或异常)放入输出队列的代码。

Start a couple of threads that wait for work units in the input queue. 启动几个线程,等待输入队列中的工作单元。 when one show up, run the work unit and put the result in the output queue. 当出现时,运行工作单元并将结果放入输出队列。

In your main code, you just need to put all the N work units into the input queue (make sure it's large enough). 在您的主代码中,您只需要将所有N个工作单元放入输入队列(确保足够大)。 After that, you can wait for N outputs in the output queue and you're done without needing to worry about threads, joins and exceptions. 之后,您可以等待输出队列中的N个输出,并且不必担心线程,联接和异常。

[EDIT] All your questions should be answered in http://perldoc.perl.org/perlthrtut.html [编辑]您的所有问题都应在http://perldoc.perl.org/perlthrtut.html中回答

Be careful when using threads in perl. 在perl中使用线程时要小心。 There are a lot of nuances about safely accessing shared data. 关于安全访问共享数据有很多细微差别。 Because most Perl code does not do threading, very few modules are thread-safe. 由于大多数Perl代码不执行线程处理,因此很少有模块是线程安全的。

After reading the link that Michael Slade provided, I found that Cyber-Guard Enterprise was correct. 阅读了Michael Slade提供的链接后,我发现Cyber​​-Guard Enterprise是正确的。 By detaching the thread the main still is performing work. 通过分离线程,主体仍在执行工作。 I haven't tested it out yet, but it looks like $thr->is_running() can tell me if the thread is still running. 我还没有测试过,但是看起来$ thr-> is_running()可以告诉我线程是否还在运行。

This was taken from the url that was provided and shows how detach is used. 这是从提供的URL中获取的,并显示了如何使用分离。 perldoc.perl.org/perlthrtut.html? perldoc.perl.org/perlthrtut.html?

use threads;
my $thr = threads->create(\&sub1); # Spawn the thread
$thr->detach(); # Now we officially don't care any more
sleep(15); # Let thread run for awhile
sub sub1 {
$a = 0;
while (1) {
$a++;
print("\$a is $a\n");
sleep(1);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM