ruby多线程_Ruby中的多线程

ruby多线程

Ruby多线程 (Ruby Multithreading)

If we talk about a normal program in Ruby, all statements are executed sequentially because it used to have a single thread commonly known as the main thread. But our program may have more than one thread and this concept is known as multithreading. The advantage of multithreading is that our program uses less memory space and share the same address. With the help of this concept, we can perform more than one task at the same instant of time. We know that we can create a new thread with the help of thread.new keyword.

如果我们谈论的是Ruby中的普通程序,则所有语句都将按顺序执行,因为它曾经只有一个通常称为主线程的线程。 但是我们的程序可能有多个线程,这一概念被称为多线程多线程的优点是我们的程序使用更少的内存空间并共享相同的地址。 借助这一概念,我们可以在同一时间执行多个任务。 我们知道可以借助thread.new关键字创建一个新线程。

Now, let us see how we create multiple threads with the help of the examples provided below:

现在,让我们看看如何借助下面提供的示例创建多个线程:

=begin
Ruby program to demonstrate multithreading
=end

def Includehelp1 
	a = 0
	while a <= 10
		puts "Thread execution: #{a}"
		sleep(1) 
		a = a + 1
	end
end

def Includehelp2 
	b = 0
	while b <= 5
		puts "Thread2: #{b}"
		sleep(2) 
		b = b + 1
	end
end

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

Output

输出量

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

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

In the above example, you can observe that both the outputs are different even though there is no change in the program. This is because the execution of the thread is carried out based on their priority and by default, all the threads have equal priority. Their execution is dependent on the pointer. You can also observe that both the threads are executing at once.

在上面的示例中,即使程序没有变化,您也可以观察到两个输出都是不同的。 这是因为线程的执行是基于它们的优先级执行的,并且默认情况下,所有线程都具有相同的优先级。 它们的执行取决于指针。 您还可以观察到两个线程同时执行。

Threads can also use the same resource in the program, refer to the following example,

线程也可以在程序中使用相同的资源,请参考以下示例,

=begin
Ruby program to demonstrate multithreading	
using same resource
=end

$g_var = 10

def Includehelp1 
	a = 0
	while a <= 10
		puts "Thread execution: #{a}"
		sleep(1) 
		a = a + 1
	end
	puts "Inside Thread 1 ; #{$g_var+19}"
end

def Includehelp2 
	b = 0
	while b <= 5
		puts "Thread2: #{b}"
		sleep(2) 
		b = b + 1
	end
	puts "Inside Thread 2 ; #{$g_var+10}"
end

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

Output

输出量

Thread execution: 0
Thread2: 0
Thread execution: 1
Thread execution: 2
Thread2: 1
Thread execution: 3
Thread2: 2
Thread execution: 4
Thread execution: 5
Thread2: 3
Thread execution: 6
Thread execution: 7
Thread2: 4
Thread execution: 8
Thread execution: 9
Thread2: 5
Thread execution: 10
Inside Thread 1 ; 29
Inside Thread 2 ; 20

In the above code, you can observe that there exists a global variable name as $g_var. Both the threads can access it but when they are manipulating its value, it is not creating any impact on the value manipulated by other threads.

在上面的代码中,您可以观察到存在一个名为$ g_var的全局变量名称。 两个线程都可以访问它,但是当它们操纵它的值时,它不会对其他线程操纵的值产生任何影响。

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

ruby多线程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值