Why does implementing Runnable not allow the access of global vars

B

Blaine

Hello,

I have a class that implements Runnable and it's constructor sets of
some vars to be global. However when I run the code (using the
..start()) the global vars are not availiable.



The following code creates the output:
Start

When you would expect it to create
Start RUNNING

How can I get this expected output?



Squirrel squirrel = new Squirrel(in,out );
Thread squirrelThread = new Thread ( squirrel );
squirrelThread.start();

public class Squirrel implements Runnable {
private InputStream inRS232;
private OutputStream outRS232;

/** Creates a new instance of Squirrel */
public Squirrel(InputStream inStream, OutputStream outStream) {
this.setInputStream(inStream);
this.setOutputStream(outStream);

this.writeRS232("\r\nStart");

}


public void run(){
this.setRunning(true)
this.writeRS232(" RUNNING ");
}

public void setInputStream(InputStream in){
this.inRS232 = in;
}

public void setOutputStream(OutputStream out){
this.outRS232 = out;
}


private void writeRS232(String request){
try {
this.outRS232.write( request.getBytes() );
} catch (IOException e) {
System.out.println( e.getMessage() );
}
}
}
 
B

blaine

No, the error I on the call during the run() function is a IOException.
Write Error.

I have added the flush call to make the code cleaner though.

Thanks.

Anyother ideas?
 
J

Joona I Palaste

Blaine <[email protected]> scribbled the following
I have a class that implements Runnable and it's constructor sets of
some vars to be global. However when I run the code (using the
.start()) the global vars are not availiable.


The following code creates the output:
Start
When you would expect it to create
Start RUNNING
How can I get this expected output?


Squirrel squirrel = new Squirrel(in,out );
Thread squirrelThread = new Thread ( squirrel );
squirrelThread.start();
public class Squirrel implements Runnable {
private InputStream inRS232;
private OutputStream outRS232;

/** Creates a new instance of Squirrel */
public Squirrel(InputStream inStream, OutputStream outStream) {
this.setInputStream(inStream);
this.setOutputStream(outStream);

this.writeRS232("\r\nStart");

}


public void run(){
this.setRunning(true)
this.writeRS232(" RUNNING ");
}
public void setInputStream(InputStream in){
this.inRS232 = in;
}
public void setOutputStream(OutputStream out){
this.outRS232 = out;
}

private void writeRS232(String request){
try {
this.outRS232.write( request.getBytes() );
} catch (IOException e) {
System.out.println( e.getMessage() );
}
}
}

After resolving some trivial errors in your code - you're missing a
semicolon, there is no method named setRunning() (I commented that out)
- and moving your start code to a main() method and changing in and out
to System.in and System.out, your code works all OK for me. It prints
"Start RUNNING" without a linefeed
 
P

Peter MacMillan

Blaine said:
Hello,

I have a class that implements Runnable and it's constructor sets of
some vars to be global. However when I run the code (using the
.start()) the global vars are not availiable.

Global vars? where? Those aren't global, they're instance variables. I
suppose if you mean global to the class... ugh.
public void run(){
this.setRunning(true)
this.writeRS232(" RUNNING ");
}

Either you posted an incomplete class or you have a big error there (are
you running this from within an IDE - some IDEs can compile a class and
let you run it even with code errors).

1) you're missing a ; at the end of the first line
2) there is no defined method with the signature void setRunning(boolean)

the following code:

//---
import java.io.*;

public class Squirrel implements Runnable {
private InputStream inRS232;
private OutputStream outRS232;

/** Creates a new instance of Squirrel */
public Squirrel(InputStream inStream, OutputStream outStream) {
this.setInputStream(inStream);
this.setOutputStream(outStream);

this.writeRS232("\r\nStart");
}

public void run() {
// there is no method called setRunning. Also, you forgot a ;
// this.setRunning(true);
this.writeRS232(" RUNNING ");
}

public void setInputStream(InputStream in) {
this.inRS232 = in;
}

public void setOutputStream(OutputStream out) {
this.outRS232 = out;
}

private void writeRS232(String request) {
try {
this.outRS232.write( request.getBytes() );
} catch (IOException e) {
System.out.println( e.getMessage() );
}
}

public static void main(String... args) {
Squirrel squirrel = new Squirrel(System.in, System.out);
Thread squirrelThread = new Thread(squirrel);
squirrelThread.start();
}
}
//---

produces the following output (JDK 1.5.0_02):

//---
C:\temp>java Squirrel

Start RUNNING
//---
 
E

Eric Sosman

No, the error I on the call during the run() function is a IOException.
Write Error.

I have added the flush call to make the code cleaner though.

Thanks.

Anyother ideas?

How about exhibiting the code? (No, not the snippet
you posted earlier, but a minimal, complete, compilable
Java class. At the moment, I'm inclined to believe that
the problem lies in the forty-second of the lines you
haven't revealed ;-)
 
B

blaine

Sorry, I should have stated that this was a code snipplet.. Kinda
thought it was obvious.

Anyhow. I used System.in and System.out as the input streams and it
worked. (Thanks Joona) Which lead me to other sections of my code to
find the the original input and output stream was closed outside of the
Squirrel thread. Thus causing my write error.

Thanks for the help.

Blaine
 
B

Bryce

Sorry, I should have stated that this was a code snipplet.. Kinda
thought it was obvious.

Note: When publishing code here, it is always best to post complete
compilable code. I usually will take the code I'm working on, and
attempt to write a small "HelloWorld" application that demonstrates
the problem I have... (Which usually leads me to figure out the
problem as well...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top