国产最新a级毛片无码专区_综合亚洲欧美日韩久久精品_日本成年片在线观看66_一本到九九av电影_一级毛片免费网站播放_国内精品久久人无码大片_国产人成视频99在线观看_欧美不卡在线一本二本_国产亚洲电影av_可以免费看黄色软件

知ing

Java面向?qū)ο蟪绦蛟O(shè)計

耿祥義,張躍平 編 / 清華大學(xué)出版社

青澀當(dāng)初 上傳

查看本書


習(xí)題15

14種狀態(tài):新建、運行、中斷和死亡。

2.有4種原因的中斷:(1JVMCPU資源從當(dāng)前線程切換給其他線程,使本線程讓出CPU的使用權(quán)處于中斷狀態(tài)。(2)線程使用CPU資源期間,執(zhí)行了sleep(int millsecond)方法,使當(dāng)前線程進(jìn)入休眠狀態(tài)。經(jīng)過參數(shù)millsecond指定的豪秒數(shù)之后,該線程就重新進(jìn)到線程隊列中排隊等待CPU資源,以便從中斷處繼續(xù)運行。(3)線程使用CPU資源期間,執(zhí)行了wait()方法,使得當(dāng)前線程進(jìn)入等待狀態(tài)。等待狀態(tài)的線程不會主動進(jìn)到線程隊列中排隊等待CPU資源,必須由其他線程調(diào)用notify()方法通知它,使得它重新進(jìn)到線程隊列中排隊等待CPU資源,以便從中斷處繼續(xù)運行。(4)線程使用CPU資源期間,執(zhí)行某個操作進(jìn)入阻塞狀態(tài),比如執(zhí)行讀/寫操作引起阻塞。進(jìn)入阻塞狀態(tài)時線程不能進(jìn)入排隊隊列,只有當(dāng)引起阻塞的原因消除時,線程才重新進(jìn)到線程隊列中排隊等待CPU資源,以便從原來中斷處開始繼續(xù)運行。

3.死亡狀態(tài),不能再調(diào)用start()方法。

4.新建和死亡狀態(tài)。

5.兩種方法:用Thread類或其子類。

6.使用 setPrority(int grade)方法。

7Java使我們可以創(chuàng)建多個線程,在處理多線程問題時,我們必須注意這樣一個問題:當(dāng)兩個或多個線程同時訪問同一個變量,并且一個線程需要修改這個變量。我們應(yīng)對這樣的問題作出處理,否則可能發(fā)生混亂。

8.當(dāng)一個線程使用的同步方法中用到某個變量,而此變量又需要其它線程修改后才能符合本線程的需要,那么可以在同步方法中使用wait()方法。使用wait方法可以中斷方法的執(zhí)行,使本線程等待,暫時讓出CPU的使用權(quán),并允許其它線程使用這個同步方法。其它線程如果在使用這個同步方法時不需要等待,那么它使用完這個同步方法的同時,應(yīng)當(dāng)用notifyAll()方法通知所有的由于使用這個同步方法而處于等待的線程結(jié)束等待

9.不合理。

10“吵醒”休眠的線程。一個占有CPU資源的線程可以讓休眠的線程調(diào)用interrupt 方法“吵醒”自己,即導(dǎo)致休眠的線程發(fā)生InterruptedException異常,從而結(jié)束休眠,重新排隊等待CPU資源。

11

public class Xiti11

{ public static void main(String args[])

????{ ??Cinema a=new Cinema();

????????a.zhang.start();

????????a.sun.start();

????????a.zhao.start();

????}

}

class TicketSeller ???//負(fù)責(zé)賣票的類。

{ ?int fiveNumber=3,tenNumber=0,twentyNumber=0;

???public synchronized void ?sellTicket(int receiveMoney)

???{ ?if(receiveMoney==5)

???????{ ?fiveNumber=fiveNumber+1;

??????????System.out.println(Thread.currentThread().getName()+

"給我5元錢,這是您的1張入場卷");

???????}

???????else if(receiveMoney==10) ??????????

????????{ while(fiveNumber<1)

????????????{ ??try { ?System.out.println(Thread.currentThread().getName()+"靠邊等");

???????????????????????wait(); ??

???????????????????????System.out.println(Thread.currentThread().getName()+"結(jié)束等待");

????????????????????}

???????????????catch(InterruptedException e) {}

????????????}

???????????fiveNumber=fiveNumber-1;

???????????tenNumber=tenNumber+1;

???????????System.out.println(Thread.currentThread().getName()+

"給我10元錢,找您5,這是您的1張入場卷"); ?

????????}

???????else if(receiveMoney==20) ??????????

????????{ ?while(fiveNumber<1||tenNumber<1)

????????????{ ??try { ?System.out.println(Thread.currentThread().getName()+"靠邊等");

???????????????????????wait(); ??

???????????????????????System.out.println(Thread.currentThread().getName()+"結(jié)束等待");

????????????????????}

???????????????catch(InterruptedException e) {}

????????????}

???????????fiveNumber=fiveNumber-1;

???????????tenNumber=tenNumber-1;

???????????twentyNumber=twentyNumber+1; ??

???????????System.out.println(Thread.currentThread().getName()+

"20元錢,找您一張5元和一張10元,這是您的1張入場卷");

?????????????????????????????

????????}

???????notifyAll();

???}

}

class Cinema implements Runnable ?????????

{ ?Thread zhang,sun,zhao;

???TicketSeller seller;

???Cinema()

???{ ?zhang=new Thread(this);

??????sun=new Thread(this);

??????zhao=new Thread(this);

??????zhang.setName("張小有");

??????sun.setName("孫大名");

??????zhao.setName("趙中堂");

??????seller=new TicketSeller();

???}

???public void run()

???{ ?if(Thread.currentThread()==zhang)

???????{ ?seller.sellTicket(20);

???????}

???????else if(Thread.currentThread()==sun)

???????{ ?seller.sellTicket(10);

???????}

???????else if(Thread.currentThread()==zhao)

???????{ seller.sellTicket(5);

???????}

???}

}

12參照本章例子9。

13參照本章例子19。

14BA

習(xí)題16

1URL對象調(diào)用InputStream openStream()?方法可以返回一個輸入流。

2.客戶端的程序使用Socket類建立負(fù)責(zé)連接到服務(wù)器的套接字對象稱為socket對象。
使用Socket的構(gòu)造方法Socket(String ?host,int port),建立連接到服務(wù)器的套接字對象。參考16.3.2

3JEditorPane

4會返回一個和客戶端Socket對象相連接的Socket對象

5域名/IP 地址 ?例如,www.sina.com.cn/202.108.35.210

6

(1) 客戶端

import java.net.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Client

{ ?public static void main(String args[])

???{ ?new ComputerClient();

???}

}

class ComputerClient extends Frame implements Runnable,ActionListener

{ ?Button connection,send;

???TextField inputText,showResult;

???Socket socket=null;

???DataInputStream in=null;

???DataOutputStream out=null;

???Thread thread;

???ComputerClient()

???{ ?socket=new Socket();

??????setLayout(new FlowLayout());

??????Box box=Box.createVerticalBox();

??????connection=new Button("連接服務(wù)器");

??????send=new Button("發(fā)送");

??????send.setEnabled(false);

??????inputText=new TextField(12);

??????showResult=new TextField(12);

??????box.add(connection);

??????box.add(new Label("輸入三角形三邊的長度,用逗號或空格分隔:"));

??????box.add(inputText);

??????box.add(send);

??????box.add(new Label("收到的結(jié)果:"));

??????box.add(showResult);

??????connection.addActionListener(this);

??????send.addActionListener(this);

??????thread=new Thread(this);

??????add(box);

??????setBounds(10,30,300,400);

??????setVisible(true);

??????validate();

??????addWindowListener(new WindowAdapter()

???????????????????{ ?public void windowClosing(WindowEvent e)

????????????????????????????{ ?System.exit(0);

????????????????????????????}

???????????????????});

???}

???public void actionPerformed(ActionEvent e)

???{ if(e.getSource()==connection)

??????{ ?try ?//請求和服務(wù)器建立套接字連接:

?????????{ if(socket.isConnected())

??????????????{}

???????????else

??????????????{ InetAddress ?address=InetAddress.getByName("127.0.0.1");

????????????????InetSocketAddress socketAddress=new InetSocketAddress(address,4331);

????????????????socket.connect(socketAddress);

????????????????in =new DataInputStream(socket.getInputStream());

????????????????out = new DataOutputStream(socket.getOutputStream());

????????????????send.setEnabled(true);

????????????????thread.start();

???????????????}

?????????}

?????????catch (IOException ee){}

??????}

?????if(e.getSource()==send)

??????{ ?String s=inputText.getText();

?????????if(s!=null)

???????????{ ?try { out.writeUTF(s);

??????????????????}

??????????????catch(IOException e1){}

???????????} ??????????????

??????}

???}

???public void run()

???{ ?String s=null;

??????while(true)

???????{ ???try{ ?s=in.readUTF();

??????????????????showResult.setText(s);

???????????????}

???????????catch(IOException e)

???????????????{ ?showResult.setText("與服務(wù)器已斷開");

??????????????????????break;

???????????????} ??

???????}

??}

}

2)服務(wù)器端

