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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377008.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377008.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377008.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Dynamic web project --- AspectJ Project

本來想今天晚上 直接轉到 以前的web項目 做測試。。。可惜在eclipse 添加 aspectj的時候 提示我不是 aspectj項目。。于是我就百度了好久&#xff0c;發現好多人都和我一樣 &#xff0c; 不過我也發現了一些可以的 比如右鍵 AJDTtools --> convert to Aspectj Project ,可惜…

2013 南京邀請賽 A play the dice 求概率

1 /**2 大意&#xff1a;給定一個色子&#xff0c;有n個面&#xff0c;每一個面上有一個數字&#xff0c;在其中的m個面上有特殊的顏色&#xff0c;當擲出的色子出現這m個顏色之一時&#xff0c;可以再擲一次。。求其最后的期望3 思路&#xff1a;假設 期望為ans4 ans 1/…

擲骰子

Description: 描述&#xff1a; In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds. 在本文中&#xff0c;我們將看到一個動態的編程問題&#xff0c;該問題可以在任何采訪回合中體現。 Problem statement:…

《YOLO算法筆記》(草稿)

檢測算法回顧 5、6年前的檢測算法大體如下&#xff1a; 手動涉及特征時應該考慮的因素&#xff1a; 1、尺度不變性 2、光照不變性 3、旋轉不變性 這一步驟稱為特征工程&#xff0c;最重要的一個算法稱為sift&#xff0c;(回顧SIFT講解)體現了上述所有的觀點。 在分類的過程中…

U盤安裝Centos6.3

一 首先下載Centos6.3的光盤鏡像文件&#xff0c;網上到鏡像實在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下載個新版本的UltraISO, 在其菜單“啟動”下有“寫入硬盤鏡像“功能到&#xff0c;原來用到綠色版本是8.6.2.2011不支持&#xff0c;…

[轉]粵語固有辭彙與漢語北方話辭彙對照

本文轉自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer調用geoserver發布的地圖實現地圖的基本功能

轉自&#xff1a;http://starting.iteye.com/blog/1039809 主要實現的功能有放大&#xff0c;縮小&#xff0c;獲取地圖大小&#xff0c;平移&#xff0c;線路測量&#xff0c;面積測量&#xff0c;拉寬功能&#xff0c;顯示標注&#xff0c;移除標注&#xff0c;畫多邊形獲取經…

LLVM與Codegen技術

LLVM 百度百科 LLVM是構架編譯器(compiler)的框架系統&#xff0c;以C編寫而成&#xff0c;用于優化以任意程序語言編寫的程序的編譯時間(compile-time)、鏈接時間(link-time)、運行時間(run-time)以及空閑時間(idle-time)&#xff0c;對開發者保持開放&#xff0c;并兼容已有…

跟烏克蘭人學編程1

今天要Disable一個菜單&#xff0c;工程項目多&#xff0c;不容易找。 烏克蘭人建議我用Spy&#xff0c;將靶拖到目標窗體上就可以看到類名。轉載于:https://www.cnblogs.com/SunWentao/archive/2012/12/19/2825220.html

html網頁轉圖片_HTML圖片

html網頁轉圖片HTML圖片 (HTML Images) Images are visuals of something that look elegant. In web pages, images are used to create a good and appealing design. 圖像是外觀精美的視覺效果。 在網頁中&#xff0c;圖像用于創建良好且吸引人的設計。 The <img> ta…

Android學習拾遺

1. java中的flush()作用&#xff1a;強制將輸出流緩沖區的數據送出。 2. 文件存儲&#xff1a; 存儲到內部&#xff1a;另外使用一個class實現&#xff0c;最開始初始化用了this,后來放在這里不合適&#xff0c;改成了帶參數的構造方法。 包括存儲、讀取、追加 讀取&#xff1a…

OLAP 技術之列式存儲與數據壓縮(快查詢方法之一)

前言 列式存儲和數據壓縮&#xff0c;對于一款高性能數據庫來說是必不可少的特性。一個非常流行的觀點認為&#xff0c;如果你想讓查詢變得更快&#xff0c;最簡單且有效的方法是減少數據掃描范圍和數據傳輸時的大小&#xff0c;而列式存儲和數據壓縮就可以幫助我們實現上述兩…

sql 視圖嵌套視圖_SQL視圖

sql 視圖嵌套視圖SQL | 觀看次數 (SQL | Views) Views in SQL are virtual tables. A view also has rows and columns as theyre during a real table within the database. We will create a view by selecting fields from one or more tables present within the database.…

Postgresql多線程hashjoin(inner join)

pg hashjoin 節點大致步驟&#xff1a; 1、分塊與分桶。對一個表hash時&#xff0c;確定塊數和桶數量。&#xff08;一塊被劃分為10個元組的桶&#xff09;確定分塊號與分桶號是由hashvalue決定的。 2、執行&#xff1a; 1、順序獲取S表中所有元組&#xff0c;對每一條元組Has…

iframe實現局部刷新和回調--開篇

今天做項目遇到一個問題。就是提交表單的時候&#xff0c;驗證用戶名是否存在和驗證碼是否正確。 當驗證碼或者用戶名存在的時候。在后臺彈窗提示。可頁面原本file里面符合要求的值刷新沒了。用戶體驗不好。因為用ifream刷新技術已不是什么新鮮技術。所以網上有大把的資料可參考…

Java文件類boolean setExecutable(boolean exec_file,boolean owner_access)方法,帶示例

文件類boolean setExecutable(boolean exec_file&#xff0c;boolean owner_access) (File Class boolean setExecutable(boolean exec_file , boolean owner_access)) This method is available in package java.io.File.setExecutable(boolean exec_file , boolean owner_acc…

OLTP 系統和 OLAP 系統的核心設計思想

關于 OLTP 系統和 OLAP 系統的核心設計思想 數據存儲系統的關于查詢的典型操作&#xff1a; -- 第一種需求&#xff1a; 根據 key&#xff08;1&#xff09; 找 value&#xff08;name,age&#xff09;&#xff0c; 單點查詢 select name, age from student where id 1; stu…

虛擬機

vt-x 虛擬技術的硬盤支持。想像成“硬解碼”的東東。不是裝虛擬機必須的&#xff0c;但有它效果會好些。 vt-x檢測工具&#xff1a;securable.exe 下載地址&#xff1a;http://pan.baidu.com/s/1kTBOvzD Hardware Virtualization選項&#xff1a; no [CPU和BIOS都不支持VT] loc…

算法(轉)

歡迎自薦和推薦鏈接。 算法 優秀博客推薦&#xff1a;各種數據結構與算法知識入門經典&#xff08;不斷更新)基本算法 貪心算法&#xff1a;貪心算法 作者&#xff1a;獨酌逸醉 貪心算法精講 作者&#xff1a;3522021224 遞歸和分治&#xff1a;遞歸與分治策略 …

sjf調度算法_如何通過靜態方法預測SJF調度中未來過程的突發時間?

sjf調度算法In SJF Scheduling, CPU is assigned to the process having the smallest burst time but it can not be implemented practically, because we dont know burst time of the arrived processes in advance. 在SJF Scheduling中 &#xff0c;將CPU分配給具有最短突…