• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

sub function in thread

I have a main program with :

Thread receiveIt = new Receive(dc, myDisplay);
receiveIt .start();
int delayMillis = 5000; // 5 seconds
try
{
      receiveIt .wait(delayMillis);
      if (receiveIt .isAlive())
      {
            //receiveIt .stopIt();
      }
}
catch (InterruptedException e)
{
      // Thread was interrupted
}

How can i call the function in the receiveIt thread? and when i write receiveIt .join(delayMillis) it throw me an exception:
join() in java.lang.Thread cannot be applied to (int)

I'm using j2me

thanks
0
hawkly
Asked:
hawkly
  • 11
  • 6
  • 4
  • +3
4 Solutions
 
Mayank SAssociate Director - Product EngineeringCommented:
Just try:

receiveIt.join () ;
0
 
expertmbCommented:
from api doc

join();
join(long millis, int nanos) ;
join(long millis)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It'll always be possible to pass an 'int' into join ( long millis ), so I don't think that should be the problem. I guess that in J2ME, the join ( long ) method is not there, but only the parameter-less join () is there.

>> from api doc

Is that from the J2SE API or the J2ME API?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hawklyAuthor Commented:
how about call a function in thread from main thread?
receiveIt .stopIt();

receiveIt.join () ; will Wait indefinitely for the thread to finish.
But i want to wait the thread for certain seconds only.

join(long millis) will throw an exception join() in java.lang.Thread cannot be applied to (int) in j2me
0
 
expertmbCommented:
>>Is that from the J2SE API or the J2ME API?

JavaTM 2 Platform Std. Ed. v1.4.2
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> JavaTM 2 Platform Std. Ed. v1.4.2

That is J2SE, not J2ME.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
In the run () method for the receiveIt thread, you can make it sleep:

Thread.sleep ( 5000 ) ; // sleep for 5 seconds
0
 
hawklyAuthor Commented:
Anyone can tell me how call a function in thread from main thread?
0
 
expertmbCommented:
i know that !!!

you asked
>>Is that from the J2SE API or the J2ME API?

it is from  "JavaTM 2 Platform Std. Ed. v1.4.2"

mb...
0
 
hawklyAuthor Commented:
>> In the run () method for the receiveIt thread, you can make it sleep:
>> Thread.sleep ( 5000 ) ; // sleep for 5 seconds

I want to let the main thread to wait 5 second for the receiveIt thread. If after 5 seconds the receiveIt thread still alive then i will close the connection.

thanks
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> i know that !!!

Then there's no use posting that on this page because the J2SE and J2ME API are different ;-)

>> I want to let the main thread to wait 5 second for the receiveIt thread

In the main thread, write:

Thread.sleep ( 5000 ) ;

Then check: if ( receiveIt.isAlive () )
                     // do your stuff ;
0
 
hawklyAuthor Commented:
Sorry for posting j2me at here, but i dun know where to put this question.

Please help in this question:
how to call a function in thread from main thread?
E.g. receiveIt .stopIt();
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can post J2ME in here. When I said:

>> Then there's no use posting that on this page because the J2SE and J2ME API are different ;-)

- it was for expertmb and not for you. I told him not to post something from the J2SE API because your question mentioned that its for J2ME.

Did you check the approach that I mentioned above?
0
 
expertmbCommented:
>>- it was for expertmb and not for you. I told him not to post something from the J2SE API because your
question mentioned that its for J2ME.

mayankeagle

just read the comments properly,
wht i said was i know tht  "JavaTM 2 Platform Std. Ed. v1.4.2" is J2SE and not J2ME need not to tell me.


meanwhile can you post wht are those apis related to join().

mb...

0
 
hawklyAuthor Commented:
Actuall the receiveIt thread is block by an IO so that i want to call the connection.close() in the thread but my problem is i was unable to call a function in receiveIt thread from the main thread.

main thread:
public class DatagramClient extends MIDlet implements CommandListener

receiveIt thread:
public class Receive extends Thread


thanks
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> wht i said was i know tht  "JavaTM 2 Platform Std. Ed. v1.4.2" is J2SE and not J2ME need not to tell me.

I know that you know that :-) what I meant was that its still no use posting it here because it is not needed on this page. I didn't mean anything else if you concluded something different.

>> Actuall the receiveIt thread is block by an IO so that i want to call the connection.close() in the thread

You want to call connection.close () in the receiveIt thread? And you want to do that by waiting in the main thread for 5 seconds and then call it?
0
 
hawklyAuthor Commented:
>>You want to call connection.close () in the receiveIt thread? And you want to do that by waiting in the main thread for 5 seconds and then call it?

yes, correct...... the main thread will wait for 5 seconds and if the receiveIt thread.isAlive() == true then i will close the receiveIt thread's connection.

thanks
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Same way:

Thread.sleep ( 5000 ) ; // this is in main ()

if ( receiveIt.isAlive () )
  receiveIt.stopThread () ;

In the Receive class, add a method called as stopThread ():

public void stopThread ()
{
  running = false ; // some boolean flag which specifies if the thread is running or not
  connection.close () ;

}

In the run () method, keep checking the value of the running flag in a loop (initially, it will be set to true) - when it is false, then break out of the loop and exit the run () method.
0
 
WebstormCommented:
Hi all,

public void stopThread ()
{
  running = false ; // some boolean flag which specifies if the thread is running or not
  connection.close () ;
  interrupt(); // <---
}
0
 
objectsCommented:
methods are related to objects, not threads.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-) perhaps he meant the thread class which he has (Receive)
0
 
krispinCommented:
>>receiveIt.stopThread () ;
I don't think this will work.

If you look at his program, he declared the receive it object like this:
Thread receiveIt = new Receive(dc, myDisplay);

receiveIt is a Thread object, and while it is referencing a Receive object, it can only access Thread methods. Since stopThread() is a method in Receive and not in Thread, you won't be able to call the method. He will have to declare receiveIt it as:
Receive receiveIt = new Receive(dc, myDisplay);
or cast it from Thread to Receive before calling the stopThread() method.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, I didn't pay attention to that. In that case, this would work:

( ( Receive ) receiveIt ).stopThread () ;
0
 
WebstormCommented:
:-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 11
  • 6
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now