This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q on thread at Jxam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q on thread at Jxam" Watch "Q on thread at Jxam" New topic
Author

Q on thread at Jxam

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Q: Which of the following will definitely stop a thread from executing?
One the of correct answer is : yield()
I was not sure if yield will definitely stop the thread....what if there is no other thread of same priority ready to run...
Thanks
Barkat
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
I think yield gives other same priority threads a chance to run. So the thread which executes yield stops, if no other thread is ready to run , this thread again starts up
V
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Technically, we can say that it is possible same that same thread will continue. As question is asking for what will definitely stop thread, I am not sure if yield() is correct answer. For example wait(), sleep() will definitly stop the thread....
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Barkat,
I am of the opinion that none of wait, sleep or yield will definitely stop a thread from executing. We cannot know for sure, because we do not have the source code.
According to the Java Programming Language, yield provides a hint to the scheduler that the current thread need not run at the present time, so the scheduler may choose another thread to run. The scheduler may follow or ignore this suggestion as it sees fit.
As for wait and sleep, if Thread-1 invokes interrupt on Thread-2 (thereby setting the interruption status to true), when Thread-2 invokes wait or sleep, the thread returns immediately. I suspect the thread never stopped executing.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
what about wait or sleep for certain amount of time?
Harry Singh
Ranch Hand

Joined: May 02, 2001
Posts: 124
Hi all,
we do have a method Thread.stop(), which can stop the thread from being executing but this method is deprecated. Do we have any other method which works the same as thread..
Taking this discussion bit further, can we actually stop a thread when that thread is actually running.. suppose thread t1 is running from main thread, so how can i stop the thread t1 to stop running from main thread. is this functionality allowed in Java?
Another thing that comes to my mind is, actually when is the interrupted exception thrown and what causes this exception. what does Thread.interrupt() method does?
Thanks in advance.
Regards,
harry
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
what about wait or sleep for certain amount of time?

If a thread invokes wait(int), sleep(int) or join(int), we expect the thread to block (stop executing). However, if the interruption status is true prior to any one of these 3 methods being called, the thread appears to return immediately. I don't think the thread stops executing.

[ August 19, 2003: Message edited by: Marlene Miller ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
This is what the API says about the yield() method:

Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is what Doug Lea says in Concurrent Programming in Java 1.1.2.5
Thread.yield is a purely heuristic hint advising the JVM that if there are any other runnable but non-running threads, the scheduler should run one or more of these threads rather than the current thread. The JVM may interpret this hint in any way it likes.
... On JVM implementations that employ pre-emptive scheduling policies, especially those on multiprocessors, it is possible and even desirable that the scheduler will simply ignor this hint provided by yield.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is what Doug Lea says about wait:
If the current thread has been interrupted, then the method exits immediately, throwing an InterruptedException. Otherwise, the current thread is blocked.
(So, contrary to the opinion of some mock exams, wait and join, and probably sleep, do not alway cause a thread to block.)
Harry Singh
Ranch Hand

Joined: May 02, 2001
Posts: 124
Hi All,
As far as yield() is concerned, the yield() method just changes the state of thread i.e. from running state, the yield() method changes the state to runnable state() and by this means that if there are some other threads waiting, then they get a chance to have CPU and if there are no other threads, then this thread can relinquish CPU. so that means that yield() stops execution of the thread and changes the thread...
Marlene.. can u tell us more about the interrupted exception and What u mean by when u say Threads being interrupted.
Regards,
Harry
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Harjinder
Try this link.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
posted by Marlene:

quote:
--------------------------------------------------------------------------------
what about wait or sleep for certain amount of time?
--------------------------------------------------------------------------------

If a thread invokes wait(int), sleep(int) or join(int), we expect the thread to block (stop executing). However, if the interruption status is true prior to any one of these 3 methods being called, the thread appears to return immediately. I don't think the thread stops executing.
code:
--------------------------------------------------------------------------------
class Test extends Thread { public void run() { interrupt(); //interrupt myself synchronized(this) { try { System.out.println("isInterrupted = " + isInterrupted()); System.out.println("wait " + System.currentTimeMillis()); wait(5000); } catch (InterruptedException e) { System.out.println("catch " + System.currentTimeMillis()); } } } public static void main(String[] args) { new Test().start(); }}isInterrupted = truewait 1061329071606catch 1061329071606

Hi Marlene:
I believe the same thread is running again because:
1. There is only one thread.
2. the interrupt status is set to true before even thread starts.
I think this is what is happening:
a. interrupt is set to true
b. thread goes in wait state, continously checks for interrupt status
c. as interrupt is true, does not wait anymore
d. goes in ready to run state
e. as there is no other competing thread, this thread goes in running state right away.
All this is happening within the millisecond. so you see no time difference.
If you were to create two threads, you will see that two threads are inter-mingled, however there could / could not be any recorded time lapse in a particular run.

This is what is happening:
a. first thread goes in run state
b. it's interrupt is set to true
c. it goes in wait state
d. as it is already interrupted, it will go in ready to run state
e. but second thread will surely get time on cpu
and print at least one statement
f. first is caught
e. second goes in wait
g. second is caught
So I think that execution of wait() will stop the thread momentarily (because it has already been interrupted).
So I guess original question about "definitely stoping a thread" can be summerized:
"A thread can be definitely stopped for n milliseconds by invoking wait(n) or sleep(n) as long as it's interrupt is not set to true. However, if interrupt is set to true prior to invoking wait(n) or sleep(n), the thread will stop momentarily and run again"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q on thread at Jxam
 
Similar Threads
Thread
does yield prevent thread execution?
Thread Q from jexam
yield()
yield();