Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can I pass a parameter to run method in java

 
Sourabh Pujari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sourabh Pujari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve!

it worked!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic