• 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

Q on thread at Jxam

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about wait or sleep for certain amount of time?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harjinder
Try this link.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
reply
    Bookmark Topic Watch Topic
  • New Topic