explain how to pass parameter to thread??

Never done multithreading in java, but I am going to assume it is like anything else. When you create a new thread, you pass in the parameter in the function call. So if you want to pass an integer, you would pass in the variable, typically in the void* parameter, of the create thread method.

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.

See http://stackoverflow.com/questions/877096/java-threading-pass-parameter-to-a-thread

It pretty much boils down to these choices:
1. Pass them to the constructor of your Thread/Runner class.
2. If you're using anonymous inner classes, make variables in the enclosing scope final , which makes them usable from within the anonymous inner class.
3. Non-static nested classes have access to fields in their parent classes. (I discourage the use of this option, as sloppy and more difficult to maintain.)

The "final" trick is the most commonly used. (#2 above)

JeffGrigg , You are correct.. The way i have answered was to make beginner to understand in a better way.

@#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: Consider this code, and see if you can find the parameter being passed to the Thread:

public class Constructor_benifit {
	public static void main(String[] args) {
		System.out.println("In primary Thread");
		new Thread(new A("Test string for secondary thread")).start();
		new Thread(new A("Here's another message.")).start();
		new Thread(new A("And this is yet another thread.")).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);
	}
}

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.

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 !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.