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();
}
}