• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Thread

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First of all i would like to give thanks to ajith who helped me over some questions related to ThreadGroup. even though ThreadGroup is not in objectives of SCJP.
Again i am looking for some help from our java experts on thread. basically i have some confusions and some questions here. All are related to thread.
Q#1
This statement is from RHE and page#204 :
When the run() methods returns, the thread has finished its task and is considered dead........ The dead thread continues to exist; it is an object like any other, and you can still access its data and call methods. you just can't make it run agian. in other word.
You can't restart a dead thread.
You can call the methods of a dead thread.
Here i understand that i can't use t.start() on a dead thread. but i don,t understand second point.
how can i call method on dead thread?
One more confusion
are dead threads not available for GC?
Q#2
List#1 : sleep and yield methods donot release the lock hold by thread.
List#2 : wait does release the lock hold by thread.
(what about suspand() and interrupt() methods?)
are there any other methods u would like to add in list#1 and List#2?
Q#3
A Thread can we considered dead when :
1. run() method returns
2. we invoke stop() method on thread ( i know it is deprecated)
are there any other way?
Q#4
A thread stop its execution due to
1. stop() method
2. sleep() method
3. yield() method
4. suspend() method
5. any method like waitForInput(), waitForImages() and waitForID()
6. due to IO
any thing else u would to like add.

Please try to understand me. i am trying to learn and i want your help. as we all knows that Thread is really one of the importent concept for the exam point of view.
Please correct me if a am wrong anywhere above.
Waiting for your reply.
thanks in advance.
vivek
[This message has been edited by Vivek Shrivastava (edited July 21, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 21, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 21, 2000).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vivek,
You can restart a thread by creating an object of the thread
once again and call its start method.
Once a thread is dead , you can still access the other methods
of the class as you access other methods in the normal class.
us'man
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q#3
A Thread can we considered dead when :
1. run() method returns
2. we invoke stop() method on thread ( i know it is deprecated)
-------rajsim----------
3. destroy - will terminate it
4. un-handled exception will terminate it
5. System.exit will terminate all threads
6. Deamon threads may terminate when all user threads are finished
7. Other reasons include System shutdown, hardware switched off
--------rajsim----------------
are there any other way?
Q#4
A thread stop its execution due to
1. stop() method
2. sleep() method
3. yield() method
4. suspend() method
5. any method like waitForInput(), waitForImages() and waitForID()
6. due to IO
--------rajsim----------------
7. wait() method and variants
8. trying to gain a lock on an object using synchronized
9. Another thread of higher priority becomes ready to run
--------rajsim----------------

[This message has been edited by rajsim (edited July 21, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
I would like to answer Q1 for you.
The statement here is actually referring to the thread Object. Like all objects, thread is an object too. However, it has a special ability to spawn a parallel path of execution. The only thing of interest for the thread scheduler is the run method. For all practical purposes, that is the "workload" of the thread. The job of the scheduler is to complete executing the run method, and while doing so, maintain appropriate information about the state of the thread.
Once the run method completes execution, the scheduler marks this thread as dead. However, since Thread object is also an object, it is governed by the same lifetime and scope rules that holds good for ordinary objects. This means, you can access the state information( attributes ) and call instance methods on the object. Though the semantics of multi-threading discourages one from re-starting a deadthread( perhaps to avoid inconsistent object states ), you can do otherthings such as calling Thread methods eg -isAlive, isDaemon, getThreadGroup, toString, getPriority etc.
The practical advantage of this feature can be realised when you implement threads by extending the Thread class. Your subclass might contain data( for example, results of some computation ) and/or other methods. Even after the thread has finished the execution, you will be able to access these entities from the Thread object.
Last but not the least, the dead thread can be restarted by creating an instance of Thread and passing the dead object as the target to the constructor. Remember this may/may not be prudent especially when your class has some computed state information.
Hope my explanation supplements other answers
Ajith
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all,
thanks usman,rajsim and ajith. i really appreciate your time and effort.
One more request for u guy i know u are not going to ignore it. please look into my question #2. i will really appreciate your effort.
waiting for reply.
vivek
[This message has been edited by Vivek Shrivastava (edited July 21, 2000).]
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lock can be released by
1. execution of wait method. (lock has to be re-gained after wait finishes by notify, time-out or InterruptedException)
2. completion of synchronized code (naturally or by exception) but not by suspend, stop or destroy deprecated methods
3. termination of thread
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Still some confusion. I was going through some old thread here and I found that Suspend(), stop() or interrupt() methods does cause thread to release the lock. http://www.javaranch.com/ubb/Forum24/HTML/002418.html
Please help here!
Thanks in advance.
vivek
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,
I'll try to clear up your confusion:
1) sleep(), yield() --- do not release any locks
2) wait() --- releases the lock
3) suspend() --- does not release the lock, and may lead to a deadlock if the thread which will call resume() is trying to seek the lock. Hence this method is deprecated as it is deadlock-prone.
4) stop() --- releases all locks held by the thread. May lead to inconsistent/damaged data, hence it is also deprecated
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bhatra.
really appreciate it.
vivek
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic