Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: more edits

...

This noncompliant code example uses a volatile done flag to indicate that it is safe to shutdown shut down the thread, as suggested in CON13-J. Ensure that threads are stopped cleanly. However, this does not help in terminating the thread because it the thread is blocked on some network IO as a consequence of using the readLine() method.

...

This noncompliant code example uses thread interruption to indicate that it is safe to shutdown shut down the thread, as suggested in CON13-J. Ensure that threads are stopped cleanly. However, this is not useful because the thread is blocked on some network IO as a consequence of using the readLine() method. Network IO is not responsive to thread interruption whne java.net.Socket is being used.

...

This compliant solution closes the socket connection, by having the shutdown() method close the socket. As a result, the thread is bound to stop because of the readLine() method will throw a SocketException. Note that there is no way to keep the connection alive if the thread is to be cleanly halted immediately.

...

This compliant solution uses an interruptible channel, SocketChannel, instead of a Socket connection. If the thread performing the network IO is interrupted using the Thread.interrupt() method while reading the data, the thread receives a ClosedByInterruptException and the channel is closed immediately. The thread's interrupt status is also set.

...

This noncompliant code example shows a thread-safe class DBConnector that creates a one JDBC connection per thread, that is, the connection belonging . Each connection belongs to one thread, and is not shared by other threads. This is a common use-case because JDBC connections are not meant to be shared by multiple-threads.

...

Unfortunately database connections, like sockets, are not inherently interruptible. So this design does not permit a client to cancel a task by closing it if the corresponding thread is blocked on a long running activity such as a join query. Furthermore, it is important to provide a mechanism to close connections to prevent thread starvation caused because of the , because there is a limited number of database connections available in the pool. Similar task cancellation mechanisms are required when using objects local to a method, such as sockets.

...