• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

threads

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1.
Given the code
1public class ThreadDemo extends Thread{
2 public void run(){
3System.out.println("Thread Started");
4//What Code ???
5 System.out.println("Done");
6 }
7 public static void main(String[] arg){
8ThreadDemo td = new ThreadDemo();
9td.start();
10 }
11}
Which of the following given code segment(s) below can be placed at line 4 to print both "Thread Started" and "Done" to the standard output.

A.suspend(); resume();
B.sleep(10000);
C.yield();
D.yield(); resume();
E.All the above
ans given is c & d why not a.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gosh,
c and d sure seem unlikely. in my opinion, the correct answers should be a,b,c.
a: suspend() and resume() should speak for themselves
b: after a period of 10000ms., thread goes to ready state. the scheduler, having determined that no other threads are in pool, reschedules the same thread for exec
c: same reason as above
d: incorrect, as resume() can only be used upon a thread that has been suspend()ed
am i correct or just confused??
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Chetan! You can't use sleep() method as it throws interrupted exception, it has to be enclosed in a try-catch construct.
(a) suspend() followed by resume() does not resume execution of thread by itself. So "Done" is not printed. When a thread invokes suspend() method its execution is suspended and the thread keeps waiting. I am not sure if it can resume execution on its own. I suspect it has to be another thread that has to invoke resume() on this thread.
yield() method immediately gives away its execution privilege and enters ready state. So it is immaterial whether resume() is called after invocation of yield or not.
So, correct answers are C and D.
[This message has been edited by Ramesh Donnipadu (edited September 23, 2000).]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You cannot use sleep(10000) because
Exception java.lang.InterruptedException must be caught, or it must be declared in the throws clause of this method.
yield();--is ok
yield();resume();--is ok
Regading the first option suspend and resume in this case the only user Thread in the entire application is suspended indefinately and the suspended thread cannot unsuspend themselves,look at this discussion for further clarification
http://www.javaranch.com/ubb/Forum24/HTML/003783.html
Surya
 
chetan nain
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, let me correct myself.
option d is actually correct, as it is permitted to resume() a thread
that has never been suspend()ed. the thread just goes about its normal
business
option b is a tricky one. it would have been correct if the sleep(10000)
would have been enclosed in a try{} catch{InterruptedException e} block
so the correct answers are c,d
however, theres another issue here. for options a and d, compiler
complains about method deprecation and suggests alternative method use.
so, speaking deeply, the only option that will compile cleanly and
print the required is c.
sherrifs, please help
 
Trailboss
Posts: 23227
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PROPER NAMES ARE NOW REQUIRED!
See http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details.
 
That new kid is a freak. Show him this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic