• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing object references between threads  RSS feed

 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to pass a reference recordItem to My thread from the Main thread

The code:

class MainThread{
public static void main (String [] args)
{
RecordItem recordItem = new RecordItem();
MyThread mt = new MyThread ();
mt.start ();

}
}

class MyThread extends Thread
{
public void run ()
{
//I want to use the reference of RecordItem here
}
}

Please suggest how do I do it

Regards
Jacob
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are many option. it's kind of hard to tell which one is the suitable solution.

- if it's all about the "new RecordItem()", refactor that code (plus the creation logic) into a RecordItemFactory.

- if you just need a reference to something (say a configuration or so), make it accessible via a helper-class with static methods.
MyClass.setConfiguration(RecordItem r)...
RecordItem MyClass.getConfiguration()...

- if your requirement is more analogues to an HttpSession, check "ThreadLocal"...

hope it helped,
Jan
 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please elaborate or put it in my code if it's only few lines of code?.

Regards
Jacob
 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requiremnt is all about the "new RecordItem()", refactor that code (plus the creation logic) into a RecordItemFactory.

Regards
Jacob
 
Saso Jordanoski
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just add a private class variable and a parameter to the constructor in the MyThread class:
private RecordItem recordItem;
public MyThread(RecordItem recordItem) {
super();
this.recordItem = recordItem;
}


Then in the main function call it like this:
MyThread mt = new MyThread(recordItem);
[ May 02, 2006: Message edited by: Saso Jordanoski ]
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a factory is a class which encapsulates creation of objects. this usually results in clearer oo-design than spreading creation-logic all over your aplication. it keeps responsibilities where they belong, and thereby reduces dependencies between classes.

enough theory, it's extremly simple (in it's easiest form)



the creation method doesnt need to be static, you can also instantiate a Factory object from within your code. Dependencies might be injected from outside ( setPropertyXxx() ) as well.

google yourself for some more advanced examples if you are interested, but i'd definitely favor this design...

many greetings,
jan
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a word about your example plus the solution offered by the other poster.

if you choose to extend the Thread class, you cannot extend any other classes, which might be a handicap in your business-logic. it's usually way better to let your class implement the Runnable interface.

anyhow, you don't want business-objects in the constructor, better let your thread control other objects dealing with business logic. say you want a thread to prepare a new record item, let it call ShopBasket.makeNewItem(), and don't burden the control thread with a REcordItem property...

jan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!