package com.Thread; //生产者和消费者模式 public class produceThreadTest { public static void main(String[] args) { //创建缓冲区 SyncStack ss = new SyncStack(); //启动生产者线程 produce producer = new produce(ss); new Thread(producer).start(); //启动消费者线程 new customer(ss).start(); } } //定义馒头类代表数据 class Mantou{ private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } //定义缓冲区 class SyncStack { //用数组表示放馒头的盒子 private Mantou[] mantous = new Mantou[10]; //定义索引用来操作盒子 private int index; //定义放馒头的方法 public synchronized void SetMantou(Mantou mantou) { //当馒头放满时,线程进行等待 while (this.index == this.mantous.length) { try { /** * wait()是Object类下的方法; * wait()语法:该方法需要在synchronized语句块中使用; * wait()执行后,线程会将持有的对象锁释放,并进入阻塞状态、使其他持有该对象锁的线程可以继续运行 */ this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); this.mantous[this.index] = mantou; this.index++; } //定义取馒头的方法 public synchronized Mantou GetMantou() { while (this.index == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //唤醒等待中的其中一个线程、需使用在synchronized语句块中 this.notify(); this.index--; return this.mantous[this.index]; } } //定义生产者线程类 class produce implements Runnable{ private SyncStack syncStack; public produce(SyncStack syncStack) { this.syncStack = syncStack; } public void run() { for(int i = 0;i < 10;i++){ Mantou mantou = new Mantou(); mantou.setId(i); this.syncStack.SetMantou(mantou); System.out.println("生成馒头 "+mantou.getId()); } } } //定义消费者线程类 class customer extends Thread{ private SyncStack syncStack; public customer(SyncStack syncStack) { this.syncStack = syncStack; } public void run() { for (int i = 0;i<10;i++){ Mantou mantou = this.syncStack.GetMantou(); System.out.println("取馒头 "+mantou.getId()); } } }
疑问:为什么我运行后,会先取馒头再生产馒头?
非静态泛型方法这里第一句 new 静态泛型方法第一句不可以 new
没听懂 问题是非静态泛型方法需要new 静态泛型方法不能new吗
老师,网盘里课件是不是没更新呀
老师,HashMap不是当键相同的时候不是会替换嘛?怎么是链表存储呢?
我调用getDeclaredMethod方法为什么把public也输出了呀
ReturnOneParam returnOneParam = (int a) -> { System.out.println("ReturnOneParam"); return a; }; System.out.println(returnOneParam.method(2));
这个方法不是返回一个值a吗,为什么打印的时候方法内的打印语句会一起打印出来
坤哥!这个表达式是啥意思
老师有没有API帮助文档下载地址啊,麻烦发一下地址
老师有没有API文档的下载地址啊
package com.yu.test03; import java.io.*; public class TestException02 { public static void main(String[] args){ InputStream a = null; try { a = new FileInputStream("D://c"); System.out.println("代码不执行"); } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ try { a.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("TestException02.main"); } }
老师,如果try捕获异常后,最后一句输出代码可以执行吗
老师,抛出异常时,出现异常下面的代码会执行吗
老师好,什么时候是泛型的定义,什么时候是泛型的使用啊?
一. 下面是我的想法,希望老师能帮忙指正。
泛型的定义:
泛型类中,紧跟在类名后面的泛型就是在进行泛型定义
泛型接口中,紧跟在接口名后面的泛型就是在进行泛型定义
泛型方法中,在”public 返回值类型“或者“static 返回值类型”之间的就是泛型的定义
泛型的使用:除以上三种的任何情况
二.
根据本课无界通配符的内容,我在代码中试了一下,通配符好像不能进行泛型定义,只能进行泛型的使用,对吗?
三.还有就是我想反馈一下这个问答环节的使用体验,我只要稍微改动一下,本地保存成功就弹出来了,老是挡到我当前正在输出或者是修改的内容,这实际上是很影响我的使用体验的
客户端断开连接没有报错啊
private void msg(){ try (BufferedReader br =new BufferedReader(new InputStreamReader(this.socket.getInputStream())); PrintWriter pw = new PrintWriter(this.socket.getOutputStream())){ while (true){ pw.println(br.readLine()+" [ok]"); pw.flush(); } }catch (Exception e){ e.printStackTrace(); System.out.println(this.socket.getInetAddress()+" 断开连接"); } }
停止客户端之后,服务器会持续接受信息是为什么?
bufferedreader与socket关闭时不能在同一个try catch吗
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637