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 =endthr = Thread.new{puts "Hello! Message from Includehelp"} thr.joinOutput
輸出量
Hello! Message from IncludehelpIn 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 =enddef Includehelp1 a = 0 while a <= 10puts "Thread execution: #{a}"sleep(1) a = a + 1end endx = Thread.new{Includehelp1()} x.joinOutput
輸出量
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: 10In 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 =enddef myname for i in 1...10p = rand(0..90)puts "My name is Vaibhav #{p}"sleep(2) end endx = Thread.new{myname()} x.joinOutput
輸出量
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 52In 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中的线程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多带带飞多少钱一只啊?
- 下一篇: 极米投影仪怎么挂墙上