Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt regarding threads

 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is from k&b threads,selftest(pgno:768,qno:13)

1.new Starter().run();
2.new Starter().start();
3. new Thread(new Starter());
4. new Thread(new Starter()).run();
5. new Thread(new Starter()).start();

if we place the above statements at line 1 in code which is true?
answers are:
c.only one might produce the output 4 2
d.exactly two might produce the output 4 4

my doubt is how can we find out the answer ? i run the above code
for 4 i got output-- 1 1
for 5 i got output--1 8

i am using 1.6 version. i am confusing which is correct ? how?
please any body explain me step wise
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my doubt is how can we find out the answer ? i run the above code
for 4 i got output-- 1 1
for 5 i got output--1 8

i am using 1.6 version. i am confusing which is correct ? how?
please any body explain me step wise


I think that you are misinterpreting the questions here. The "2" and "4" aren't (or may not be) the actual numbers. The significance is whether the numbers are the same or different -- meaning whether or not the code is being run by the same thread or different thread.

Henry
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think that you are misinterpreting the questions here. The "2" and "4" aren't (or may not be) the actual numbers. The significance is whether the numbers are the same or different -- meaning whether or not the code is being run by the same thread or different thread.

Henry

Hi Henry,
Thanks for giving some clarity.you mean if the same thread is run it,the numbers will be same and if the different thread run it numbers will be different. isn't it??
if my understanding is wrong please correct me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic