江山如此多娇!
java代码
一个两个用户的案例,生产者和消费者,当仓库产品为0时,生产者生产产品,然后休眠,消费者进入消费完产品后唤醒生产者,生产者再生产产品,如此循环:
/** 消费者类*/
public class Customer implements Runnable {
private Product pro;
public Customer(Product pro) {
this.pro = pro;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized (pro) {
System.out.println("消费者抢到仓库");
if (pro.getNum() != 0) {
System.out.println("消费");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pro.setNum(0);
pro.notify();//唤醒在此对象监视器上等待的单个线程
}
}
}
}
}
/** 生产者类*/
public class Productor implements Runnable {
private Product pro;
public Productor(Product pro) {
this.pro = pro;
}
@Override
public void run() {
while (true) {
synchronized (pro) {
System.out.println("生产者抢到仓库");
if (pro.getNum() == 0) {
System.out.println("生产");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pro.setNum(1);
try {
pro.wait();// 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
/**仓库类*/
public class Product {
private int num ;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
/**测试类*/
public class Test {
public static void main(String[] args) {
Product product = new Product();
Thread productor = new Thread(new Productor(product));
Thread customer = new Thread(new Customer(product));
productor.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
customer.start();
}
}
/*运行结果:生产者抢到仓库
生产
消费者抢到仓库
消费
生产者抢到仓库
生产
消费者抢到仓库
消费*/
……
一个简单的两人聊天代码
/* 服务端平台*/
public class Server extends JFrame {
private Socket socket;
private JPanel contentPane;
private JTextField textField;
private boolean isRunning = true;
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Server frame = new Server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Server() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 344, 320);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnNewButton = new JButton("启动服务");
btnNewButton.setBounds(10, 10, 155, 55);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动");
socket = server.accept();
Thread t = new Thread(new ServerRead(Server.this));
t.start();
System.out.println("有客户端连接");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
contentPane.setLayout(null);
contentPane.add(btnNewButton);
JButton button = new JButton("\u53D1\u9001");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
write();
}
});
button.setBounds(248, 233, 70, 23);
contentPane.add(button);
textField = new JTextField();
textField.setBounds(10, 222, 199, 50);
contentPane.add(textField);
textField.setColumns(10);
}
public void read() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
System.out.println("服务器接受信息 " + line);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String words = textField.getText();
System.out.println("服务器发送信息");
bw.write(words + "\n");
textField.setText("");
bw.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public class ServerRead implements Runnable {
private Server server;
public ServerRead(Server server) {
this.server = server;
}
@Override
public void run() {
while(server.isRunning()){
server.read();
}
}
}
/* 客户端平台*/
public class Client extends JFrame {
private Socket socket;
private JPanel contentPane;
private JTextField textField;
private JList list;
private DefaultListModel<String> modle;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Client() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 418);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(52, 307, 301, 63);
contentPane.add(textField);
JButton button = new JButton("\u53D1\u9001");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
write();
}
});
button.setBounds(363, 325, 73, 23);
contentPane.add(button);
list = new JList();
list.setBounds(52, 24, 291, 273);
modle=new DefaultListModel<>();
list.setModel(modle);
contentPane.add(list);
JButton button_1 = new JButton("\u8FDE\u63A5\u670D\u52A1\u5668");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
System.out.println("连接服务器");
socket=new Socket("192.168.0.66", 8080);
System.out.println("连接服务器成功");
Thread t=new Thread(new ClientRead(Client.this));
t.start();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button_1.setBounds(353, 46, 93, 65);
contentPane.add(button_1);
}
public void read() {
try {
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
modle.addElement("服务器说的"+line);
System.out.println(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void write(){
try {
OutputStream os=socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
String words= textField.getText();
bw.write(words+"\n");
modle.addElement(words);
textField.setText("");
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ClientRead implements Runnable {
private Client client;
public ClientRead(Client client) {
this.client = client;
}
@Override
public void run() {
while (true) {
client.read();
}
}
}
Server运行后
Client运行后