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

ruby 线程id_Ruby中的线程相关推荐

  1. 一个线程池中的线程异常了,那么线程池会怎么处理这个线程?

    一个线程池中的线程异常了,那么线程池会怎么处理这个线程? 参考文章: (1)一个线程池中的线程异常了,那么线程池会怎么处理这个线程? (2)https://www.cnblogs.com/fangua ...

  2. Java 线程池中的线程复用是如何实现的?

    前几天,技术群里有个群友问了一个关于线程池的问题,内容如图所示: 关于线程池相关知识可以先看下这篇:为什么阿里巴巴Java开发手册中强制要求线程池不允许使用Executors创建? 那么就来和大家探讨 ...

  3. linux下c语言线程传参数,【linux】C语言多线程中运行线程池,在线程池中运行线程池,,传递的结构体参数值为空/NULL/0...

    C语言多线程中运行线程池,在线程池中运行线程池,,传递的结构体参数值为空/NULL/0 本贴问题,之前已经提问过一次,当时已经解决了,原贴在这里https://segmentfault.com/q/1 ...

  4. python 判断线程是否执行完毕_判断线程池中的线程是否全部执行完毕

    在使用多线程的时候有时候我们会使用 java.util.concurrent.Executors的线程池,当多个线程异步执行的时候,我们往往不好判断是否线程池中所有的子线程都已经执行完毕,但有时候这种 ...

  5. 判断线程池中的线程是否全部执行完毕

    在使用多线程的时候有时候我们会使用 java.util.concurrent.Executors的线程池,当多个线程异步执行的时候,我们往往不好判断是否线程池中所有的子线程都已经执行完毕,但有时候这种 ...

  6. java线程池newfi_Java 线程池中的线程复用是如何实现的?

    前几天,技术群里有个群友问了一个关于线程池的问题,内容如图所示: 那么就来和大家探讨下这个问题,在线程池中,线程会从 workQueue 中读取任务来执行,最小的执行单位就是 Worker,Worke ...

  7. Java 确定线程池中工作线程数的大小

    以问答形式展开,会更有针对性: 1.工作线程是不是越多越好? 不是.a.服务器cpu核数有限,所以同时并发或者并行的线程数是有限的,所以1核cpu设置1000个线程是没有意义的. b.线程切换也是有开 ...

  8. 判断线程池中某个线程是否执行完成

    目录 1.先写结果 2.判断某个线程是否执行完成(不使用线程池) 3.在线程池中不能使用isAlive判断线程状态的原因 3-1.错误示例 3-2.创建线程工厂 3-3.创建线程方法(ThreadPo ...

  9. c语言数据库线程池,C语言多线程中运行线程池,在线程池中运行线程池,,传递的结构体参数值为空/NULL/0...

    typedef struct { }LoanInfos; typedef struct{ int cp;//主线程编号 int thread;//线程编号 long int time; int arr ...

最新文章

  1. 第五周周记(国庆第三天)
  2. web移动端_移动端的轮播
  3. Widget上实时刷新图片,造成anr问题
  4. JS 给某个DIV增加CLASS样式名
  5. 小白自学深度学习——目录
  6. matlab源代码 语义相似度计算,如何计算两个句子之间的相似度(句法和语义)...
  7. java byte[] 文件流 转换成string是乱码_Java学习--IO(二)、多线程
  8. 在spring中使用JdbcTemplate进行数据库管理操作
  9. Maven学习总结(53)——利用Maven插件构建镜像进行持续交付中的版本号管理
  10. procise 时钟的坑
  11. python怎么定义一个变量为空列表_python – 为什么一个类变量没有在列表理解中定义,但另一个是?...
  12. poj 3040 Allowance (贪心
  13. rvm,ruby的安装
  14. 《商务与经济统计》要点回顾笔记
  15. 奥维地图从服务器获取信息超时,2021奥维地图不能用了原因及解决办法
  16. Unison 的相关参数介绍
  17. hue oozie rerun使用问题记录
  18. ..\OBJ\PRESSURE_SYSTEM.axf: Error: L6218E: Undefined symbol FLASH_ErasePage (referred from flash.o).
  19. 鲁大师2023年Q3电脑排行:三足鼎立,PC圈一场旷日持久之战即将开启
  20. Studio 3T 破解教程 mogodb

热门文章

  1. 计算机专业常用图论,同等学力申硕计算机专业--数学公式集合(新增学习笔记)...
  2. php 验证码文件,php实现的验证码文件类实例
  3. python图标icon_用Python提取exe图标icon
  4. rip协议中周期性广播路由信息的报文_技术实操||距离矢量路由协议-RIP
  5. elementUI之switch应用的坑
  6. (1-1)line-height的定义和行内框盒子模型
  7. 转载:pycharm最新版新建工程没导入本地包问题:module 'selenium.webdriver' has no attribute 'Firefox'...
  8. Android 获取ROOT权限原理解析
  9. 解决git clone报错SSL certificate problem
  10. JS-取出字符串中重复次数最多的字符并输出