老师,foreach中不是需要传入一个consumer对象么,为什么直接传入System.out的方法也可以呢
老师,这个还是不太懂,他是怎么自动把遍历出来的元素传入consumer中的啊
老师,这一步的代码是什么意思,我可以直接用user中的getname直接打印输出吗
FileInputStream如何读取中文呢?
为什么再Test03中根据百度服务器的ip地址获取设备的名称时获取不到,获取的结果为IP地址啊 代码如下:
Test2.zip
老师,图一红色方框的内容不是端口吗,然后HTTP服务器默认的端口为80,为什么填写HTTPS默认的端口号443也能获取到百度的服务器地址以及服务器的名称啊?
为什么会出现这种错误呢?
test03.zip
知道哪里错了, 谢谢老师,
老师,为什么我的会出现这样的错误
老师,在不适用字节缓冲区时候,read每次读取一个字节数据然后使用while循环写入,使用字节缓冲的话,是不是先读取字节文件写入字节数组,再把值赋给temp,然后一次循环一次循环的覆盖被写入的文件,直到最后一个字节,循环结束,就会把完整的数据都写入,所以temp就可以是length?
就是是不是在while循环没结束之前,目标文件是一次次的被写入不完整但是逐渐完整的数据,而且每次是覆盖呢?
--------------------------------------------OK
----------------------------------------------------------------------------------------------老师,这个怎么没有文档
老师,为啥有的时候新建节点,用的是Node,为啥有的时候就要Node<E>,呢
为什么这里不需要返回值
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()); } } }
疑问:为什么我运行后,会先取馒头再生产馒头?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637