Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 54 lines (39 sloc) 934 Bytes
require_relative "../lib/chopstick"
require_relative "../lib/table"
class Philosopher
def initialize(name)
@name = name
end
def dine(table, position)
@left_chopstick = table.left_chopstick_at(position)
@right_chopstick = table.right_chopstick_at(position)
loop do
think
eat
end
end
def think
puts "#{@name} is thinking"
end
def eat
take_chopsticks
puts "#{@name} is eating."
drop_chopsticks
end
def take_chopsticks
@left_chopstick.take
@right_chopstick.take
end
def drop_chopsticks
@left_chopstick.drop
@right_chopstick.drop
end
end
names = %w{Heraclitus Aristotle Epictetus Schopenhauer Popper}
philosophers = names.map { |name| Philosopher.new(name) }
table = Table.new(philosophers.size)
threads = philosophers.map.with_index do |philosopher, i|
Thread.new { philosopher.dine(table, i) }
end
threads.each(&:join)
sleep