import java.io.*;

import java.net.*;

import java.util.*;

public class Server

{ ?public static void main(String args[])

???{ ?ServerSocket server=null;

??????Server_thread thread;

??????Socket you=null;

??????while(true)

???????{ ?try{ ?server=new ServerSocket(4331);

?????????????}

??????????catch(IOException e1)

?????????????{ ?System.out.println("正在監(jiān)聽"); //ServerSocket對象不能重復(fù)創(chuàng)建

?????????????}

??????????try{ ?System.out.println(" 等待客戶呼叫");

????????????????you=server.accept();

????????????????System.out.println("客戶的地址:"+you.getInetAddress());

?????????????}

?????????catch (IOException e)

?????????????{ ?System.out.println("正在等待客戶");

?????????????}

?????????if(you!=null)

???????????????{ ?new Server_thread(you).start(); //為每個客戶啟動一個專門的線程??

?????????????}

???????}

???}

}

class Server_thread extends Thread

{ ?Socket socket;

???DataOutputStream out=null;

???DataInputStream ?in=null;

???String s=null;

???boolean quesion=false;

???Server_thread(Socket t)

???{ ?socket=t;

??????try { ?out=new DataOutputStream(socket.getOutputStream());

?????????????in=new DataInputStream(socket.getInputStream());

??????????}

??????catch (IOException e)

??????????{}

???} ?

???public void run() ???????

???{ ?while(true)

??????{ ?double a[]=new double[3] ;

?????????int i=0;

?????????try{ ?s=in.readUTF();//堵塞狀態(tài),除非讀取到信息

???????????????quesion=false;

???????????????StringTokenizer fenxi=new StringTokenizer(s," ,");

?????????????????while(fenxi.hasMoreTokens())

???????????????????{ ?String temp=fenxi.nextToken();

??????????????????????try{ ?a[i]=Double.valueOf(temp).doubleValue();i++;

?????????????????????????}

??????????????????????catch(NumberFormatException e)

?????????????????????????{ ?out.writeUTF("請輸入數(shù)字字符");

????????????????????????????quesion=true;

?????????????????????????}

???????????????????}

????????????????if(quesion==false)

????????????????{ ?double p=(a[0]+a[1]+a[2])/2.0;

???????????????????out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));

????????????????}

????????????}

?????????catch (IOException e)

????????????{ ?System.out.println("客戶離開");

???????????????return;

????????????}

??????}

