Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

how to  pass parameters to java threads

Thread view: 
laclac01@gmail.com - 27 Apr 2006 11:34 GMT
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.
Trung Chinh Nguyen - 27 Apr 2006 11:52 GMT
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()

>  am trying to learn java, and i have a question. How do i pass
> parameters to threads??? Here is an example;
[quoted text clipped - 72 lines]
> that will be changed or accessed by each thread
> This is just an example, as I want to apply to my own code.
Gordon Beaton - 27 Apr 2006 11:59 GMT
>  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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

primeattheark - 27 Apr 2006 12:17 GMT
Intercommunication between threads is possible through pipes.

read the section on pipes in Bruce Eckel's excellent Thinking In Java you can
download it from

http://mindview.net/Books/DownloadSites/

I hope this helps.

Rob

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

>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.
Gordon Beaton - 27 Apr 2006 12:36 GMT
> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.