Solved

sub function in thread

Posted on 2004-08-06
25
527 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:Mayank S
ID: 11733886
Just try:

receiveIt.join () ;
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 20 total points
ID: 11733890
from api doc

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

Expert Comment

by:Mayank S
ID: 11733916
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

Author Comment

by:hawkly
ID: 11733930
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
ID: 11733944
>>Is that from the J2SE API or the J2ME API?

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

Expert Comment

by:Mayank S
ID: 11733977
>> JavaTM 2 Platform Std. Ed. v1.4.2

That is J2SE, not J2ME.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11733990
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
ID: 11733993
Anyone can tell me how call a function in thread from main thread?
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11733996
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
ID: 11734006
>> 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:Mayank S
ID: 11734038
>> 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
ID: 11734056
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 11734075
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
ID: 11734094
>>- 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
ID: 11734097
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:Mayank S
ID: 11734124
>> 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
ID: 11734139
>>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:Mayank S
ID: 11734162
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
ID: 11734199
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
ID: 11740551
methods are related to objects, not threads.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11742322
:-) perhaps he meant the thread class which he has (Receive)
0
 

Assisted Solution

by:krispin
krispin earned 30 total points
ID: 11745621
>>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:
Mayank S earned 70 total points
ID: 11750242
Well, I didn't pay attention to that. In that case, this would work:

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

Expert Comment

by:Webstorm
ID: 11760577
:-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11770364
:-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 134
expectj telnet failing 5 36
Running JavaFX on JDeveloper 12C 1 32
Crystal Reports Licensing Questions 4 12
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

809 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