我正在尝试同时使用代理回调。不幸的是,无论我做什么,它似乎总是顺序运行而不是并行运行。(没有代理它没有)
主类(应用):
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
a1 : separate PROCEDURE
a2 : separate PROCEDURE
do
create my_counter.make (1, 100_000_000)
create my_counter2.make (2, 100_000_000)
launch_counter(my_counter)
launch_counter(my_counter2)
end
feature -- Launch
launch_counter(c: separate COUNTER)
do
c.run (5, agent print_hello)
end
print_hello
do
print("Hello!%N")
end
feature -- Access
my_counter : separate COUNTER
my_counter2 : separate COUNTER
end
计数器类:
class
COUNTER
inherit
EXECUTION_ENVIRONMENT
create
make
feature -- Initilzation
make (i: INTEGER; delay: INTEGER)
do
id := i
delay_time := delay
end
feature -- Stuff
run (times: INTEGER; p: separate PROCEDURE)
local
c : INTEGER
do
from
c := times
until
c = 0
loop
print("COUNTER: " + id.out)
p.call
sleep(delay_time)
c := c - 1
end
end
feature {NONE} -- Access
delay_time : INTEGER
id: INTEGER
end
预期输出:
COUNTER: 1Hello!
COUNTER: 2Hello!
COUNTER: 1Hello!
etc.
实际输出:
COUNTER: 1Hello!
COUNTER: 1Hello!
COUNTER: 1Hello!
COUNTER: 1Hello!
COUNTER: 1Hello!
COUNTER: 2Hello!
COUNTER: 2Hello!
COUNTER: 2Hello!
COUNTER: 2Hello!
COUNTER: 2Hello!
我必须改变什么才能使其按预期运行?