Forums Register Login

Why Thread loop is not finishing without mentioning printing?

+Pie Number of slices to send: Send
I was trying to read an article from here http://www.javaworld.com/article/2071214/java-concurrency/java-101--understanding-java-threads--part-3--thread-scheduling-and-wait-notify.html
I was doing the same example but loop is not ending. Thread is struck at CalculateThreadDemo run method. If i mentioned System.out.println
then it got terminated else infinite loop continues. I don't understand this. Please help me.

code:

BlockedThreadDemo




CalculateThread




Main class:





Output:

With commenting the statement:

1
i: = 49i: = 13i: = 10
1
i: = 49i: = 13i: = 10
isFinished: true
main thread is dead
1
i: = 49i: = 13i: = 10
BlockedThreadDemo is finished.
1

1
1
...


Without commenting the statement:


597222
597223
597224
597225
isFinished: true
main thread is dead
597226
CalculateThread is finished.

i: = 49i: = 13i: = 10
BlockedThreadDemo is finished.
+Pie Number of slices to send: Send
I would start by forgetting about an article that was written over 12 years ago. It is not current practice to extend Thread, rather you would implement Runnable instead.

For learning about how to work with concurrency (in this decade), I recommend the The Java Tutorials : Concurrency.
+Pie Number of slices to send: Send
Agree with your point that this is old practice(extending Thread class) but i want to know why this is coming when it should not be? It could come using Runnable
as well as problem is in run() method. I am searching where i am getting wrong..
+Pie Number of slices to send: Send
Hi Tushar, correct me if am wrong ..
Here you are using two different thread say BlockedThreadDemo and CalculateThread and one main thread that contains the main().The main catch is


bt1.setFinished(true);
ct1.setFinished(true);



After sleeping 5 sec the main therad wakes up and setting these two flags.In the mean while the CalculateThread thread completed its execution.The BlockedThreadDemo thread is still alive , if we make a enter (\n) there the BlockedThreadDemo thread comes up from its while and the thread ends up its execution ..The app also complete its execution.


Satya
+Pie Number of slices to send: Send
If you're working on this example to learn about concurrency, then you're wasting your time by learning the wrong thing.
+Pie Number of slices to send: Send
Thanks Tim, I modified the code as per current practice but still showing same thing. I am trying to learn only scheduling thing.

Updated code:









meantime i am looking for oracle docs for the same.

+Pie Number of slices to send: Send
 

In the mean while the CalculateThread thread completed its execution.


It doesnot.
+Pie Number of slices to send: Send
I figured it out. I have to use volatile to solve it.. Thanks Tim again.. I will stop using this practice now..
Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 599 times.
Similar Threads
strange results from wait() and notify()
How to access variable from run() method
Problem with while statement
Daemon Thread
question about using boolean to check a condition of an object?
Thread - Wait & Notify Example
How to implement Decrementing Carousel With Limit
Trying to access methods from multiple Threads of the same class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:00:20.