how to pass parameters to java threads

L

laclac01

am trying to learn java, and i have a question. How do i pass
parameters to threads??? Here is an example;


import java.io.*;
import java.net.*;

public class MultiEchoServer {

public static int MYECHOPORT = 8189;

public static void main(String argv[]) {
ServerSocket s = null;
int myNumber =8;
try {
s = new ServerSocket(MYECHOPORT);
} catch(IOException e) {
System.out.println(e);
System.exit(1);
}

while (true) {
Socket incoming = null;
try {
incoming = s.accept();
} catch(IOException e) {
System.out.println(e);
continue;
}

new SocketHandler(incoming).start();

}
}
}

class SocketHandler extends Thread {

Socket incoming;

SocketHandler(Socket incoming) {
this.incoming = incoming;
}

public void run() {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
PrintStream out =
new PrintStream(incoming.getOutputStream());
out.println("Hello. Enter BYE to exit");

boolean done = false;
while ( ! done) {
String str = reader.readLine();
if (str == null)
done = true;
else {
out.println("Echo: " + str);
if (str.trim().equals("BYE"))
done = true;
}

}
incoming.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}



How would i pass "myNumber" to the thread. I know from looking up on
the internet that run() does'nt take parameters. It would be great if i
could just do run(int myNumber); But alas i can't. mynumber is a number
that will be changed or accessed by each thread
This is just an example, as I want to apply to my own code.
 
T

Trung Chinh Nguyen

You can write a new constructor for the thread that takes the number as
a parameter or write a method like setNumber(int number), obviously this
method should be called before calling start()
 
G

Gordon Beaton

am trying to learn java, and i have a question. How do i pass
parameters to threads??? Here is an example;

Prefer to "implement Runnable" rather than "extend Thread", since in
almost all cases, your class isn't itself a Thread, i.e. it doesn't
provide Thread-like capabilities to others, it just uses a Thread
itself.

Your class is not special, it can have fields and methods just like
any other. For example you can pass parameters through constructor
arguments, or by calling setters. Values stored in the object are
available to all methods, including run():

public class MyClass implements Runnable {
int number;

public MyClass(int n) {
number = n;
}

public int getNumber() {
return number;
}

public void setNumber(int n) {
number = n;
}

public void run() {
while (number > 0) {
System.out.println(number);
number--;
}
}
}

/gordon
 
G

Gordon Beaton

Intercommunication between threads is possible through pipes.

If you need to "stream" objects between threads, an object queue is
simpler than a pipe, less error prone, and doesn't require any
serialization or parsing.

For single updates, calling setters in the target object is even
easier.

/gordon
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top