java中的sleep是什么意思
SLEEP,英語單詞,名詞、動詞,作名詞時意為“睡眠,人名;(英)斯利普”,作動詞時意為“睡,睡覺”。單詞發音英[sli_p]美[sli_p]基本用法sleep用作動詞的基本意思是“睡眠”,也可作“為(某數量的人)提供床位”解。
sleep與介詞to連用時一般都省略冠詞。sleep用作名詞的意思是“睡眠”,是不可數名詞;加不定冠詞時,表示“一段時間的睡眠”。
sleep的進行時可以表示按計劃、安排或打算即將發生的動作,這時句中往往有表示將來的時間狀語或特定的上下文。一站式出國留學攻略 http://www.offercoming.com
SLEEP的Java中的函數
作用:程序暫停,延遲執行若干時間. 時間的長短由其參數決定 例如:
try{ Thread.sleep(500); /***休眠500毫秒***/ } catch (InterruptedException e) { e.printStackTrace(); }
注意:sleep函數是靜態函數,在執行時要拋出一個中斷異常, 必須對其進行捕獲并處理才可以使用這個函數
--------------------------------------------------
java 中sleep()方法或者wait()方法的使用
簡單說:sleep由線程自動喚醒,wait必須顯示用代碼喚醒。
sleep是Thread類的靜態方法。sleep的作用是讓線程休眠制定的時間,在時間到達時恢復,也就是說sleep將在接到時間到達事件事恢復線程執行,例如:
try{
System.out.println("I'm going to bed");
Thread.sleep(1000);
System.out.println("I wake up");
}
catch(IntrruptedException e) {
}
wait是Object的方法,也就是說可以對任意一個對象調用wait方法,調用wait方法將會將調用者的線程掛起,直到其他線程調用同一個對象的notify方法才會重新激活調用者,例如:
//Thread 1
try{
obj.wait();//suspend thread until obj.notify() is called
}
catch(InterrputedException e) {
}
java sleep()
下面分別是服務器端和客戶端的代碼,先運行服務器端,再運行客戶端
服務器端:
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(9000);
ServerFrame sf = new ServerFrame();
sf.launchFrame();
List list = new ArrayList(); // 創建數組
while (true) { // 開啟多線程
Socket s = ss.accept();
list.add(s);
Thread t = new ServerThread(s, list, sf);
t.start();
}
}
}
class ServerThread extends Thread { // 創建線程類
Socket s;
BufferedReader in;
PrintWriter out;
ServerFrame sf;
public ServerThread(Socket s, List list, ServerFrame sf) {
this.s = s;
this.sf = sf;
sf.sokectList = list;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
String str = in.readLine();
if (str == null)
continue;
sf.jta.append("接收到:" + str + "\n");
} catch (IOException e) {
return;
}
}
}
}
class ServerFrame {
List sokectList;
JTextField jtf;
JTextArea jta;
PrintWriter out;
public void launchFrame() {
JFrame frame = new JFrame("服務器端"); // 創建 frame對象
frame.tSize(400, 300); // 設置fram大小
frame.tLocation(300, 250);
jta = new JTextArea();
jta.tEditable(fal);
jtf = new JTextField();
jtf.addActionListener(new ActionListener() { // 注冊監聽器
public void actionPerformed(ActionEvent arg0) {
nd();
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf, "South");
frame.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE);
frame.tVisible(true);
}
public void nd() { // 輸出文字
String text = this.jtf.getText();
this.jtf.tText("");
if(sokectList==null) {
jta.append("無客戶端,發送失敗:" + text + "\n");
return;
}
jta.append("發送指令:" + text + "\n");
Iterator it = sokectList.iterator();
while (it.hasNext()) {
Socket socket = (Socket) (it.next());
try {
out = new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
jta.append("與客戶端連接失敗!\n");
continue;
}
out.println(text);
out.flush();
}
}
}
客戶端:
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc = new ChatClient();
while(true)
cc.receive();
}
static boolean boo;
Frame clientFrame;
Panel topPanel;
TextArea topTextArea;
Socket s;
BufferedReader in;
PrintWriter out;
public ChatClient() {
this.clientFrame = new Frame("客戶端");
this.clientFrame.tBounds(350, 250, 150, 250);
this.clientFrame.tResizable(fal);
this.clientFrame.tVisible(true);
this.topPanel = new Panel();
this.clientFrame.tBounds(350, 250, 150, 200);
this.topTextArea = new TextArea();
this.topPanel.add(this.topTextArea);
this.clientFrame.add(this.topPanel, BorderLayout.NORTH);
this.clientFrame.pack();
this.clientFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int var = JOptionPane.showConfirmDialog(null, "退出?",
"退出", JOptionPane.OK_CANCEL_OPTION);
if (var == JOptionPane.OK_OPTION)
System.exit(0);
}
});
try {
s = new Socket("127.0.0.1", 9000);//設置端口
in = new BufferedReader(new InputStreamReader(s.getInputStream()));//創建對象in
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));//創建對象out
} catch(UnknownHostException e) {//捕捉異常
e.printStackTrace();
} catch(ConnectException e) {
JOptionPane.showMessageDialog(null, "與服務器連接失敗,請確認服務器是否已經開啟!");
System.exit(-1);
} catch(IOException e) {
e.printStackTrace();
}
}
public void receive() { // 讀信息
try {
String text = in.readLine();
if("STOP".equals(text.toUpperCa())) {
if(boo) {
this.topTextArea.append("結束發送!\n");
boo = fal;
}
return;
}
if("START".equals(text.toUpperCa())) {
boo = true;
this.topTextArea.append("開始向服務器發送數據:\n");
new SendThread(this).start();
return;
}
this.topTextArea.append("接收到無效指令:" + text + "\n");
} catch (SocketException e) {
JOptionPane.showMessageDialog(null, "與服務器斷開連接!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
public void nd() {
while(boo) {
String str = Math.random() + "";
out.println(str);
out.flush();
this.topTextArea.append("發送:" + str + "\n");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SendThread extends Thread {
ChatClient cc;
SendThread(ChatClient cc) {
this.cc = cc;
}
@Override
public void run() {
// TODO Auto-generated method stub
cc.nd();
}
}