???}

}

7.參照本章例子16.6及以下代碼。

1)服務(wù)器

Server.java

import java.io.*;

import java.net.*;

import java.util.zip.*;

public class Server

{ ?public static void main(String args[])

???{ ?ServerSocket server=null;

??????ServerThread thread;

??????Socket you=null;

??????while(true)

???????{ ??try{ ?server=new ServerSocket(4331);

??????????????}

??????????catch(IOException e1)

?????????????{ ??System.out.println("正在監(jiān)聽"); ?

?????????????}

??????????try{ ?you=server.accept();

????????????????System.out.println("客戶的地址:"+you.getInetAddress());

?????????????}

?????????catch (IOException e)

?????????????{ ?System.out.println("正在等待客戶");

?????????????}

?????????if(you!=null)

?????????????{ ?new ServerThread(you).start(); ?

?????????????}

???????}

???}

}

class ServerThread extends Thread

{ ?Socket socket;

???ZipOutputStream out;

???String s=null;

???ServerThread(Socket t)

???{ ?socket=t;

??????try ?{ ?out=new ZipOutputStream(socket.getOutputStream());

???????????}

??????catch (IOException e){} ?

???}

???public void run() ???????

???{ ?try{out.putNextEntry(new ZipEntry("Example.java"));

??????????FileInputStream reader=new FileInputStream("Example.java");

??????????byte b[]=new byte[1024];

??????????int n=-1;

??????????while((n=reader.read(b,0,1024))!=-1)

??????????{ out.write(b,0,n); ?????????????//發(fā)送壓縮后的數(shù)據(jù)到客戶端。

??????????}

??????????out.putNextEntry(new ZipEntry("E.java"));

??????????reader=new FileInputStream("E.java");

??????????n=-1;

??????????while((n=reader.read(b,0,1024))!=-1)

??????????{ out.write(b,0,n); ??????????????//發(fā)送壓縮后的數(shù)據(jù)到客戶端。

??????????} ?

?????????reader.close();

?????????out.close();

????????}

?????catch (IOException e) {}

???}

}

2)客戶端

Client.java

import java.net.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.zip.*;

public class Client extends Frame implements Runnable,ActionListener

{ ?Button connection,getFile;

???TextArea showResult;

???Socket socket=null;

???ZipInputStream in;

???Thread thread;

???public ?Client()

???{ ?socket=new Socket(); ?????????????

??????connection=new Button("連接服務(wù)器,獲取文件內(nèi)容");

??????setLayout(new FlowLayout());

??????showResult=new TextArea(10,28);

??????add(connection);

??????add(showResult);

??????connection.addActionListener(this);

??????thread=new Thread(this);

??????setBounds(100,100,460,410);

??????setVisible(true);

??????addWindowListener(new WindowAdapter()

???????????????????{ ?public void windowClosing(WindowEvent e)

????????????????????????????{ ?System.exit(0);

????????????????????????????}

???????????????????});

???}

???public void run()

???{ byte b[]=new byte[1024];

?????ZipEntry zipEntry=null;

?????while(true)

???????{ try{ ?while((zipEntry=in.getNextEntry())!=null)

???????????????????{ ?showResult.append("\n"+zipEntry.toString()+":\n");

??????????????????????int n=-1;

??????????????????????while((n=in.read(b,0,1024))!=-1)

???????????????????????{ ?String str=new String(b,0,n);

??????????????????????????showResult.append(str);

???????????????????????}

???????????????????} ?????

?????????????}

???????????catch(IOException e) { ?}

???????}

??}

??public void actionPerformed(ActionEvent e)

??{ if(e.getSource()==connection)

????{ try { if(socket.isConnected())

??????????????{}

????????????else

??????????????{ InetAddress ?address=InetAddress.getByName("127.0.0.1");

????????????????InetSocketAddress socketAddress=new InetSocketAddress(address,4331);

????????????????socket.connect(socketAddress);

????????????????in=new ZipInputStream(socket.getInputStream());

????????????????thread.start();

??????????????}

?????????}

????????catch (IOException ee)

?????????{ ?System.out.println(ee);

?????????}

????}

??}

??public static void main(String args[])

??{ ?Client win=new ?Client();

??}

}

習(xí)題17

1

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Xiti1?extends Applet implements ActionListener{

???Button button;

???TextField text;

???int sum;

???public void init() {

??????button=new Button("點點看...");

??????text=new TextField(20); ?

??????add(button);

??????add(text); ?

??????button.addActionListener(this); ??

???}

???public void actionPerformed(ActionEvent e){

??????String str=button.getLabel();

??????text.setText("按鈕上寫著:"+str);

???}

}

超文本文件:

<applet code=Xiti1.class height=180 width=300>

</applet>

2

import java.applet.*;

import java.awt.*;

import java.awt.event.*; ?

import javax.swing.*;

public class Xiti2 extends Applet implements ActionListener

{ ?TextField text1,text2;

???Label label;

???public void init()

???{ ?text1=new TextField(10);

??????text2=new TextField(20);

??????Box box1=Box.createHorizontalBox();

??????Box box2=Box.createHorizontalBox();

??????Box boxV=Box.createVerticalBox();

??????box1.add(new Label("輸入一個數(shù)回車確定:"));

??????box1.add(text1);

??????label=new Label("數(shù)的平方:");

??????box2.add(label);

??????box2.add(text2);

??????boxV.add(box1);

??????boxV.add(box2);

??????add(boxV);

??????text2.setEditable(false);

??????text1.addActionListener(this);

???}

???public void actionPerformed(ActionEvent e)

???{ ?String number=e.getActionCommand();

??????try{ double n=Double.parseDouble(number);

???????????double m=n*n;

???????????label.setText(n+"的平方:");

???????????text2.setText(""+m);

???????????text1.setText("");

???????????validate();

?????????}

??????catch(NumberFormatException exp)

?????????{ ?text2.setText(""+exp);

?????????}

???}

}

3.參照本章例子17.2,17.3。


查看更多