aspose file tools*
The moose likes Threads and Synchronization and the fly likes Regarding thread excution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Regarding thread excution" Watch "Regarding thread excution" New topic
Author

Regarding thread excution

Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
hi,

............code.................

function()
{
//some instruction are sent to some server
//There is thread which is continuosly running at background
//to read the response from the server .
//then in thread some variable are reset.
}
reset_var;

-----------------------
here i want to ask ,that the task which is being performed in thread
i want to get into reset_var, which being set in thread.

So here the query is whether instrunction reset_var,which immediatly
follows the function block, would get the updated value or not.

if not then please suggest any way how to cope with such prob.


regards sachindra
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Is this java that you are talking about?
Can you post real code

What is the meaning of an instruction after a function block?

Are you talking about a member variable being initialized after a method definition?
If yes, then in java all member variable initialization happens first and then method execution starts.


apigee, a better way to API!
Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
Thanks for your reply,

Actually function consist of java instruction
for call establishment with sip server.

Well there is a thread ,which is running all the
time when application is up.
Note :And this thread is not running inside the
function block.
The purpose of thread is to read the response,
of instructio being sent in the mentioned function.

So when appropriate reply comes from server, on response to
request which is being sent in the mentioned function,
there is variable which is being set.

And that particular variable is checked just after that function.
so what would be the state of that variable.


regards sachindra
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

This is all so esoteric, please give some solid example with some real code. This should be a SSCCE program, just enough to give us an idea of the problem you are having, but it needs to be complete enough that we can copy it and run it ourselves.

If you wanted a short answer to:
"whether instrunction reset_var,which immediatly
follows the function block, would get the updated value or not"

The answer is: Yes, if you set it up to be able to be gotten, or No, if not.
[ August 21, 2008: Message edited by: Steve Luke ]

Steve
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

If you are talking about the visibility gurarantees of a variable across two threads, then you can make the variable volatile.

For more on visibility guarantees, refer to the Java Memory Model.

If it is in the same thread then the latest value will always be available.
Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
Thankyou ,Steve and Nishant.
I have found out some solution to the above mentioned proble by making use
of join().
But now i am facing a new logical problem.

-----------code------------------------

t1.start();
t2.start();
t3.start();

////////

thread t1{
public void run(){
//fires some command to
........//instruction send to server
t1.stop();
}
}

thread t2{
public void run(){
while(true){
t1.join();
if(callFlag){//callFlag value set to true only
//when requiste response is read from server through our client prog.
//which is reply of the instrunction send in thread t1;
......
callFlag=false;
t2.stop();
}
}}
}

thread t3{
public void run(){
t2.join();
.....
......
}
}

//Well problem i am facing is suppose the desired response never comes
the callFlag is never set to true;
so the thread t2 never stops .

So please help me out.


regards
Sachindra
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

As already mentioned, please post real code. Posting pseudo-code, and then expecting specific answers about the real (not posted) code is just not going to work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Yes, to get a specific answer we will need to see real code.

And pleas use the code tags to display the code (there is a code button at the bottom of the editor).

Generally, though, this is a design question you have to answer. What do you think should happen if the response isn't made? How do you know that the correct response isn't made? When do you know the correct response isn't made?

Is the first line of code in each run method actually a join() with the previous thread? If so, then what benefit do you have of making them separate threads? Why not make them all execute in the same thread?

It also sounds like you may be implementing some sort of event call-back mechanism. For example, Thread 1 is chugging along doing some work. When a certain event occurs (correct response is sent) then Thread 2 should do its thing, and when Thread 2 is done then Thread 3 should do its thing. Meanwhile Thread 1 is still chugging along doing more work and may still need to Signal Thread 2 to do its job again.

Does this sound correct?
Vaibhav Jaiswal
Greenhorn

Joined: Jan 17, 2008
Posts: 12
if call flag is never set to true than probably you should look at the client code, what is happening there how can anybody tell this from the code that is checking it rather than the code that is setting it.
Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
Thankyou All of you, for the valuable suggestion.

Steve i solved the problem.


regards,
Sachindra
 
wood burning stoves
 
subject: Regarding thread excution