• 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

///thread///

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JohnJuntMockExam...
Which methods may cause a thread to stop executing?
a.sleep();
b.stop();
c.yield();
d.wait();
e.synchronized()
my answer are acd,but the answer are ab.
method stop() is depreciated,so we can skip it.
my question is option c and d??
They can put the current executing thread into waiting state.It's their definition,isn't it??
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you've more than two threads and you've called yield() on first thread, it's possible that this thread will stop running and let other threads to run.
But if you've just one running thread and you call yield() on this, it will not stop - because there's no another thread...
The same concerns wait().
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree with the same concerns wait()
If the only thread is made to wait, it is still waiting. I mean it was clearly "stopped".
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait() and notifyAll() are only used in a code paragraph signed by synchronized,no matter a thread exists or not. So in a thread, if there is no synchronized code, wait() can't be used. So the thread can't be stopped using wait method.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what are the answers?
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jamal,
Are you sure that if a program doesn't create
any other thread there will be just the main
thread ?
How about the garbage collector though of low
priority ?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Alan
Nice question - I don't know what happens when GC waits...? but I'm sure that yield() doesnt' stop thread any time. It depends on situation.
P.S. Maybe GC is JVM's private thread - therefore main thread doesn't stop...
Additionally you can read JLS, CHAPTER 17 - Threads and Locks
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wait() and notifyAll() are only used in a code paragraph signed by synchronized,no matter a thread exists or not. So in a thread, if there is no synchronized code, wait() can't be used. So the thread can't be stopped using wait method.


This makes no sense to me at all. Having the wait() method in synchronized code or not does not affect its ability to stop the thread. (Btw, An IllegalMonitorStateException runtime exception is thrown when wait() and notify() isn't in synchronized code.)
My answers are all of them, actually. When the thread encounters synchronized code and it doesn't have that object's lock, it stops executing and waits until it obtains that lock.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic