Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cancellation and Interruption in Threads  RSS feed

 
Mohit Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In Java Concurrency in Practice[JCIP] , there is explanation about how to use cancellation and interruption in threads .This example is on Page 21 of Chapter7.Cancellation and Shutdown which states : Listing 7.3. Unreliable Cancellation that can Leave Producers Stuck in a Blocking Operation. Don't Do this.


Here actually they are telling in order to stop any thread operation , just create a volatile flag which can be checked .Depending upon the status of that flag , thread execution stops.

Now there is one program for explaining same .It works fine there ,Below is the example :




In above code cancelled is the volatile flag which we can check for the cancellation check and thread execution stops if its true .




But if we do the same operation which we have done above but use BlockingQueue there is some problem.

If, however, a task that uses this approach calls a blocking method such as
BlockingQueue.put, we could have a more serious problem the task might never check the cancellation
flag and therefore might never terminate.

BrokenPrimeProducer in below program illustrates this problem. The producer thread generates primes and
places them on a blocking queue. If the producer gets ahead of the consumer, the queue will fill up and put
will block. What happens if the consumer tries to cancel the producer task while it is blocked in put? It can
call cancel which will set the cancelled flag but the producer will never check the flag because it will
never emerge from the blocking put (because the consumer has stopped retrieving primes from the queue).


Here is the code for the same ::




I am not able to understand why cancellation will not work in case of blocking Queue in second code example. Please explain !
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit Chauhan wrote:
I am not able to understand why cancellation will not work in case of blocking Queue in second code example. Please explain !


It does kinda makes sense, doesn't it? Cooperative polling algorithms need to actually poll to work. If they can't poll, they can't really be cooperative, which means that it doesn't really work. And of course, a thread can't poll when it may be indefinitely blocked on a queue.

Henry
 
Mohit Chauhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for reply.But my doubt is how is it getting blocked , since if we are producing , we are consuming threads as well down the line .
So in which condition is it blocked and also according to JCIP(Java Concurency in Practice) , in below code its stated as :

What happens if the consumer tries to cancel the producer task while it is blocked in put? It can
call cancel which will set the cancelled flag but the producer will never check the flag because it will
never emerge from the blocking put (because the consumer has stopped retrieving primes from the queue)


Now this statement which is given : it will never emerge from the blocking put (because the consumer has stopped retrieving primes from the queue) in above sentence is what seems confusing to me

In below code there is a call to consume(primes.take()); which is consuming the tasks from queue as well.So when and where has consumer stopped retrieving . Please explain .




Thanks
Mohit



 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit Chauhan wrote:
In below code there is a call to consume(primes.take()); which is consuming the tasks from queue as well.So when and where has consumer stopped retrieving . Please explain .


It seems pretty clear to me...



When the cancel() method is call at line 25, the consumer has already left the while loop and has already stopped retrieving. If the producer blocks, while the consumer is between line 23 and line 25, there is no way for the producer to unblock.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!