% Test asynchronous processing in Java threads with returns.
% Must be run with options -t -1 to keep the message loop alive indefinitely.
% Corrected in Prova version 1.5.

:- eval(test_spawn()).

test_spawn() :-
	println(["==========test spawn=========="]),

	P=ws.prova.examples.Portfolio("IBM"),
	% P.longcalc(2) is an instance method.
	% It takes 2 seconds to complete and returns a Boolean.
	spawn(XID,P,longcalc,2),
	% Ret will contain a returned object in a list,
	%    a Boolean.TRUE in this case.
	% If a called method was void or returned null,
	%    Ret would have contained a string 'null'. 
	rcvMsg(XID,self,Me,return,Ret),
	println(Ret).
test_spawn() :-
	sendMsg(XID,self,0,test,t()),
	% java.lang.Thread.sleep(2000) is a static method running for 2 seconds
	spawn(XID,java.lang.Thread,sleep,2000),
	% If the method returns void or returns null, return is instantiated with
	%    a string constant 'null'
	rcvMsg(XID,self,Me,return,[Ret]),
	println([Ret]),
	println(["========test complete========="]).
