Forums Register Login

Thread

+Pie Number of slices to send: Send
Source: Devaka Cooray's Exam Simulator(made some changes)


here at TRead.<init>(TRead.java:3) is printed many times???
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Wouter,
It was a modified question.
You should read the answers.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thanks Devaka. I understood.
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 597 times.
Similar Threads
Thread
synchronized block
Thread start
synchronized method
Question on multithreading from Sierra/Bates Chapter 9, Question 16
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:41:12.