Skip to content

Instantly share code, notes, and snippets.

@gouravtiwari
Last active August 12, 2018 14:40
Show Gist options
  • Save gouravtiwari/8fb2a5421b30a305f07d1c5a5536f876 to your computer and use it in GitHub Desktop.
Save gouravtiwari/8fb2a5421b30a305f07d1c5a5536f876 to your computer and use it in GitHub Desktop.
Simple Queue
class SimpleQueue
attr_accessor :queue
def initialize
@queue = []
end
def enqueue obj
queue << obj
end
def dequeue
queue.shift
end
def status
queue
end
end
my_queue = SimpleQueue.new
my_queue.enqueue("1: square of 50")
my_queue.enqueue("2: multiplication of 12 and 15")
my_queue.enqueue("3: addition of 50 and 27")
my_queue.enqueue("4: plan for a friend's birthday")
THREAD_POOL_COUNT = 2
threads = []
THREAD_POOL_COUNT.times do
threads << Thread.new do
while value = my_queue.dequeue
p "processing: #{value}"
end
end
end
threads.join
p my_queue
# Output here would be something like this, depending on which process was completed first:
# > processing: 1: square of 50
# > #<SimpleQueue:0x007fd67321ce50 @queue=["1: square of 50", "2: multiplication of 12 and 15", "3: addition of 50 and 27", "4: plan for a friend's birthday"]>
# > processing: 2: multiplication of 12 and 15
# > processing: 3: addition of 50 and 27
# > processing: 4: plan for a friend's birthday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment