知识点
多线程(续)
消费者与生产者(一般方法)
概述:生产者没生产一个产品,就休眠一段时间让消费者消费此产品,当消费者消费完该产品后,唤醒生产者继续生产者。在程序中,消费者与生产者的共享对象是产品,故以产品作为程序锁。
Productor
public class Productor implements Runnable{
private Production production;
public Productor(Production production) {
this.production = production;
}
@Override
public void run() {
while(true){
synchronized (production) {
System.out.println("生产者抢到仓库");
if(production.getNum()==0){
System.out.println("生产者生产产品");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
production.setNum(1);
try {
production.notify();
production.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
Consumer
public class Consumer implements Runnable {
private Production production;
public Consumer(Production production) {
this.production = production;
}
public void run() {
while (true) {
try {
Thread.sleep(500);
//当消费者消费完产品并激活production后会马上同生产者抢占仓库,所以此处让消费者休息一段时间避免抢占
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (production) {
System.out.println("消费者抢到仓库");
if (production.getNum() == 1) {
System.out.println("消费者正在消费");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
production.setNum(0);
production.notify();
}
}
}
}
}
Production
public class Production {
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) {
Production production = new Production();
Consumer xiaofeizhe = new Consumer(production);
Productor shengchengzhe = new Productor(production);
new Thread(shengchengzhe).start();
new Thread(xiaofeizhe).start();
}
}
synchronized方法
Productor
public class Productor implements Runnable{
private Production production;
public Productor(Production production) {
this.production = production;
}
@Override
public void run() {
while(true){
synchronized (production) {
production.productor();
}
}
}
}
Consumer
public class Consumer implements Runnable {
private Production production;
public Consumer(Production production) {
this.production = production;
}
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
production.consumer();
}
}
}
Production
public class Production {
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public synchronized void consumer(){
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("消费者抢到仓库");
if (getNum() == 1) {
System.out.println("消费者正在消费");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setNum(0);
notify();
}
}
public synchronized void productor(){
System.out.println("生产者抢到仓库");
if(getNum()==0){
System.out.println("生产者生产产品");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.setNum(1);
notify();
}
}
}
实现
public class Test {
public static void main(String[] args) {
Production production = new Production();
Consumer xiaofeizhe = new Consumer(production);
Productor shengchengzhe = new Productor(production);
new Thread(shengchengzhe).start();
new Thread(xiaofeizhe).start();
}
}
TCP/IP及时通讯系统
概述:此部分内容涉及到了java中swing、多线程、网络等部分知识,实现了客户端和服务器互联通信的功能。
服务器Server
public class StartServer extends JFrame {
private JPanel contentPane;
private JTextField textField;
private Socket socket;
private JList list;
private DefaultListModel<String> model;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StartServer frame = new StartServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StartServer() {
setTitle("聊天系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 332);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("启动服务器");
btnNewButton.setBounds(314, 10, 109, 96);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ServerSocket server = new ServerSocket(8080);
System.out.println("服务器启动了");
socket = server.accept();
System.out.println("有客户连接");
new Thread(new ServerRead(socket)).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("发送信息");
btnNewButton_1.setBounds(303, 212, 93, 61);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String word = "服务器:"+textField.getText()+"\n";
textField.setText("");
model.addElement(word);
bw.write(word);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
});
contentPane.add(btnNewButton_1);
textField = new JTextField();
textField.setBounds(22, 201, 253, 83);
contentPane.add(textField);
textField.setColumns(10);
JList list = new JList();
list.setBounds(10, 10, 281, 183);
model = new DefaultListModel<>();
list.setModel(model);
contentPane.add(list);
}
private class ServerRead implements Runnable{
private Socket socket;
public ServerRead(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
InputStream is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
String rMessage;
while(true){
try {
rMessage = br.readLine();
model.addElement(rMessage);
} catch (IOException e) {
System.out.println("有客户端失联了");
break;
}
}
}
}
}
客户端Client
public class StartClient extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JList list;
private Socket socket;
private DefaultListModel<String> model;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StartClient frame = new StartClient("客户端");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public StartClient() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 590, 493);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton button = new JButton("连接服务器");
button.setBounds(412, 406, 111, 39);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
socket = new Socket("192.168.0.65", 8080);
model.addElement("连接服务器成功");
new Thread(new ClientRead(socket)).start();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
contentPane.add(button);
JButton button_1 = new JButton("发送");
button_1.setBounds(410, 363, 111, 37);
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String word = "客户端:"+textField.getText()+"\n";
textField.setText("");
bw.write(word);
bw.flush();
model.addElement(word);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
contentPane.add(button_1);
textField = new JTextField();
textField.setBounds(1, 366, 387, 76);
contentPane.add(textField);
textField.setColumns(10);
JList list = new JList();
list.setBounds(2, 2, 382, 357);
model = new DefaultListModel<>();
list.setModel(model);
contentPane.add(list);
}
private class ClientRead implements Runnable{
private Socket socket;
public ClientRead(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader br = null;
try {
InputStream is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
} catch (IOException e) {
e.printStackTrace();
}
String rMessage;
while(true){
try {
rMessage = br.readLine();
model.addElement(rMessage);
} catch (IOException e) {
System.out.println("与服务器失联了");
break;
}
}
}
}
}