Solved

sub function in thread

Posted on 2004-08-06
25
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 64
ejb entity bean example issue 2 26
Why doesn't this text field show up on my Applet frame? 2 28
Java array 21 84
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

749 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