how to pass parameter to thread??
explain how to pass parameter to thread??
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
If you are using another class
public class Constructor_benifit{
public static void main(String[] args){
System.out.println("In primary Thread");
A a=new A("Test string for secondary thread");
Thread thread_object=new Thread(a);
thread_object.start();
}
}
class A implements Runnable
{
String to_print;
A(String to_print){
this.to_print=to_print;
}
public void run(){
System.out.println(to_print);
}
}
In same class means anyway no problem.. You can make use of common variable or same style you can follow it.
Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6
JeffGrigg , You are correct.. The way i have answered was to make beginner to understand in a better way.
Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6
@#Muralidharan.E
I can't tell from the comments in your code what parameter is being passed to the Thread. Could you edit your example and explain that?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
see if you can find
That is exactly the point. Students need a bit more help understanding how code works.
They should not have to "find" how posted code works. That should be explained in the code's comments.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
That is exactly the point. Students need a bit more help understanding how code works.
They should not have to "find" how posted code works. That should be explained in the code's comments.
Since balajisathya asked How to pass parameter to thread, I have given that code without any comments. And there is no complex flow which will confuse beginner. That is very short which can be understand by a student.
Note- He didnt ask what is parameter and how to pass parameter, He asked How to pass
parameter to thread. So he knows what is parameter.
By considering these things only i have given like that.
Anyway it is always good to have proper comments even for small bit of
programs. Here after i will follow that !
Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6