• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create new thread  RSS feed

 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Threads and have a basic doubt about thread creation.
I am creating a new thread by extending Thread
I override the run() method.

Should I write the run method in my Thread class?
Also I write t.start(); that is going to call run(), but where do i pass the parameters??

Pls help me out, i am confused.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to Threads and have a basic doubt about thread creation.
I am creating a new thread by extending Thread
I override the run() method.


The above is correct, although another alternative would be to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.

Should I write the run method in my Thread class?
No.

Also I write t.start(); that is going to call run(), but where do i pass the parameters??



Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

I believe you don't need any parameters.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I write the run method in my Thread class?


I hope I understood what you meant by the above statement.

Basically when the current thread executes the t.start() method, a new thread will be created, this new thread will automatically execute your overridden run() method.
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig.

I understood the start and run() methods.

I need the request and response objects for my method.
How can I call this method from the run?
I didnt undestand that part
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the request and response objects for my method.
How can I call this method from the run?
I am assuming you are talking about the HttpSerlvetRequest and HttpServletResponse objects from the javax.servlet.* packages, correct?

Is this a Servlet or JSP page?

If so, one way would be to store the object references in a instance(shared) variable, but that may not be such a good idea, because of the concurrent requests.

Can you elaborate more on what you are trying accomplish?
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On click of a button I am processing for payment.
Simultaneously i am sending a fax request also.
After payment processing a page is displayed. Actually I dont want to do this one after the other because of the time it takes. My fax request is independent of the next page, so i was thinking, I will call the payment processing and once that is over, I will create a separate thread for faxing, and continue with the fax request separately .
Hope I am clear with this.
Can you tell me if this approach is correct? or if there is any other better way to do this.
Thank you!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass any parameters you like to a constructor on your object, or call set methods before you start the thread. It's usually preferable to implement Runnable rather than extend Thread.

Do you see where you could pass the request & response to the runnable?

Now you can give the fax handler access to the request and response to get input. I'm a little worried about that tho, because a naughty developer in the future could try to write to the response. I'd prefer to extract exactly the data the fax handler needs into a custom object and pass that along instead of the request & response. Hope that made sense.
[ May 10, 2005: Message edited by: Stan James ]
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree.

I would recommend you store any pertinent information from the Request object in a Value Object or Transfer Object, and then pass that object to the class that will handle the fax processing, either thru the contructor or as a parameter in a method.
 
vivek ja
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I will look into that part of sending only the required values instead of the response. Thanks for pointing that out.
one more thing that I would like to ask is,

in Stan's example, I can access the args inside the run() only if i store them as instance variables, isnt it?
if i store the objects as an instance variable, it would not be thread safe right? whats the way out for that
Shall I make the method synchronized?
Sorry to be asking these basic questions, this is the first time I am programming with threads.
 
Daniel Spaethe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be fine with having the arguments set within the constructor or setting them with a "setter" method. Each thread that comes through this method will spin off it's own thread with it's own instance of the Runnable target. You can actually create multuple threads o rnu on the same runnable target, which in this case is "MyClass", but it lokos like you are just having one thread that you create do that, which is fine. I will use the same examples that have been used above. Lets say that the someMethod() is where you are having multiple threads commnig through your web-application. Inside this method, you are creating a new instance of "MyClass" out on the heap, which is an object local to that method. When the method "someMethod" finishes, your stack frame is gone, but the thread you created and passed your runnable class as the target will still be processing and running in the background until that thread finishes the run() method you must implement.

The preferred way is to implement the Runnable interface like you are someone else mentioned here. The only reason you really extend any class is to override behavior of that class and most people are just wanting it fo the pubic void run() method. If you look at the Thread class, it actually implements the Runnable interface.

Hope this helps,
Dan

Dan
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!