• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Danchisholm question -threads

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Theres a question by danchisholm;

class A extends Thread {
private Object obj;
public A(Object obj) {this.obj = obj;}
public void run() {
try {
synchronized (obj) {obj.wait();}
} catch (InterruptedException ie) {}
System.out.print(Thread.currentThread().getName());
}}
class B {
private void m1() {
for (int i = 0; i < 10; i++) {
A t1 = new A(this);
t1.setName(String.valueOf(i)); t1.setDaemon(true); t1.start();
}
synchronized (this) {notifyAll();}
}
public static void main(String[] args) {new B().m1();}
}

What are the possible results of attempting to compile and run the program?

a. All of the numbers 0 through 9 must always be printed
b. Some or all of the numbers 0 through 9 could be printed
c. Nothing is printed
d. Run-time error

The answer is b&c.
There are no chances that any number will be printed because daemon thread will die as soon as the main thread finishes.
Is it correct to give a. as an option too?

Thanks in advance.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I dont think a is a right option since it has the word "must" and the behaviour of threads is never guranteed.It might print nothing as said in ans.b

Regards
Smitha
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic