Skip to content

Instantly share code, notes, and snippets.

@LuPoYi
Created May 6, 2021 13:52
Show Gist options
  • Save LuPoYi/ca9a993f99dab540da02a9d9ba16dc81 to your computer and use it in GitHub Desktop.
Save LuPoYi/ca9a993f99dab540da02a9d9ba16dc81 to your computer and use it in GitHub Desktop.
require 'thread'
module TT
def self.tester
@@queue1 = Queue.new
@@queue2 = Queue.new
@@package = []
#宇宙無敵快的thread
@@fast_thread = Thread.new do
loop do
@@queue1.push(rand) if Time.now.to_i / 6 % 2 == 0 #模擬需求產生中斷頻率
sleep(0.00333333) #可以嘗試把這行拿掉 ...
end
end
#收斂打包器
@@package_thread = Thread.new do
loop do
t = Time.now.to_f
package = []
begin
while data = @@queue1.pop(true)
package << data
break if Time.now.to_f - t > 1
end
rescue ThreadError => e
raise e unless e.message == 'queue empty'
end
if package.empty?
print '-'
else
@@queue2 << package
end
sleep(0.333333)
end
end
#末端整理器
@@runner_thread = Thread.new do
while data = @@queue2.pop(false)
puts "run : #{data.length}"
end
end
sleep(65535) #main sleep
end
end
TT.tester
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment