Adding Multithreading Capability to Your Java Applications

src: http://www.informit.com/articles/article.aspx?p=26326&seqNum=3

Interrupting Threads

A thread terminates when its run method returns. (In the first version of the Java programming environment, there also was astop method that another thread could call to terminate a thread. However, that method is now deprecated. We will discuss the reason later in this chapter.)

There is no longer a way to force a thread to terminate. However, the interrupt method can be used to request termination of a thread. That means that the run method of a thread ought to check once in a while whether it should exit.

public void run()
{
  . . .
  while (no request to terminate && more work to do)
  {
   do more work
  }
  // exit run method and terminate thread
}

However, as you have learned, a thread should not work continuously, but it should go to sleep or wait once in a while, to give other threads a chance to do their work. But when a thread is sleeping, it can't actively check whether it should terminate. This is where the InterruptedException comes in. When the interrupt method is called on a thread object that is currently blocked, the blocking call (such as sleep or wait) is terminated by an InterruptedException.

There is no language requirement that a thread that is interrupted should terminate. Interrupting a thread simply grabs its attention. The interrupted thread can decide how to react to the interruption by placing appropriate actions into the catch clause that deals with the InterruptedException. Some threads are so important that they should simply ignore their interruption by catching the exception and continuing. But quite commonly, a thread will simply want to interpret an interruption as a request for termination. The run method of such a thread has the following form:

public void run()
{
  try
  {
   . . .    while (more work to do)
   {
     do more work
   }
  }
  catch(InterruptedException exception)
  {
   // thread was interrupted during sleep or wait
  }
  finally
  {
   cleanup, if required
  }
  // exit run method and terminate thread
}

However, there is a problem with this code skeleton. If the interrupt method was called while the thread was not sleeping or waiting, then no InterruptedException was generated. The thread needs to call the interrupted method to find out if it was recently interrupted.

while (!interrupted() && more work to do)
{
  do more work
}

In particular, if a thread was blocked while waiting for input/output, the input/output operations are notterminated by the call to interrupt. When the blocking operation has returned, you need to call theinterrupted method to find out if the current thread has been interrupted.

NOTE

Curiously, there are two very similar methods, interrupted and isInterrupted. The interruptedmethod is a static method that checks whether the current thread has been interrupted. (Recall that a thread is interrupted because another thread has called its interrupt method.) Furthermore, calling the interrupted method resets the "interrupted" status of the thread. On the other hand, the isInterrupted method is an instance method that you can use to check whether any thread has been interrupted. Calling it does not change the "interrupted" status of its argument.

 

It is a bit tedious that there are two distinct ways of dealing with thread interruption—testing the "interrupted" flag and catching the InterruptedException.

It would have been nice if methods such as sleep had been defined to simply return with the "interrupted" flag set when an interruption occurs—then one wouldn't have to deal with theInterruptedException at all. Of course, you can manually set the "interrupted" flag when anInterruptedException is caught:

try
{
  sleep(delay);
}
catch (InterruptedException exception)
{
  Thread.currentThread().interrupt();
}

You need to use this approach if the sleep method is called from a method that can't throw any exceptions.

NOTE

You'll find lots of published code where the InterruptedException is squelched, like this:

try { sleep(delay); }
catch (InterruptedException exception) {} // DON'T!

Don't do that! Either set the "interrupted" flag of the current thread, or propagate the exception to the calling method (and ultimately to the run method).

If you don't want to clutter up lots of nested methods with isInterrupted tests, you can turn the "interrupted" flag into an exception.

if (isInterrupted()) throw new InterruptedException();

Assuming that your code is already prepared to terminate the run method when anInterruptedException is thrown, this is a painless way of immediately terminating the thread when an interruption is detected. The principal disadvantage is that you have to tag your methods with

throws InterruptedException

since, alas, the InterruptedException is a checked exception.


java.lang.Thread

  • void interrupt() 
    sends an interrupt request to a thread. The "interrupted" status of the thread is set to true. If the thread is currently blocked by a call to sleep or wait, an InterruptedException is thrown.
  • static boolean interrupted() 
    tests whether or not the current thread (that is, the thread that is executing this instruction) has been interrupted. Note that this is a static method. The call has a side effect—it resets the "interrupted" status of the current thread to false.
  • boolean isInterrupted() 
    tests whether or not a thread has been interrupted. Unlike the static interrupted method, this call does not change the "interrupted" status of the thread.
  • static Thread currentThread() 
    returns the Thread object representing the currently executing thread.
时间: 2024-09-28 13:17:53

Adding Multithreading Capability to Your Java Applications的相关文章

Free Apache Tomcat Hosting in the Cloud for Java Applications? It's Called OpenShift!

原文:https://openshift.redhat.com/community/blogs/free-apache-tomcat-hosting-in-the-cloud-for-java-applications-its-called-openshift If you are a Java developer, you have probably struggled with finding a reliable host for your Tomcat based java projec

An HTTP & HTTP/2 client for Android and Java applications OkHttp

  HTTP is the way modern applications network. It's how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth. OkHttp is an HTTP client that's efficient by default: HTTP/2 support allows all requests to the

Pushy is a Java library for sending APNs (iOS and OS X) push notifications

pushy Pushy is a Java library for sending APNs (iOS and OS X) push notifications. It is written and maintained by the engineers at RelayRides and is built on the Netty framework. Pushy was created because we found that the other APNs libraries for Ja

Scheduled Jobs with Custom Clock Processes in Java with Quartz and RabbitMQ

原文地址: https://devcenter.heroku.com/articles/scheduled-jobs-custom-clock-processes-java-quartz-rabbitmq   Table of Contents Prerequisites Scheduling jobs with Quartz Queuing jobs with RabbitMQ Processing jobs Running on Heroku Further learning There a

Java NIO Chapter1 Learning Tips

1.java.io效率低的原因But in most cases, Java applications have not truly been I/O bound in the sense that the operating system couldn't shuttle data fast enough to keep them busy. Instead, the JVMs have not been doing I/O efficiently. There's an impedance

JAVA程序员眼里的C#

程序|程序员 Java developer's view of C#.September 03, 2004 There are two ways to compare programming languages: a religious war and a "we just don't care" type of attitude. I will try to find the middle ground and start with generalities. The key fea

java语音实现技术TTS资料

要做语音,查了一些资料. Java Speech API :http://java.sun.com/products/java-media/speech/ Java Speech API 允许开发人员将语音技术整合到 Java applet 和应用程序的用户界面中.这种 API 规定一个跨平台的接口来支持命令-控制识别器.听写系统和语音合成器. 这个综合 API 被分成几个规范: Java Speech API 规范(Java Speech API Specification,JSAPI) Ja

44个JAVA代码质量管理工具(转)

  1. CodePro AnalytixIt's a great tool (Eclipse plugin) for improving software quality. It has the next key features: Code Analysis, JUnit Test Generation, JUnit Test Editor, Similar Code Analysis, Metrics, Code Coverage and Dependency Analysis.2. PM

Java JWT: JSON Web Token

  Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand library for creating and verifying JSON Web Tokens (JWTs) on the JVM. JJWT is a Java implementation based on the JWT, JWS, JWE, JWK and JWA RFC specific