老师,我发现int temp和String temp两种方法都可以输出字符流喔,怎么回事呢
老师我想问一下FileReader,FileWriter,BufferedReader和BufferedWriter的close顺序是Br-Fr-Bw-Fw吗?Stream字节流也是一样的吗?
老师,我输出图片成功,可是图片只有1kb怎么回事
上节课中finally中没关闭serverSocket,这节课却关闭了,这是为什么呢
印象中之前讲过,PrintWriter输出流有自动刷新缓冲功能。
这节课中,如果不加pw.flush(),好像又不行,很奇怪啊
老师。这里的super(name)就是调用的父类构造方法对吧,那是不是说明Thread类中也有一个有参的构造方法
package com.bjsxt; /** * 生产者消费者模式 */ public class ProduceThread { public static void main(String[] args) { SyncStack ss = new SyncStack(); new Producer(ss).start(); new Consumer(ss).start(); } } /** * 创建缓冲区 */ class ManTou{ /**定义馒头类*/ private int id; public ManTou(int id) { this.id = id; } public int getId() { return id; } } class SyncStack{ /**定义缓冲区类*/ private ManTou[] mt = new ManTou[10]; // 定义存放馒头的盒子 private int index; // 定义操作盒子的索引 /**放馒头*/ public synchronized void push(ManTou manTou){ // 判断盒子满否 while (this.index == this.mt.length){ /** * 语法:wait(),该方法必须要在synchronized块中调用。 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态, * 其他需要该对象锁的线程就可以继续运行了。 */ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 唤醒取馒头的线程 /** * 语法:该方法必须要在synchronized块中调用。 * 该方法会唤醒处于等待状态队列中的一个线程。 */ this.notify(); this.mt[index] = manTou; index++; } /**取馒头*/ public synchronized ManTou pop(){ while (index == 0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); index--; return this.mt[index]; } } /** * 创建生产者消费者线程 */ class Producer extends Thread{ /**定义生产者线程类*/ private SyncStack ss; public Producer(SyncStack ss){ this.ss = ss; } @Override public void run() { for (int i=1; i<11; i++){ System.out.println("生产馒头:"+i); this.ss.push(new ManTou(i)); } } } class Consumer extends Thread{ /**定义消费者线程类*/ private SyncStack ss; public Consumer(SyncStack ss){ this.ss = ss; } @Override public void run() { for (int i=1; i<11; i++){ ManTou manTou = this.ss.pop(); System.out.println("消费馒头:"+i); } } }
附上代码,照着讲义敲的(只改了几个名字)
question:
缓冲区馒头满了,或者没了,为什么要用while()来判断
实测用if也是一样的,逻辑也没问题
老师,这个所谓的排队是不是也不一定严格的按照顺序进行线程的执行呢?
比如说这个,一先开始之后0第二个开始,然后执行了0一部分代码之后2开始,结果还是没有处理一线程,又继续执行了0线程
public List<E> getGrandChildren(E item){ //存放所有子孙结点中的元素 List<E> list = new ArrayList<>(); //获取当前结点的子结点 List<E> child = this.getChild(item); if(child ==null)return list; //遍历子结点 for (int i =0;i<child.size();i++){ //获取节点中的元素 E ele = child.get(i); //递归 List<E> tem = this.getGrandChildren(ele); list.add(ele); list.addAll(tem); } return list; }
老师好,如果获取生物的子孙结点(植物,动物,菌类),当递归到动物结点时,会递归(脊椎动物,脊索动物 等),一直往下的子结点走,那么菌类什么时候递归?
问题:子为父买烟案例中
控制台输出,有没有可能“儿子出门买烟”在“爸爸等待儿子买烟回 来”之前。
运行好几次,都是之后(就跟课堂上一样),但个人感觉,在前这种 可能也是有的哇,求解惑
public class JoinDemo { public static void main(String[] args) { System.out.println("爸爸和儿子买烟的故事"); Thread t = new Thread(new FatherThread()); t.start(); } } /** * 儿子买烟线程 */ class SonThread implements Runnable{ @Override public void run() { System.out.println("儿子出门买烟"); System.out.println("儿子买烟需要10分钟"); for (int i=1; i<11; i++){ System.out.println("第"+i+"分钟"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("儿子买烟回来了"); } } /** * 爸爸抽烟线程 */ class FatherThread implements Runnable{ @Override public void run() { System.out.println("爸爸想抽烟,发现烟抽完了"); System.out.println("爸爸让儿子去买一包红塔山"); Thread t = new Thread(new SonThread()); t.start(); System.out.println("等待儿子买烟回来"); try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("爸爸出门找儿子"); System.exit(1); } System.out.println("爸爸高兴的接过烟,并把零钱给了儿子"); } }
老师,为啥我的idea找不到jar啊
为什么不能使用BufferedInputStream处理FileInputStream流呢处理之后我的就报异常但是没有的话就不报异常
新建文件夹.rar
那么DataOutputStream把数据写入data.xt文件乱码问题怎么解决呢
是不是因为文本里面有汉字就会出现乱码啊
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637