• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java threads

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a main program which talks to a network device. I want to initiate a thread from the main program which checks if the main program has completed execution. If yes, i want to kill this thread else i want to throw an exception and stop termination of the main program. How can it be done?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This cannot be done . Because main is a aaemon thread that is necessary for the child thread ( user created thread) to run . The main thread will finish executing only after the child thread has finished execution.
 
TechR
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for typo , what i meant was : main is a daemon thread
 
Shubha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not want to kill the main thread. I want to throw an exception from the user created thread to the main thread and return from the main program. Is this possible? If yes, how will i access the state of the main thread?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by TechR:
Sorry for typo , what i meant was : main is a daemon thread



This is not true. The "main" thread -- the thread that calls the main() method that starts the application -- is not a daemon thread.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubha P.P:
I do not want to kill the main thread. I want to throw an exception from the user created thread to the main thread and return from the main program. Is this possible? If yes, how will i access the state of the main thread?



Exceptions are not designed to be thrown across threads. To do that, you need to use a framework that will do that (BTW, the callable tasks supported by the executors support a similar mechanism).

In your case, it doesn't even make sense. You want to throw an exception to the main thread, after the main thread has terminated?

Henry
[ April 14, 2008: Message edited by: Henry Wong ]
 
Shubha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me put my question clearly again. I have a main thread. I start a child thread from the main thread. After that i invoke an API in the main thread which interacts with the network element. Now the job of the child thread is to see if the API invoked in the main thread is done its execution. If yes, the chld thread should kill itself and return to the main thread. If the API does not complete execution within 30 mts, then the child thread should throw an exception to the main program and the main program should return. I am confused how to monitor the state of main thread from child thread.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubha:If yes, the chld thread should kill itself and return to the main thread.


What do you mean by the child thread should return to the main thread? A thread once started runs on its own and does not return as a method call.
I dont think killing the thread is the write word to use. If the run method returns, the thread stops, i think this is what you want.

Shubha:If the API does not complete execution within 30 mts, then the child thread should throw an exception to the main program and the main program should return.


Again, a thread once started runs on its own and can not throw an exception to the thread that started it.(A difference between starting a thread and calling a normal method.)

Shubha:I am confused how to monitor the state of main thread from child thread.


This is one thing that probably is a valid question.
So, from your post there is the following that I understand:

1) You have to do some task in a separate thread that you expect to complete withing 30 mins. You call this the main thread. (Main thread is mostly referred to as the thread that calls the main() method of the class that is used to launch the JVM. So, this terminology is a little misleading.)
2) For making sure that the above thread does not run for more than 30 mins, you have spawned one more thread that you call as child thread.

For avoding confusion, I will call the first thread as T1 and the second one as T2.
You can achieve what you want by calling join(long) on T1. This will make the calling thread to wait (for a maximum of the time duration) for T1 to finish.
You can modify the code of T1 to update a shared variable denoting the process completion status when finished.
After the thread that called join() on T1 returns, it can check the shared variable value. If it is done then quietly process else interrupt T1 or signal T1 to stop. (stop() is deprecated so you can not forcibly stop a thread)

Below is a sample code. This code assumes that the thread that starts T1 does not have any other work to do. So, it just calls join with T1. If this is not the case for you, you can spawn T2 that just joins on T1.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shubha P.P:
Now the job of the child thread is to see if the API invoked in the main thread is done its execution.



Why make it hard on yourself?

Have the main spawn a thread to do the long task. Have another thread to check. Let the main just drink coffee.

And you really need to fix your profile name.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Shubha",
Welcome to JavaRanch!

We're pleased to have you here with us here on the ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. You can change it here

Note: Accounts with invalid screen names are subject to being closed.
[ April 22, 2008: Message edited by: Ben Souther ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic