Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread  RSS feed

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Devaka Cooray's Exam Simulator(made some changes)


here at TRead.<init>(TRead.java:3) is printed many times???
 
Devaka Cooray
Marshal
Posts: 5564
711
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here at TRead.<init>(TRead.java:3) is printed many times???


What you have seen is a part of the stack trace of StackOverFlowError. Well, you had modified the question, so now this is not a thread problem. This exception (StackOverFlowError) is occured when you are trying to recursively create instances of TRead class.

Try to compile and run the following modified program (this is cleary not about threads na), and try to understand this yourself






Devaka.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats because when you create a TRead object, it creates another TRead object and another and another....

I think this is a strange question because there is no need for a instance variable of type TRead because a TRead object is a Thread already. No need for getInst() and the run doesn't need to be synchronized.

Wouter Oet
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter,
It was a modified question.
You should read the answers.
 
Devaka Cooray
Marshal
Posts: 5564
711
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i replace TRead tr=new TRead(); with
static TRead tr=new TRead();

The answer is 12..


Yes! If tr is NON-static, there should be a NEW TRead instance associated with each TRead instances; which provides a recursive access.

But if tr is a static variable, a single instance of TRead will be shared within every instances of TRead class. So it doesn't provide a recursive execution.

Devaka.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Devaka. I understood.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!