Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processor#run() 方法的改进的一个建议 #57

Open
ykgarfield opened this issue Mar 10, 2017 · 0 comments
Open

Processor#run() 方法的改进的一个建议 #57

ykgarfield opened this issue Mar 10, 2017 · 0 comments

Comments

@ykgarfield
Copy link

Processor#run() 有如下的一个代码片段:

SelectionKey key = null;
try {
	key = iter.next();
	iter.remove();
	if (key.isReadable()) {
		read(key);
	} else if (key.isWritable()) {
		write(key);
	} else if (!key.isValid()) {
		close(key);
	} else {
		throw new IllegalStateException("Unrecognized key state for processor thread.");
	}
} 

这里是先判断了 isReadable()isWritable(), 最后判断的 key.isValid(), 这里最好应该先判断 key.isValid(). 在判断 isReadable()isWritable().因为在判断 isReadable()isWritable() 之前 key 已经无效了, 所以可能会产生 CancelledKeyException.虽然在 catch 中最终会捕获到这个异常, 但是按照比较好的方式做的话, 应该还是先判断 key.isValid() 会更好一点.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant