Forums Register Login

multithread problem - start is not calling run() - child thread is not completing before parent

+Pie Number of slices to send: Send
class ChildThread extends Thread
{
public boolean processing = false;
public void run()
{
getResult();
}
public void getResult()
{
connect with webservice and get some data from it. once it is successfully completed,Iam changing the flag value

processing = false;
}
public void post(string params)
{
after getting parameters from calling method, i am starting this thread
and setting flag value

processing = true;
this. start(); // starting the thread to call run method
}
}
########################

class parent
{
method()
{
new ChildThread().post(String params);
while(processing)
{
print("processing is going on...");
}
doSomethingElse();
}
}
doSomthingElse() gets executed before the childThread run() method is run at all. orelse some times, the childThread start() is not at all calling run() method sothat the parent is going to infinte loop.
I set high priority to childThread . but no use.


As i am using J2ME CLDC 1.1, there is no join() concept or java.util.concurrent library support. If any one knows the solution to how to handle this, kindly post here.
+Pie Number of slices to send: Send
I would recommend you to use "wait" and "notify" instead of going in an infinite loop.

Lets the parent thread wait on an object and let the child thread notify parent thread once it finish the processing. This would give very good performance CPU wise and will reduce your execution time as well.
+Pie Number of slices to send: Send
Thanks Sunny Jain. I have tried your suggestion.

in the Parent class
{
ChildThread childThread = new ChildThread();
childThread.post(params);

// replaced While loop with the following code

synchronized(childThread)
{
try {
System.out.println("waiting for the thread process.......");
childThread.wait();
System.out.println("waiting is over .......");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


#####################
in ChildThread
#####################
public void getResult()
{
synchronized (this)
{
connect with webservice and get some data from it. once it is successfully completed,Iam changing the flag value

processing = false;
this.notifyAll();
}
}


But in output .... parent is stopping in waiting for ever. Am I using synchronization - wait and notify correctly ?
Please let me know.

Thanks for your help.
+Pie Number of slices to send: Send
Hi majety,

Please UseCodeTags when posting code in forums. Unformatted code is difficult to read...
+Pie Number of slices to send: Send
How is your getResult() method being called from the code you are calling?
+Pie Number of slices to send: Send
I'm afraid the truth is that no one can really tell if you're doing anything correctly, or what problems you might be having, because you're not posting real code, only summaries. The problem with that is that the summaries reflect what you think the code looks like, whereas often if there's a problem it's because the actual code has some major problem that isn't reproduced in your summary. There's a page on our Wiki called PostRealCode (that's a link) which explains why this is a good idea.
+Pie Number of slices to send: Send
Hi,
Thanks for your reply.

I have already posted my real code here.
https://coderanch.com/t/526710/JME/Mobile/Multithreading-me-webservice-connection

But, I did not get any reply for that. I thought, it is time taking to understand the whole code and posted it in snippets form.

for Vijitha Kumara' query : childThread's run method is calling GetResult() method. If you see the above link or my first post, you can understand this.

I am a new person to the forums and all. So, still if I miss any information please let me know with out getting angry.
+Pie Number of slices to send: Send
Sorry.. here is my code in properly arranged code format.
What I want is, my Parent (Sync) has to wait until the child(TestConnectionThread) completes.
i.e in the output - the red colour lines should be interchanged so that Sync - checkTestConnection() - isConnection value willbecome true;




output :
Sync - checkTestConnection()
current thread name & priority:Thread-4-5
TestConnection Thread - posted parameters - url <url - value>
In URL <properly working url - value>
Connecting To Server...
TestConnectionThread - run()
TestConnectionThread - run() - thread name and priority :Thread-7-10
TestConnectionThread - run()-inside thread running
TestConnection Thread - GetResult()
soapAction <soap-action value >
In URL <properly working url - value>
TestConnection Thread - GetResult() - before ht.call
Connecting To Server....
Connecting To Server.....
Connecting To Server......
Connecting To Server.......
Sync-checkTestConnection()- inside time out
<<<<<<<<<<< thread killed <<<<<<<<<<<<<<<<
Sync-checkTestConnection()-while is over
Sync-checkTestConnection()- inside else block>>>>>>>>>>>>>>>>>>>>>>
Sync - checkTestConnection() - finally value is set to : false
>>>>>>>>>>>>>>>>>>>>>>>>> Sync - checkTestConnection() - isConnection value =falseTestConnection Thread - GetResult() - after ht.call
>>>>>>>>>>>>>>>>>>>>>>>>> TestConnection Thread - GetResult()- got response from webservice:trueTestConnection Thread - GetResult() - set constants url as <properly working url - value>
TestConnection Thread - GetResult() - sending value :false
Execution completed.
I found a beautiful pie. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2594 times.
Similar Threads
Whether using Threads join correctly or not
Two threads of same class instance to wait separately
Evidence 4 Main Thread dead after main method exits
[ A good finding ] - User & Deamon Threads
wait and notify
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:24:58.