• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Thread loop is not finishing without mentioning printing?  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're working on this example to learn about concurrency, then you're wasting your time by learning the wrong thing.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the mean while the CalculateThread thread completed its execution.

It doesnot.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out. I have to use volatile to solve it.. Thanks Tim again.. I will stop using this practice now..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!