• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
majety srikanth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi majety,

Please UseCodeTags when posting code in forums. Unformatted code is difficult to read...
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is your getResult() method being called from the code you are calling?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
majety srikanth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
majety srikanth
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic