8.05(生产和消费线程)
来源:互联网 发布:以下 数据库的数据模型 编辑:程序博客网 时间:2024/05/08 20:12
江山如此多娇!
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运行后
0 0
- 8.05(生产和消费线程)
- 线程 --生产和消费问题
- 线程生产消费模式
- 线程---生产消费问题
- 线程间通信:生产线程和消费线程
- 生产和消费
- 继 生产和消费
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- 消费和生产 二条线程同步 的例子
- java线程同步----生产消费
- 生产消费线程讲解例子
- 线程间通信--生产消费
- 单线程生产,多线程消费
- 线程_生产消费模型
- Java中线程知识总结(生产消费模型)
- java学习9:生产和消费问题,先生产才能消费,线程间通信与等待
- java生产消费线程小例子
- java线程之生产与消费
- 关于Application
- Emoji 表情替换
- break和continue语句+函数+函数重载-第4天
- HDU 2553 N皇后问题
- iOS- <项目笔记>项目配置常见文件
- 8.05(生产和消费线程)
- JAVA基础篇二(Java,C++中的面向对象)
- The Shortest Path(矩阵快速幂构造有向图+floyed算法求图多源最短路)
- 统计硬币
- 新手的linux之旅 四、安装flash插件
- VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
- Linux sort 命令
- C++ 成员函数的重载与覆盖与隐藏
- hdu 5351 MZL's Border(15多校第五场1009)