• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Thread Question  RSS feed

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Options

Select 1 correct option.

1.It'll not compile as run method cannot be overridden.
2.It'll print both "Hello" and "World" 5 times each.
3.It'll print both "Hello" and "World" 5 times each but they may be interspersed.
4.Total 5 words will be printed.
5.Either 5 "Hello" or 5 "world" will be printed.


Ans given is: option 4.

I executed the code and at some point the result was 6 words printed.

I think the nature of the output cannot be determined.


Please explain.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. It is possible for thread1 to perform the check before thread2 increases the counter to 5, therefore 6 words can be printed.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then its an error question in JDiscuss Test4 for SCJP 1.4 Exam
 
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Then its an error question in JDiscuss Test4 for SCJP 1.4 Exam

Definitely.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there is no possibility of printiing "Hello"

Can someone please explain?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can do that. In fact there's no way to tell what will be printed except that neither word will be printed more than 5 times and the total number of words printed will be either 5 or 6.
 
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppl,

wouldn't polymorphism come into play? so the word Hello cannot be printed out correct?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------
It can do that. In fact there's no way to tell what will be printed except that neither word will be printed more than 5 times and the total number of words printed will be either 5 or 6.
--------------------------------------------------------------------------
Hi Jereon,
Why only 5 or 6 words will be printed?Why don't we get 5 "Hello" words and 5 "Worlds" printed?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi only five words are printed becose we r using static instanse variable i=0;
that retain its value..
but we cant expect what the word will be printed
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct.
Once 5 of any word are printed that loop will always complete.
Only in the very special case where one thread interrupts the other after the word has been printed but before the counter has been increased can a 6th word be printed and that will always be a different one.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Marzo Kaahn is correct. Polymorphism should come into play. When I executed the code several times, it always prints "world" 5 times.

So, I don't think it will print "hello" at all. Please explain if this is wrong.

Thanks
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run it another few thousand times

Remember you're talking about threading issues. Very little is guaranteed to happen.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, I don't think it will print "hello" at all. Please explain if this is wrong.

The behavior you experience is not guaranteed and may vary between runs (even on the same computer). Polymorphism has nothing to do with which values are printed. You should not depend on the thread scheduler for behavior.
[ July 11, 2005: Message edited by: Steve Morrow ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

What if we set max priority for Thread t1 will that print hello 5 times?

I just want to know How to set priority for t1 thread.I tried t1.SET_PRIORITY(10); shows error.

Plz correct me if iam wrong.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What if we set max priority for Thread t1 will that print hello 5 times?

Possibly, but the behavior is still not guaranteed. I say again, do not count on the thread scheduler for behavior.

I just want to know How to set priority for t1 thread.I tried t1.SET_PRIORITY(10); shows error.

I think the method you're looking for is setPriority().
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wouldn't polymorphism come into play? so the word Hello cannot be printed out correct?



Don't forget, t1 is not a TestClass, it is an A. That's why it is not a question of polymorphism.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!