i have a rather ugly workaround by reading/writing the seed from a
local variable:
private static class MyRandom
extends java.util.Random
{
private long seed;
private final static long multiplier = 0x5DEECE66DL;
private final static long addend = 0xBL;
private final static long mask = (1L << 48) - 1;
synchronized public void setSeed( long seed ) {
seed = (seed ^ multiplier) & mask;
}
protected int next( int bits ) {
seed = (seed * multiplier + addend) & mask;
return (int) (seed >>> (48 - bits));
}
};
private void test()
{
final Runnable r = new Runnable() {
public void run()
{
final MyRandom rnd = new MyRandom();
int value = 500;
int d;
long seedCopy = rnd.seed;
while( true ) {
rnd.seed = seedCopy;
d = rnd.nextInt( 51 ) - 25;
seedCopy = rnd.seed;
System.out.println( "val = " + value + "; d
= " + d );
value += d;
if( value < 0 ) {
value += 1001;
} else if( value > 1000 ) {
value -= 1001;
}
Continuation.suspend();
}
}
};
final Continuation c = Continuation.startSuspendedWith( r );
Continuation d;
d = c;
System.out.println( "First run" );
for( int i = 0; i < 5; i++ ) d = Continuation.continueWith( d );
d = c;
System.out.println( "Second run" );
for( int i = 0; i < 5; i++ ) d = Continuation.continueWith( d );
}
... but i would a prefer a solution that is "opaque" to the person
that writes the Runnable. Also i will have additional problems with
other objects.
In fact java.util.Random implements Serializable, so it can indeed be
re-stored with its old seed...
ciao, -sciss-
Am 04.12.2007 um 18:53 schrieb Sciss:
hello torsten,
i have another question: how can i include objects other than
primitives into the continuation storage? in this example:
final Runnable r = new Runnable() {
public void run()
{
final java.util.Random rnd = new
java.util.Random();
int value = 500;
int d;
while( true ) {
d = rnd.nextInt( 51 ) - 25;
System.out.println( "val = " + value + "; d
= " + d );
value += d;
if( value < 0 ) {
value += 1001;
} else if( value > 1000 ) {
value -= 1001;
}
Continuation.suspend();
}
}
};
final Continuation c = Continuation.startSuspendedWith( r );
Continuation d;
System.out.println( "First run" );
d = c;
for( int i = 0; i < 5; i++ ) d = Continuation.continueWith( d );
System.out.println( "Second run" );
d = c;
for( int i = 0; i < 5; i++ ) d = Continuation.continueWith( d );
... the output is something like:
First run
val = 500; d = 11
val = 511; d = 5
val = 516; d = -19
val = 497; d = 11
val = 508; d = 21
Second run
val = 500; d = -25
val = 475; d = -6
val = 469; d = -15
val = 454; d = -11
val = 443; d = 9
so obviously 'value' got restored, but the random generator doesn't
restore its seed. so can i make javaflow store the internal state
of 'rnd', too?
thanks, -sciss-
Am 04.12.2007 um 16:04 schrieb Torsten Curdt:
Great! ...but still added the example to my TODO.
Javaflow needs some polishing anyway.
cheers
--
Torsten
On 04.12.2007, at 14:51, Sciss wrote:
thank you torsten,
putting my runnable in a dedicated package and using
addLoaderPackageRoot("your.package.the.runnable.is.in") did it!
now going back to experimentation ;-9
ciao, -sciss-
Am 03.12.2007 um 12:23 schrieb Torsten Curdt:
Seems like I badly need to look into providing the tutorial
example to just run out of the box.
On the first glance your try on the class loading seems a bit
unorthodox but should still work. (I never used the classloader
but always used javaflow through jci) ...but looking at the code
I'd assume you also need to tell the classloader what classes to
instrument. You might want to try
addLoaderPackageRoot("your.package.the.runnable.is.in");
or set
setParentFirst(false);
Let me know if that helped.
cheers
--
Torsten
-------------------------------------------------------------------
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]