aspose file tools*
The moose likes Threads and Synchronization and the fly likes can I pass a parameter to run method in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "can I pass a parameter to run method in java" Watch "can I pass a parameter to run method in java" New topic
Author

can I pass a parameter to run method in java

Sourabh Pujari
Greenhorn

Joined: Nov 17, 2010
Posts: 4
I have 1 object of the calling class which needs to be validated in the called class.

this validation needs to be done in one of the threads of the called class so I need to pass a parameter (instance of calling class) to the run method of called class.

How can I do this....or please can you suggest me some other way through which I can do this?

Thanks in advance for you help.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

No you can't pass parameters to the run() method. The signature tells you that (it has no parameters). Probably the easiest way to do this would be to use a purpose-built object that takes a parameter in the constructor and stores it in a final variable:


Once you do that - you have to be careful of the data integrity of the object you pass into the 'WorkingTask'. The data will now exist in two different threads so you have to make sure it is Thread Safe.


Steve
Sourabh Pujari
Greenhorn

Joined: Nov 17, 2010
Posts: 4
Thanks Steve for your reply!

I am handling a huge data in my calling class instance.....

If I create an instance of "toWorkWith" class (calling class) in "WorkingTask" called class and initialize it, this will bring whole data in the calling class instance to the called class and this will result in the redundant data in both the classes....this will even eat up a huge memory...

can you please share your thoughts on this.... or this is the only way I can do it?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

You do not create a new instance of the Object you want to pass in. You pass in the reference and store it in a variable. You do not create redundant data - you create a new reference to the same data. The memory usage is minimal.
Sourabh Pujari
Greenhorn

Joined: Nov 17, 2010
Posts: 4
Thanks Steve!

it worked!
 
Don't get me started about those stupid light bulbs.
 
subject: can I pass a parameter to run method in java