?
Solved

sub function in thread

Posted on 2004-08-06
25
Medium Priority
?
535 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 80 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 120 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 120 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 280 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 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