aspose file tools*
The moose likes Threads and Synchronization and the fly likes Java threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Java threads" Watch "Java threads" New topic
Author

Java threads

Shubha
Greenhorn

Joined: Apr 14, 2008
Posts: 3
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?
TechR
Greenhorn

Joined: Jul 07, 2006
Posts: 2
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

Joined: Jul 07, 2006
Posts: 2
Sorry for typo , what i meant was : main is a daemon thread
Shubha
Greenhorn

Joined: Apr 14, 2008
Posts: 3
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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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

Joined: Apr 14, 2008
Posts: 3
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.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java threads