• 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

suspend()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I found in a mock exam:
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}

public static void main(String args []) {
(new ThreadTest()).start();
}}

A - Compilation will fail in the method main.
B - Compilation will fail in the method run.
C - A warning will be generated for method run.
D - The string "In run" will be printed to standard out.
E - Both strings will be printed to standard out.
F - Nothing will happen.
The answer is D but why is not E??
Thanks in advanced
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jordi
Only the first string will be printed out because right after it prints it it calls the suspend method and stops running. There is no way for it to get to and call the resume method because it is suspended. The code will print the first string to the screen then just hang.
Hope that helps
Dave
by the way keep in mind that both suspend and resume are deprecated and are not on the test. You should focus on the newer thread controls (wait, notify, notifyAll, yield)
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, suspend() and resume() are deprecated as of Java 2, so they shouldn't show up on the exam.
Second: A suspended thread can only be resumed from another thread.
The reason the code doesn't print "Leaving run" is because the thread is never resumed and that SOP statement is never reached. For that matter, resume() is never reached because a thread can't resume itself. The control comes from outside the thread.
April
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The resume() statment would never be executed in the code snipplet posted.Hence we donot see the second statement in the output.
Also, make use of wait() and notify() instead of suspend() and resume().The latter methods are deprecated and prone to deadlock.Refer to the API docs for more information on why these methods are deprecated.
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 28, 2001).]
 
There is no "i" in denial. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic