pw = new PrintWriter(socket.getInputStream)为什么这个时候,不要将socket.getInputStream转化成字符,是PrintWriter自动能将字节流转换成字符流嘛?
老师,判断放馒头和取馒头线程是否等待,不用if语句而用while语句还是不同明白,能解释一下吗?
老师,之前这版块不是到十三章“手写服务器”的吗?为什么没有了的?
map.get(u)值返回为null,而不是我put的值。但是我用比较器进行比较,结果又是正常的。
public class TreeMapTest { public static void main(String[] args) { Map<Users,String> map = new TreeMap<>(); Users u1 = new Users("小a",18); Users u2 = new Users("小b",5); Users u3 = new Users("小c",9); String value = map.put(u1,"a"); System.out.println(value); System.out.println(map.get(u1)); map.put(u2,"b"); map.put(u3,"c"); Set<Users> keys = map.keySet(); for(Users u: keys){ System.out.println(u+"--- "+map.get(u)); } } } public class Users implements Comparable<Users>{ private String uname; private int uage; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getUage() { return uage; } public void setUage(int uage) { this.uage = uage; } public Users(String uname, int uage) { this.uname = uname; this.uage = uage; } public Users() { } //如果大于返回1,小于返回-1 ,相等返回0 @Override public int compareTo(Users o) { if(this.uage > o.uage){ return 1; }else{ return -1; } } @Override public String toString() { return "Users{" + "uname='" + uname + '\'' + ", uage=" + uage + '}'; } }
package com.bjsxt.ls.DuoXianCheng.线程并发; /* 线程并发:生产者和消费者模型 定义一个做馒头和取馒头的类,中间有一个缓冲区。做好的馒头放入缓冲区,取馒头从缓冲区取。 假设缓冲区容量是10,那么当做了十个馒头的时候,做馒头线程就要停止(阻塞)。当缓冲区没有馒头的时候,取馒头线程就要停止(阻塞)。 但是当做馒头线程放入馒头的时候,就要用notify提醒取馒头线程,不要一直处于阻塞状态; 同样当取馒头线程拿馒头的时候,也要提醒做馒头线程要做馒头,不要一直处于阻塞状态; */ //定义馒头类 class ManTou{ private int id; public ManTou(int id) { this.id = id; } public int getId() { return id; } } //定义缓冲区,用数组来存放馒头 class resitor{ private ManTou[] arr =new ManTou[10]; //创建一个长度为10,类型为ManTou的数组 private int index; //定义索引 //定义做馒头方法 //因为做馒头和取馒头都是对同样对象进行操作,所以这两个状态是要互斥,即同步的。所以要用synchronized使得这两个状态处于同步状态 //synchronzied放在方法名上相当于将synchronized(this){}将方法体包裹起来。 public synchronized void makeMantou(ManTou manTou){ //用while做出判断,如果当数组满的时候,就要用wait方法,使得此线程进入阻塞状态,不再生产馒头了; while (this.index == this.arr.length){ try { wait(); /*wait属于Object类只能用在synchronized块中。 此方法执行之后,在本方法所在的对象锁会被阻塞, 其他需要该对象锁的线程就可以继续运行了。*/ } catch (InterruptedException e) { e.printStackTrace(); } } //notify也属于Object类 this.notify();//当放入馒头的时候,要用notify唤醒取馒头的线程,以防拿馒头线程处于阻塞状态 。 //该方法会唤醒处于等待状态队列中的一个线程 this.arr[this.index]=manTou; index++; } //定义取馒头方法 public synchronized ManTou takeMantou(){ //用while判断,当索引为0,即缓冲区没有馒头的时候,就用wait阻塞此状态,不要再去取馒头了; while (this.index==0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify();//拿馒头的时候也要唤醒做馒头的线程,以防处于阻塞状态不做馒头了。 this.index--; return this.arr[this.index]; } } //创建做馒头线程 class makeThread extends Thread{ private resitor r; //定义一个类型是resitor的变量r public makeThread(resitor r){ this.r = r; } @Override public void run() { for (int i=1;i<11;i++){ System.out.println("生成第"+i+"个馒头"); ManTou manTou =new ManTou(i); this.r.push(manTou); } } } //创建取馒头线程 class takeThread extends Thread{ private resitor r; //定义一个类型是resitor的变量r public takeThread(resitor r){ this.r = r; } @Override public void run() { for (int i=1;i<11;i++){ ManTou manTou =this.r.pop(); System.out.println("拿走第"+i+"个馒头"); } } } public class ProducerThread { public static void main(String[] args) { resitor tt =new resitor(); new makeThread(tt).start(); new takeThread(tt).start(); } }
老师这是什么原因造成的?
老师,this 、字符串、Class、自定义这几个对象锁,分别都应该在什么情况下使用?
老师,用字符串作为对象缩的时候,是不是根据程序员类提供的构造方法的类型的。如果构造方法是一个整型或者其他类型,那么这个字符串索就没用了?
如果说我们拆到"[" 请问添加的字符串"]"是加到"["左边还是右边
老师我想问一下,Set不是单例集合么 为什么能存键值对呢
下面的6是什么意思,还有为什么i的值不是从0开始加的?
能不能这么理解,IO流操作出现后,main主线程进入阻塞状态,st.stop也运行不了,结束了IO流操作,main运行后子线程st就可以调用stop方法结束子线程st。
这一章节的文件拷贝实操总是报错,请老师看看,是需要我先创建文件吗?
package com.program; import java.io.*; public class CopyFileDirectory { public static void main(String[] args) { File src=new File("D:/a/java"); File des=new File("E:/b"); copyDir(src,des); } public static void copyDir(File SrcDir,File TargetDir) { if (TargetDir != null) { TargetDir.mkdir();//如果目的地的目录不存在,那么创建目录 } File[] files = SrcDir.listFiles(); for (File file : files) { if (file.isFile()) { copyFile(new File(SrcDir + "" + file.getName()), new File(TargetDir + "" + file.getName())); } else { copyDir(new File(SrcDir + "" + file.getName()), new File(TargetDir + "" + file.getName())); } } } public static void copyFile(File srcfile, File Desfile ){ BufferedInputStream bis= null; BufferedOutputStream bos=null; try{ bis=new BufferedInputStream(new FileInputStream(srcfile)); bos=new BufferedOutputStream(new FileOutputStream(Desfile)); byte[] by=new byte[1024]; int temp=0; while((temp=bis.read(by))!=-1){ bos.write(temp); } bos.flush(); }catch(Exception e){ e.printStackTrace(); }finally { try{ if (bos!=null){ bos.close(); } if (bis!=null){ bis.close(); } }catch (Exception e){ e.printStackTrace(); } } } }
有些同学觉得老师讲的不好,我觉得没必要这样说,觉得老师讲的快的可以弄成0.5倍速或者更慢。而且老师讲的快的地方基本都是前面讲过的基础操作,我觉得老师讲的很不错
什么情况用字节流读取文本文件并添加行号更好?直接用字符缓冲流字符流不是更方便吗?
老师,为什么switch的case后面要跟着break?我试着不加break,也不报错,但是显示出来的东西有点奇怪
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637