Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么赢了';开始?_Ruby_Multithreading - Fatal编程技术网

Ruby 为什么赢了';开始?

Ruby 为什么赢了';开始?,ruby,multithreading,Ruby,Multithreading,我似乎无法让这个工作;为什么线程不启动启动 # encoding: utf-8 require 'socket' print "choose host: " host = gets.chomp print "choose starting port: " sport = gets.to_i print "choose ending port: " eport = gets.to_i def scanner (sport, eport, host) while sport <= ep

我似乎无法让这个工作;为什么
线程不启动
启动

# encoding: utf-8
require 'socket'
print "choose host: "
host = gets.chomp
print "choose starting port: "
sport = gets.to_i
print "choose ending port: "
eport = gets.to_i
def scanner (sport, eport, host)
    while sport <= eport
        begin
            s = TCPSocket.new(host, sport)
            if s
                puts "Port #{sport} is open!"
            end
        rescue 
            puts "Port #{sport} is closed!"
        end 
        sport += 1

    end
end
Thread.start([scanner]sport, eport, host)
编码:utf-8 需要“插座” 打印“选择主机:” host=gets.chomp 打印“选择起始端口:” 运动=到达 打印“选择结束端口:” eport=get.to_i def扫描仪(运动型、eport型、主机型)
而sport则需要从主线程加入辅助线程。发生的情况是主线程退出,这会导致整个进程退出,并在工作线程完成之前关闭它


您需要在启动工作线程后通过加入它来等待它。在您的语言线程api中查找类似于
Thread.join
或类似的函数。

不确定,但可能需要Thread.join?可能主线程退出导致整个进程在工作线程完成之前终止。@AndrewTomazos Dethomling:这正是这里发生的事情。你应该把它作为答案发布。启动一个线程然后立即加入它不是有点多余吗?这意味着相同的工作可以简单地由主线程执行。+1。。。这可以通过将
join
发送到
Thread.start
的返回值来实现,即
Thread=Thread.start(…);thread.join