当前位置:首页 > JAVA期末复习资料2
private int m_id;
public (2)_________(Socket s, int id) { m_socket= s; m_id= id; }
public void (3)_______{
try {(4)_______. sleep(10000);
System.out.println(\ m_socket.close();
} catch (Exception e) {} }
public static void main(String args[]){ int n= 1;
_(5)_________ server= null; try{
server= new (6)____________(5000); System.out.println( \ }
catch (IOException e){} while (true){ try{
System.out.println(\(7)_________ s= server.accept( );
ServerExample t=new ServerExample(s, n++);
(8)_________ th =new Thread(t); (9)______.start( ); }
catch (IOException e){}
} // End of loop: while } // End of method: main }
2、下面是一个支持多个客户端交互的程序,请根据注释要求补充、完成代码: importjava.io.*;
(1)________ //加载网络API包
public class ServerThread extends Thread{ Socket socket=null; int clientnum;
publicServerThread(Socket socket,int num) { this.socket=socket; clientnum=num+1; }
public void run() { try{
String line;
InputStream in=(2)_______________//得到socket的输入流
BufferedReader is=new BufferedReader(new InputStreamReader(in)); PrintWriteros=new PrintWriter(socket.getOutputStream());
//由系统标准输入设备构造BufferedReader对象 BufferedReader sin=new BufferedReader ((3)______________);
System.out.println(\line=sin.readLine();
while(!line.equals(\os.println(line);
//刷新输出流,使Client马上收到该字符串 (4)____________
System.out.println(\
System.out.println(\line=sin.readLine(); }
os.close();//关闭Socket输出流 is.close(); //关闭Socket输入流 socket.close(); //关闭Socket对象 }catch(Exception e){
System.out.println(\ } } }
3、定义类ThdTest,其父类为Thread类;并在主方法中创建一个ThdTest的对象,同时启动该线程对象。
//声明类ThdTest,其父类为Thread类 (1)___________________ {
public void run(){
for(int i = 0; i < 10; i++){
(2)___________________________//输出当前线程的名字和i的值 try{
(3)__________________//让当前线程休眠100ms }catch(Exception e){ e.printStackTrace ();} } } }
public class Demo{
public static void main(String[] args){
(4)___________________//创建一个ThdTest的对象
(5)________________//启动线程对象,使其进入就绪状态
} }
4、下面的程序利用线程输出从a到z的26个字母,每隔一秒钟输出一个字母,程序不完整,请阅读程序代码,根据注释要求在划线处补充完成代码。 public class Test4 implements Runnable {
charcharArray[]=new char[26]; public Test4() {
for(int i = 0; i charArray[i]=(char)(i+'a'); } } public void run() { try { for (int i = 0; i (1)____________//休眠一秒钟 System.out.print(charArray[i]); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String args[]) { Thread t = _(2)______________//实例化线程对象 (3)________________//启动线程 } } 5、定义类ThdDemo,实现接口Runnable;并在主方法中创建一个ThdDemo的对象td,然后使用对象td创建一个线程对象,同时启动该线程对象。 //声明类ThdDemo,实现接口Runnable (1)__________________ classThdDemo implements Runnable{ public void run(){ for(int i = 0; i < 10; i++){ //输出当前线程的名字和i的值 System.out.println(Thread.currentThread().getName() + “:” + i); try{ (2)_______________//让当前线程休眠100ms }catch(Exception e){ e.printStackTrace ();} } } } public class Demo{ public static void main(String[] args){ _(3)_______________//创建一个ThdDemo的对象td _(4)_____________//使用td创建线程对象 _(5)_______________//启动线程对象,使其进入就绪状态 } } 6、制作一个Applet小应用程序,设置其布局为BorderLayout,定义一个按钮和多行文本框并分别放到 “North”区域和“Center”区域。创建网页文件运行这个Applet程序。 importjava.applet.*; importjavax.swing.*; _(1)______________________________________________________//声明AppletDemo类,其父类为Applet,并实现接口ActionListener { Jbutton btn; JTextArea txt; public void init(){ _(2)_________________________//设置其布局为BorderLayout btn = new JButton(“Show Msg”); txt = new JTextArea(3, 20); _______________________ //把按钮btn放到“North”区域,txt放到“Center”区域 (3)_______________________ _(4)_______________________//给按钮注册监听器 } public void actionPerformed(ActionEvent e){ txt.append(btn.getText()); } } 网页文件为:
共分享92篇相关文档