R
Richard A. DeVenezia
I am trying to implement a Runnable that will do various actions by way of
methods.
The design is a little bass-ackwards because the eventual user will
'bridging' into the JVM (from app ABC) through a sub-optimal jni adapter
that can receive new references, but not receive object references in return
and loses the new references between the 'tasks' of ABC.
Q: how bad is the 'Service' design ?
Q: is there something better than could be done in the run() ? (Such as
yield or wait)
Is
while(!end) {} just as bad as
while(!end) { Thread.yield(); }
Thanks.
//-------------
public class Service implements Runnable {
public void run () {
private boolean end = false;
private int status = 0;
public void end () { this.end=true; }
System.out.println ("Running");
try {
while (!end) { Thread.sleep(50) }
}
catch (InterruptedException e) {}
System.out.println ("Done");
}
public int getStatus () {
return this.status;
}
public void connect () {
status = 100;
return 1;
}
public int obtainX () {
status = 200;
return 2;
}
public void setA (int X, String s)
{
// some feature S of an object
// functionally derived from X
// is set to s
status = 300;
}
}
//-------------
public class ServiceAdapter {
private static Service service = null;
private static Thread thread = null;
ServiceAdapter () {
if (service != null) return;
service = new Service();
thread = new Thread (service);
thread.start();
}
void connect () {
service.connect();
}
int obtainX () {
return service.obtainX ();
}
void setA (int X, String s) {
service.setA (X,s);
}
void end () {
service.end();
}
}
//-------------
// Some pseudo-jni adapted application does the equivalent of
//-------------
ServiceAdapter jsa = new ServiceAdapter();
jsa.connect();
jsa = null;
jsa = new ServiceAdapter();
int x = jsa.obtainX();
jsa.setA (x, "foobar");
jsa = null;
jsa = new ServiceAdapter();
jsa.end();
methods.
The design is a little bass-ackwards because the eventual user will
'bridging' into the JVM (from app ABC) through a sub-optimal jni adapter
that can receive new references, but not receive object references in return
and loses the new references between the 'tasks' of ABC.
Q: how bad is the 'Service' design ?
Q: is there something better than could be done in the run() ? (Such as
yield or wait)
Is
while(!end) {} just as bad as
while(!end) { Thread.yield(); }
Thanks.
//-------------
public class Service implements Runnable {
public void run () {
private boolean end = false;
private int status = 0;
public void end () { this.end=true; }
System.out.println ("Running");
try {
while (!end) { Thread.sleep(50) }
}
catch (InterruptedException e) {}
System.out.println ("Done");
}
public int getStatus () {
return this.status;
}
public void connect () {
status = 100;
return 1;
}
public int obtainX () {
status = 200;
return 2;
}
public void setA (int X, String s)
{
// some feature S of an object
// functionally derived from X
// is set to s
status = 300;
}
}
//-------------
public class ServiceAdapter {
private static Service service = null;
private static Thread thread = null;
ServiceAdapter () {
if (service != null) return;
service = new Service();
thread = new Thread (service);
thread.start();
}
void connect () {
service.connect();
}
int obtainX () {
return service.obtainX ();
}
void setA (int X, String s) {
service.setA (X,s);
}
void end () {
service.end();
}
}
//-------------
// Some pseudo-jni adapted application does the equivalent of
//-------------
ServiceAdapter jsa = new ServiceAdapter();
jsa.connect();
jsa = null;
jsa = new ServiceAdapter();
int x = jsa.obtainX();
jsa.setA (x, "foobar");
jsa = null;
jsa = new ServiceAdapter();
jsa.end();