Solved

sub function in thread

Posted on 2004-08-06
25
524 Views
Last Modified: 2008-03-03
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
Comment
Question by:hawkly
  • 11
  • 6
  • 4
  • +3
25 Comments
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Just try:

receiveIt.join () ;
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 20 total points
Comment Utility
from api doc

join();
join(long millis, int nanos) ;
join(long millis)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 

Author Comment

by:hawkly
Comment Utility
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
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
>>Is that from the J2SE API or the J2ME API?

JavaTM 2 Platform Std. Ed. v1.4.2
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> JavaTM 2 Platform Std. Ed. v1.4.2

That is J2SE, not J2ME.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
In the run () method for the receiveIt thread, you can make it sleep:

Thread.sleep ( 5000 ) ; // sleep for 5 seconds
0
 

Author Comment

by:hawkly
Comment Utility
Anyone can tell me how call a function in thread from main thread?
0
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
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
 

Author Comment

by:hawkly
Comment Utility
>> 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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
 

Author Comment

by:hawkly
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
>>- 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
 

Author Comment

by:hawkly
Comment Utility
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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
 

Author Comment

by:hawkly
Comment Utility
>>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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 30 total points
Comment Utility
Hi all,

public void stopThread ()
{
  running = false ; // some boolean flag which specifies if the thread is running or not
  connection.close () ;
  interrupt(); // <---
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
methods are related to objects, not threads.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
:-) perhaps he meant the thread class which he has (Receive)
0
 

Assisted Solution

by:krispin
krispin earned 30 total points
Comment Utility
>>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
 
LVL 30

Accepted Solution

by:
mayankeagle earned 70 total points
Comment Utility
Well, I didn't pay attention to that. In that case, this would work:

( ( Receive ) receiveIt ).stopThread () ;
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
:-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java continue statement 10 70
Fibonacci challenge 11 81
count7 challenge 12 66
stringclean challenge 26 54
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now