ruby 线程id_Ruby中的线程

ruby 线程id

Ruby线程 (Ruby Threads)

In Ruby, with the help of threads, you can implement more than one process at the same time or it can be said that Thread supports concurrent programming model. Apart from the main thread, you can create your thread with the help of the "new" keyword. Threads are light in weight and you can create your thread with the help of the following syntax:

在Ruby中,借助线程 ,您可以同时实现多个进程,或者可以说Thread支持并发编程模型。 除了主线程之外,您还可以借助“ new”关键字创建线程线程重量轻,您可以借助以下语法来创建线程:

    Thread_name = Thread.new{#statement}

Now, let us see how we can create a thread in Ruby with the help of a supporting example?

现在,让我们看看如何在一个支持示例的帮助下在Ruby中创建线程

=begin
Ruby program to demonstrate threads	
=end

thr = Thread.new{puts "Hello! Message from Includehelp"}
thr.join

Output

输出量

Hello! Message from Includehelp

In the above example, we have created a thread named as thr. Thr is an instance of Thread class and inside the thread body, we are calling puts method with the string. thr.join method is used to start the execution of the thread.

在上面的示例中,我们创建了一个名为thr 的线程 。 Thr是Thread类的实例,并且在线程体内,我们用字符串调用puts方法。 thr.join方法用于启动线程的执行。

Now, let us create a user-defined method and call it inside the thread in the following way,

现在,让我们创建一个用户定义的方法,并通过以下方式在线程内调用它:

=begin
Ruby program to demonstrate threads	
=end

def Includehelp1 
a = 0
while a <= 10
	puts "Thread execution: #{a}"

	sleep(1) 

	a = a + 1
    end
end

x = Thread.new{Includehelp1()} 
x.join

Output

输出量

Thread execution: 0
Thread execution: 1
Thread execution: 2
Thread execution: 3
Thread execution: 4
Thread execution: 5
Thread execution: 6
Thread execution: 7
Thread execution: 8
Thread execution: 9
Thread execution: 10

In the above code, we are creating an instance of Thread class names as x. Inside the thread body, we are giving a call to the user-defined method Includehelp1. We are printing the loop variable for 10 times. Inside the loop, we have accommodated the sleep method which is halting the execution for 1 second. We are starting the execution of thread with the help of the join method.

在上面的代码中,我们正在创建一个Thread类名称为x的实例。 在线程主体内部,我们正在调用用户定义的方法Includehelp1 。 我们将循环变量打印10次。 在循环内部,我们容纳了sleep方法,该方法将暂停执行1秒钟。 我们将在join方法的帮助下开始执行线程。

=begin
Ruby program to demonstrate threads	
=end

def myname 
	for i in 1...10
		p = rand(0..90)
		puts "My name is Vaibhav #{p}"
		sleep(2) 
	end
end

x = Thread.new{myname()} 
x.join

Output

输出量

My name is Vaibhav 11
My name is Vaibhav 78
My name is Vaibhav 23
My name is Vaibhav 24
My name is Vaibhav 82
My name is Vaibhav 10
My name is Vaibhav 49
My name is Vaibhav 23
My name is Vaibhav 52

In the above code, we have made a thread and printing some values inside it. We are taking help from the rand method and sleep method.

在上面的代码中,我们创建了一个线程并在其中打印一些值。 我们正在从rand方法和sleep方法获得帮助。

By the end of this tutorial, you must have understood the use of thread class and how we create its objects.

在本教程结束时,您必须已经了解线程类的用法以及我们如何创建其对象。

翻译自: https://www.includehelp.com/ruby/threads.aspx

ruby 线程id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值