class GetPort{ 共享資源類
(_________) int port; 共享的端口
public GetPort(int port){ 構造方法
this.port = port;
}
(_________) int getPort(){ 獲得端口累加號
return port++;
}
}
class ScanThread extends (_________){ 掃描端口線程類
String host; 目標主機
GetPort port; 共享的端口資源
Thread t; 主線程
String name; 子線程名稱
public ScanThread(String host, GetPort port, String name, Thread t){ 構造方法
this.host = host;
this.t = t;
this.port = port;
this.name = name;
}
(_________)(){ 線程運行方法
try{
Socket s = new (_________)();
RandomAccessFile raf;
int p = 0;
while((p = port.getPort())<(_________)){
SocketAddress inet = new (_________)(host, p);
s.(_________)(inet, 5000);
if(s.(_________)()){
raf = new RandomAccessFile("record.dat", "rw");
raf.(_________)(raf.length());
raf.(_________)(p);
raf.close();
}
}
s.close();
}(_________)(Exception e){
e.printStackTrace();
}(_________){
try{
t.(_________)();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
class mainThread{ 主類
public static void main(String [] args){ 主方法
int flag = 0; 線程累計數
String host; 目標主機
Thread t = Thread.currentThread(); 獲得主線程
GetPort p = new GetPort(0); 共享的端口累加資源
try{
host = (new (_________)(System.in)).next();
for(int i=0; i<10; i++){
(new ScanThread(host, p, "Thread-"+i, t)).(_________);
while(flag < 10){ 採用喚醒等待的方法,保證子線程都正常結束
synchronized(t){
flag++;
t.(_________)();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
(10.0分)
我的答案:
第一空:
private
synchronized
Thread
public void run
Socket
65535
InetSocketAddress
connect
isReachable
seek
writeUTF
catch
finally
close
Scanner
start
wait
2
/* xml格式如下
<book>
<name></name>
<price></price>
</book>
*/
import java.util.List;
import java.io.RandomAccessFile;
import (_________)ByteBuffer;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
class TransferDataFromXMLtoFILE{
public static void main(String [] args){
try{
SAXReader reader = (_________);
Document document = reader.(_________)("books.xml"); 打開xml文件
Element root = document.(_________)();
List<(_________)> list = root.elements("book");
RandomAccessFile raf = new RandomAccessFile("books.dat", "rw");
FileChannel fc = (_________)();
String msg = null;
ByteBuffer buf = ByteBuffer.(_________)(256);
while(list.(_________)()){
Element bookE1 = (_________)list.next();
msg = bookE1.elementText("name");
msg += bookE1.elementText("price");
buf.(_________)();
buf.put(msg.(_________)());
buf.(_________)();
while(buf.(_________)())
fc.write(buf);
}
fc.close();
raf.close();
reader.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
(10.0分)
我的答案:
第一空:
java.nio.
new SAXReader()
read
getRootElement()
Element
raf.getChannel()
allocate
isEmpty
Element
clear
getBytes
flip
hasRemaining
3
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class ChatRoomServer {
private Selector selector = null; 事件選擇器
static final int port = 9999;
public void init() throws IOException
{
selector = Selector.(_________)(); 獲得selector對象
ServerSocketChannel server = ServerSocketChannel.(_________)(); 獲得channel對象
server.(_________)(new InetSocketAddress(port));
server.configureBlocking(_________);
server.register(selector, (_________)); 註冊監聽事件
System.out.println("Server is listening now...");
while(true) {
int readyChannels = selector.select();
if(readyChannels == 0) continue;
Set selectedKeys = selector.(_________)(); 獲得事件列表
Iterator keyIterator = selectedKeys.iterator();
while(keyIterator.hasNext()) {
SelectionKey sk = (SelectionKey) keyIterator.next();
keyIterator.(_________)(); 移除已處理過的事件
dealWithSelectionKey(server,sk);
}
}
}
public void dealWithSelectionKey(ServerSocketChannel server,SelectionKey sk) throws
IOException {
if(sk.isAcceptable()) 有連接請求
{
SocketChannel sc = server.(_________)();
sc.(_________)(false);
sc.register(selector, SelectionKey.OP_READ);
sk.interestOps(SelectionKey.OP_ACCEPT);
}
if(sk.isReadable()) 有數據到達
{
SocketChannel sc = (SocketChannel)sk.(_________)();
ByteBuffer buff = ByteBuffer.(_________)(1024);
StringBuilder content = new StringBuilder();
try
{
while(sc.read(buff) > 0)
{
buff.(_________)();
content.append(buff);
}
System.out.println(content);
sk.(_________)(SelectionKey.OP_READ); 追加關注事件
}
catch (IOException io)
{
sk.cancel();
if(sk.channel() != null)
{
sk.channel().close();
}
}
}
}
public static void main(String[] args) throws IOException
{
new ChatRoomServer().init();
}
}
(10.0分)
我的答案:
第一空:
open
open
bind
false
selectionKey.OP ACCEPT
selectedKeys
remove
accept
configureBlocking
channel
allocate
flip
interestOps